|  | 
	                
  
    |  |  
    | 
        
        รบกวนพี่ๆช่วยผมหน่อยครับ เกี่ยวกับการใส่รูปใน SQL server 2005 ครับ     |  
    |  |  
 
	
		|  |  |  |  |  
		|  |  | 
          
            | คือผมกำลังเขียนโปรแกรมที่ต้องมีการเรียกรูปจากฐานข้อมูลมาแสดงอะครับ แต่ผมไม่รู้ว่าจะเก็บรูปไว้ในฐานข้อมูลอย่างไร พยายามหาข้อมูลมาหลายวัน รู้แต่ว่าต้องเก็บเป็น Path ของรูป 
 ตอนนี้ที่ผมทำไว้คือ สร้างฟิล img มีชนิดเป็น text แล้วเก็บ path ประมาณนี้ครับ D:\image\01.jpg แต่ก็ยังรันไม่ผ่านครับ T-T มันขึ้นแบบนี้อะครับ
 
 Unable to cast object of type 'System.String' to type 'System.Byte[]'.
 
 โค๊ดที่เขียนเป็นแบบนี้ครับ
 
 Code (C#)
 
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;    
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DecistionSupportApplication
{
    public partial class decistionForm : Form
    {
        public decistionForm()
        {
            InitializeComponent();
        }
        private void decistionForm_Load(object sender, EventArgs e)
        {
        }
        private void exitButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("คูณต้องการออกจากระบบใช่หรือไม่ ?", "โปรดยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                Application.Exit();
        }
        private void seacthButton_Click(object sender, EventArgs e)
        {
            string conStr = @"Data Source=ARMSPRITE-PC\SQLEXPRESS;
			Initial Catalog=master;
            Integrated Security=SSPI;";
            SqlConnection connection = new SqlConnection(conStr);
            connection.Open();
            string sql = "SELECT* FROM DecistionNotebook";
            int nCheckCB = priceComboBox.SelectedIndex;
            if (nCheckCB == -1)
            {
                MessageBox.Show("กรุณาเลือกราคาที่ต้องการ", "เกิดข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            switch (priceComboBox.SelectedItem.ToString())
            {
                case "ต่ำกว่า 10,000 บาท":
                    sql += " WHERE price BETWEEN 0 AND 10000"; break;
                case "10,001 - 20,000 บาท":
                    sql += " WHERE price BETWEEN 10001 AND 20000"; break;
                case "20,001 - 30,000 บาท":
                    sql += " WHERE price BETWEEN 20001 AND 30000"; break;
                case "30,001 - 40,000 บาท":
                    sql += " WHERE price BETWEEN 30001 AND 40000"; break;
                case "40,001 - 50,000 บาท":
                    sql += " WHERE price BETWEEN 40001 AND 50000"; break;
                case "50,001 - 60,000 บาท":
                    sql += " WHERE price BETWEEN 50001 AND 60000"; break;
                case "แสดงทั้งหมด":
                    sql += " WHERE price BETWEEN 0 AND 100000 "; break;
            }
            int nCheckCB2 = brandComboBox.SelectedIndex;
            if (nCheckCB2 == -1)
            {
                MessageBox.Show("กรุณาเลือกยี่ห้อที่ต้องการ", "เกิดข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            switch (brandComboBox.SelectedItem.ToString())
            {
                case "Acer":
                    sql += " AND brand = 'Acer'"; break;
                case "Asus":
                    sql += " AND brand = 'Asus'"; break;
                case "Compaq":
                    sql += " AND brand = 'Compaq'"; break;
                case "Dell":
                    sql += " AND brand = 'Dell'"; break;
                case "Fujitsu":
                    sql += " AND brand = 'Fujitsu'"; break;
                case "HP":
                    sql += " AND brand = 'HP'"; break;
                case "Sony":
                    sql += " AND brand = 'Sony'"; break;
                case "Sumsung":
                    sql += " AND brand = 'Samsung'"; break;
                case "Toshiba":
                    sql += " AND brand = 'Toshiba'"; break;
                case "แสดงทั้งหมด":
                    sql += ""; break;
            }
            if (eVGARadioButton.Checked)
            {
                sql += " AND grapCard <> 'On Broad'";
            }
            else if (onBRadioButton.Checked)
            {
                sql += " AND grapCard = 'On Broad'";
            }
            SqlCommand command = new SqlCommand(sql, connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            data = new DataSet();
            adapter.Fill(data, "notebook");
            numRows = data.Tables["notebook"].Rows.Count;
            ShowData();
            connection.Close();
        }
        private DataSet data;
        private int curRow, numRows;
        private void ShowData()
        {
            DataTable DecistionNotebook = data.Tables["notebook"];
            if (DecistionNotebook.Rows.Count == 0)
            {
                MessageBox.Show("ไม่พบข้อมูลกรุณาเลือกใหม่อีกครั้ง", "ขออภัย", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            prodIDTextBox.Text = DecistionNotebook.Rows[curRow]["prodID"].ToString();
            brandTextBox.Text = DecistionNotebook.Rows[curRow]["brand"].ToString();
            modelTextBox.Text = DecistionNotebook.Rows[curRow]["model"].ToString();
            cpuTextBox.Text = DecistionNotebook.Rows[curRow]["CPU"].ToString();
            grapTextBox.Text = DecistionNotebook.Rows[curRow]["grapCard"].ToString();
            ramTextBox.Text = DecistionNotebook.Rows[curRow]["RAM"].ToString();
            harddiscTextBox.Text = DecistionNotebook.Rows[curRow]["harddisk"].ToString();
            priceTextBox.Text = DecistionNotebook.Rows[curRow]["price"].ToString();
            discDTextBox.Text = DecistionNotebook.Rows[curRow]["discDrive"].ToString();
            discPTextBox.Text = DecistionNotebook.Rows[curRow]["display"].ToString();
            wCamTextBox.Text = DecistionNotebook.Rows[curRow]["web_camera"].ToString();
            battTextBox.Text = DecistionNotebook.Rows[curRow]["battery"].ToString();
            wlanTextBox.Text = DecistionNotebook.Rows[curRow]["wireless_lan"].ToString();
            weightTextBox.Text = DecistionNotebook.Rows[curRow]["weight"].ToString();
            warranTextBox.Text = DecistionNotebook.Rows[curRow]["warranty"].ToString();
            usbTextBox.Text = DecistionNotebook.Rows[curRow]["portUSB"].ToString();
            byte[]pic = (byte[])data.Tables["notebook"].Rows[0]["img"];
            MemoryStream streamPic = new MemoryStream(pic);
            picBox.Image = Bitmap.FromStream(streamPic);
            CurRowlabel.Text = "รายการที่ "+ (curRow + 1) + " จากรายการสินค้าทั้งหมด " + numRows +" รายการ";
        }
        private void firstButton_Click(object sender, EventArgs e)
        {
            curRow = 0;
            ShowData();
        }
        private void backbutton_Click(object sender, EventArgs e)
        {
            if (curRow > 0)
            {
                curRow -= 1;
                ShowData();
            }
        }
        private void nextbutton_Click(object sender, EventArgs e)
        {
            if (curRow < (numRows - 1))
            {
                curRow += 1;
                ShowData();
            } 
        }
        private void lastButton_Click(object sender, EventArgs e)
        {
            curRow = numRows - 1;
            ShowData();
        }
    }
}
 
 
 Tag : Ms SQL Server 2005, C#
 
 
 |  
            |  |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2012-01-26 22:12:17 | By :
                          aRMSprite | View :
                          1395 | Reply :
                          8 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |  
		|  |  |  |  |  
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | byte[]pic = (byte[])data.Tables["notebook"].Rows[0]["img"]; 
 img น่าจะเป็น ชื่อไฟล์เฉยๆ หรือเปล่ามันไม่ใช่ byte[] นะครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-26 22:28:57 | By :
                            ikikkok |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | คือ เป็น Code ที่ดูมาจากหนังสืออะครับ ลองเปลี่ยนดูแล้ว แต่ไม่ผ่านครับผม เหมือนว่ามันอ้างอิงคอลัมที่เก็บข้อมูลอะครับ T-T 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-26 23:52:55 | By :
                            aRMSprite |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | รบกวนพี่ๆ เรื่อง การจัดเก็บรูปภาพในฐานข้อมูลด้วยครับ T-T 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-26 23:59:43 | By :
                            aRMSprite |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | รบกวนด้วยครับ  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-27 04:02:51 | By :
                            aRMSprite |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              |  
 รบกวนด้วยครับ ขอบคุณครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-27 07:36:54 | By :
                            aRMSprite |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | จากโค้ดตรงนี้ 
 Code (C#)
 
 byte[]pic = (byte[])data.Tables["notebook"].Rows[0]["img"]; 
MemoryStream streamPic = new MemoryStream(pic); 
picBox.Image = Bitmap.FromStream(streamPic); 
 
 ดูเหมือน img มิได้เก็บ Path หรอก แต่น่าจะเก็บ Content ของรูปเลยมากกว่า
 หนังสือมันบอกว่าให้เก็บ img เป็น Text เหรอ
 ดูผิดหรือเปล่าครับ
 
 ดูตัวอย่างจากนี่ล่ะกัน เป็นตัวอย่างการเก็บรูปในฐานข้อมูล และการเอารูปจากฐานข้อมูลมาแสดงผล
 http://www.codeproject.com/Articles/10861/Storing-and-Retrieving-Images-from-SQL-Server-usin
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-27 09:19:15 | By :
                            watcharop |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอบคุณครับ แล้วพอจะมีรูปแบบของพาทที่ใช้เก็บไหมครับ พอดีโปรแกรมที่่ผมทำมันดึงรูปอย่างเดียวอะครับ ไม่มีเก็บีูป รบกวนด้วยครับ ขอบคุณครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-27 18:00:30 | By :
                            aRMSprite |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 |  |