2014-02-06 176 views
2

我試着插入我的表從picturebox一些圖像:插入圖片框圖片到SQL Server數據庫

MemoryStream ms = new MemoryStream(); 
    pictureBox1.Image.Save(ms, ImageFormat.Jpeg); 
    byte[] photo = new byte[ms.Length]; 
    ms.Position = 0; 
    ms.Read(photo, 0, photo.Length); 

    command.CommandText = "INSERT INTO ImagesTable (Image) VALUES('" + photo + "')"; 
    command.CommandType = CommandType.Text; 
    command.ExecuteNonQuery(); 

我得到了數據庫中的以下結果:

ID Image 
6 0x53797374656D2E427974655B5D 

然而,當我插入一些圖片使用SQL script

insert into ImagesTable (Image) 
SELECT BulkColumn 
FROM Openrowset(Bulk 'C:\pinguins.jpg', Single_Blob) as img 

然後插入的數據如下所示:

ID Image 
4 0xFFD8FFE000104A464946000102010[.....] 

這裏二進制數據要長得多。

當我從數據庫中檢索該圖像回picturebox,它正確地顯示出來:

  command.CommandText = "SELECT Image FROM ImagesTable where ID = 4"; 

      byte[] image = (byte[])command.ExecuteScalar(); 
      MemoryStream ms1 = new MemoryStream(image); 
      pictureBox2.Image = Bitmap.FromStream(ms1); 

但隨着ID = 6(從pictureBox加載)檢索圖像時,我得到的錯誤。

ArgumentException: Parameter is not valid. 

我在做什麼錯?

我會很感激任何建議。

+0

做你長了這個解決辦法嗎? – anand360

回答

6

寫這種方式:]

Image img = Image.FromFile(@"C:\Lenna.jpg"); 
byte[] arr; 
using (MemoryStream ms = new MemoryStream()) 
{ 
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
    arr = ms.ToArray(); 
} 

Image img = picturebox1.Image(); 
    byte[] arr; 
ImageConverter converter = new ImageConverter(); 
    arr=(byte[])converter.ConvertTo(img, typeof(byte[])); 

command.CommandText = "INSERT INTO ImagesTable (Image) VALUES('" + arr + "')"; 
    command.CommandType = CommandType.Text; 
    command.ExecuteNonQuery(); 
+0

謝謝,但有沒有辦法直接從PictureBox存儲?不是從物理路徑?或者當我需要從位圖存儲圖片時就會出現這種情況。 –

+0

這樣寫:Image img = PictureBox1.Image(); – pankeel

+0

從數據庫中檢索時出現相同的錯誤。 –

1
using System; 
using System.Windows.Forms; 
using System.Data; 
using System.Data.SqlClient; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.IO; 
using Microsoft.VisualBasic; 

namespace inserting_imgs 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
    SqlConnection con; 
    SqlCommand cmd; 
    SqlDataAdapter adapter; 
    DataSet ds; int rno = 0; 
    MemoryStream ms; 
    byte[] photo_aray; 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     con = new SqlConnection("user id=sa;password=123;database=adil"); 
     loaddata(); 
     showdata(); 
    } 
    void loaddata() 
    { 
     adapter = new SqlDataAdapter("select sno,sname,course,fee,photo from student", con); 
     adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; 
     ds = new DataSet(); adapter.Fill(ds, "student"); 
    } 
    void showdata() 
    { 
     if (ds.Tables[0].Rows.Count > 0) 
     { 
      textBox1.Text = ds.Tables[0].Rows[rno][0].ToString(); 
      textBox2.Text = ds.Tables[0].Rows[rno][1].ToString(); 
      textBox3.Text = ds.Tables[0].Rows[rno][2].ToString(); 
      textBox4.Text = ds.Tables[0].Rows[rno][3].ToString(); 
      pictureBox1.Image = null; 
      if (ds.Tables[0].Rows[rno][4] != System.DBNull.Value) 
      { 
       photo_aray = (byte[])ds.Tables[0].Rows[rno][4]; 
       MemoryStream ms = new MemoryStream(photo_aray); 
       pictureBox1.Image = Image.FromStream(ms); 
      } 
     } 
     else 
      MessageBox.Show("No Records"); 
    } 
    private void browse_Click(object sender, EventArgs e) 
    { 
     openFileDialog1.Filter = "jpeg|*.jpg|bmp|*.bmp|all files|*.*"; 
     DialogResult res = openFileDialog1.ShowDialog(); 
     if (res == DialogResult.OK) 
     { 
      pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); 
     } 
    } 
    private void newbtn_Click(object sender, EventArgs e) 
    { 
     cmd = new SqlCommand("select max(sno)+10 from student", con); 
     con.Open(); 
     textBox1.Text = cmd.ExecuteScalar().ToString(); 
     con.Close(); 
     textBox2.Text = textBox3.Text = textBox4.Text = ""; 
     pictureBox1.Image = null; 
    } 
    private void insert_Click(object sender, EventArgs e) 
     { 
      cmd = new SqlCommand("insert into student(sno,sname,course,fee,photo) values(" + textBox1.Text + ",'" + textBox2.TabIndex + "','" + textBox3.Text + "'," + textBox4.Text + ",@photo)", con); 
      conv_photo(); 
      con.Open(); 
      int n = cmd.ExecuteNonQuery(); 
      con.Close(); 
      if (n > 0) 
      { 
       MessageBox.Show("record inserted"); 
       loaddata(); 
      } 
     else 
      MessageBox.Show("insertion failed"); 
    } 
    void conv_photo() 
    { 
     //converting photo to binary data 
     if (pictureBox1.Image != null) 
     { 
      //using FileStream:(will not work while updating, if image is not changed) 
      //FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read); 
      //byte[] photo_aray = new byte[fs.Length]; 
      //fs.Read(photo_aray, 0, photo_aray.Length); 

      //using MemoryStream: 
      ms = new MemoryStream(); 
      pictureBox1.Image.Save(ms, ImageFormat.Jpeg); 
      byte[] photo_aray = new byte[ms.Length]; 
      ms.Position = 0; 
      ms.Read(photo_aray, 0, photo_aray.Length); 
      cmd.Parameters.AddWithValue("@photo", photo_aray); 
     } 
    } 

    private void search_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      int n = Convert.ToInt32(Interaction.InputBox("Enter sno:", "Search", "20", 100, 100)); 
      DataRow drow; 
      drow = ds.Tables[0].Rows.Find(n); 
      if (drow != null) 
      { 
       rno = ds.Tables[0].Rows.IndexOf(drow); 
       textBox1.Text = drow[0].ToString(); 
       textBox2.Text = drow[1].ToString(); 
       textBox3.Text = drow[2].ToString(); 
       textBox4.Text = drow[3].ToString(); 
       pictureBox1.Image = null; 
       if (drow[4] != System.DBNull.Value) 
       { 
        photo_aray = (byte[])drow[4]; 
        MemoryStream ms = new MemoryStream(photo_aray); 
        pictureBox1.Image = Image.FromStream(ms); 
       } 
      } 
      else 
       MessageBox.Show("Record Not Found"); 
     } 
     catch 
     { 
      MessageBox.Show("Invalid Input"); 
     } 
    } 
    private void update_Click(object sender, EventArgs e) 
    { 
     cmd = new SqlCommand("update student set sname='" + textBox2.Text + "', course='" + textBox3.Text + "', fee='" + textBox4.Text + "', [email protected] where sno=" + textBox1.Text, con); 
     conv_photo(); 
     con.Open(); 
     int n = cmd.ExecuteNonQuery(); 
     con.Close(); 
     if (n > 0) 
     { 
      MessageBox.Show("Record Updated"); 
      loaddata(); 
     } 
     else 
      MessageBox.Show("Updation Failed"); 
    } 

    private void delete_Click(object sender, EventArgs e) 
    { 
     cmd = new SqlCommand("delete from student where sno=" + textBox1.Text, con); 
     con.Open(); 
     int n = cmd.ExecuteNonQuery(); 
     con.Close(); 
     if (n > 0) 
     { 
      MessageBox.Show("Record Deleted"); 
      loaddata(); 
      rno = 0; 
      showdata(); 
     } 
     else 
      MessageBox.Show("Deletion Failed"); 
    } 
    private void first_Click(object sender, EventArgs e) 
    { 
     rno = 0; showdata(); 
     MessageBox.Show("First record"); 
    } 

    private void previous_Click(object sender, EventArgs e) 
    { 

     if (rno > 0) 
     { 
      rno--; showdata(); 
     } 
     else 
      MessageBox.Show("First record"); 
    } 
    private void next_Click(object sender, EventArgs e) 
    { 
     if (rno < ds.Tables[0].Rows.Count - 1) 
     { 
      rno++; showdata(); 
     } 
     else 
      MessageBox.Show("Last record"); 
    } 
    private void last_Click(object sender, EventArgs e) 
    { 
     rno = ds.Tables[0].Rows.Count - 1; 
     showdata(); MessageBox.Show("Last record"); 
    } 
    private void exit_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 
} 

}

+0

請考慮包括一些關於你的答案的信息,而不是簡單地發佈代碼。我們嘗試提供的不僅僅是「修復」,而是幫助人們學習。你應該解釋原始代碼中的錯誤,你做了什麼不同,以及爲什麼你的改變起作用。 –