2015-05-02 133 views
2

我写了存储在数据库中的代码,但是如何检索?如何从图片框中的Sql数据库检索图像

Dim connection_string As String = "Data Source=(local);Initial Catalog=ashpaz;Integrated Security=True" 

    Using myconnection As New SqlConnection(connection_string) 
     Using myconn As New SqlConnection(connection_string) 
      Using mycommand As New SqlCommand() 
       Using mycomm As New SqlCommand() 
        myconnection.Open() 
        myconn.Open() 
        mycommand.Connection = myconnection 
        mycomm.Connection = myconn 
        Dim m As String = "insert into mavadbisk(mavad1,mavad2,mavad3,mavad4,mavad5,mavad6,mavad7,mavad8,mavad9,mavad10,mavad11,mavad12,mavad13,mavad14,mavad15,mavad16,mavad17,mavad18,mavad19,mavad20,mavad21,mavad22,mavad23,mavad24,mavad25,mavad26,mavad27,mavad28,mavad29,mavad30,mavad31,mavad32,mavad33,mavad34,mavad35,mavad36,mavad37,mavad38,mavad39,mavad40,mavad41,mavad42,mavad43,mavad44,mavad45,mavad46,mavad47,mavad48,mavad49,mavad50) values('" & boxes(1).Text & "','" & boxes(2).Text & "','" & boxes(3).Text & "','" & boxes(4).Text & "','" & boxes(5).Text & "','" & boxes(6).Text & "','" & boxes(7).Text & "','" & boxes(8).Text & "','" & boxes(9).Text & "','" & boxes(10).Text & "','" & boxes(11).Text & "','" & boxes(12).Text & "','" & boxes(13).Text & "','" & boxes(14).Text & "','" & boxes(15).Text & "','" & boxes(16).Text & "','" & boxes(17).Text & "','" & boxes(18).Text & "','" & boxes(19).Text & "','" & boxes(20).Text & "','" & boxes(21).Text & "','" & boxes(22).Text & "','" & boxes(23).Text & "','" & boxes(24).Text & "','" & boxes(25).Text & "','" & boxes(26).Text & "','" & boxes(27).Text & "','" & boxes(28).Text & "','" & boxes(29).Text & "','" & boxes(30).Text & "','" & boxes(31).Text & "','" & boxes(32).Text & "','" & boxes(33).Text & "','" & boxes(34).Text & "','" & boxes(35).Text & "','" & boxes(36).Text & "','" & boxes(37).Text & "','" & boxes(38).Text & "','" & boxes(39).Text & "','" & boxes(40).Text & "','" & boxes(41).Text & "','" & boxes(42).Text & "','" & boxes(43).Text & "','" & boxes(44).Text & "','" & boxes(45).Text & "','" & boxes(46).Text & "','" & boxes(47).Text & "','" & boxes(48).Text & "','" & boxes(49).Text & "','" & boxes(50).Text & "')" 
        Dim g As String = "insert into sabtbisko(namebis,caleri,carbo,charbi,prot,ghand,charbieshba,tosiasar,picmain,pic1,pic2,pic3,pic4,marhale1,marhale2,marhale3,tarikhche) values('" & txtnamebis.Text & "','" & txtcaleri.Text & "','" & txtcarbo.Text & "','" & txtcharbi.Text & "','" & txtprot.Text & "','" & txtghand.Text & "','" & txtcharbieshbashode.Text & "','" & txttosiasarashpaz.Text & "',@picmain,@pic1,@pic2,@pic3,@pic4,'" & txtmarhale1.Text & "','" & txtmarhale2.Text & "','" & txtmarhale3.Text & "','" & txttarikhche.Text & "')" 
        mycommand.CommandText = m 
        Dim ms As New MemoryStream() 
        picpicbis.BackgroundImage.Save(ms, picpicbis.BackgroundImage.RawFormat) 
        Dim data As Byte() = ms.GetBuffer() 
        Dim p As New SqlParameter("@picmain", SqlDbType.Image) 
        p.Value = data 
        mycomm.Parameters.Add(p) 
        Dim ms1 As New MemoryStream() 
        picpicone.BackgroundImage.Save(ms1, picpicone.BackgroundImage.RawFormat) 
        Dim data1 As Byte() = ms1.GetBuffer() 
        Dim p1 As New SqlParameter("@pic1", SqlDbType.Image) 
        p1.Value = data1 
        mycomm.Parameters.Add(p1) 
        Dim ms2 As New MemoryStream() 
        picpictwo.BackgroundImage.Save(ms2, picpictwo.BackgroundImage.RawFormat) 
        Dim data2 As Byte() = ms2.GetBuffer() 
        Dim p2 As New SqlParameter("@pic2", SqlDbType.Image) 
        p2.Value = data2 
        mycomm.Parameters.Add(p2) 
        Dim ms3 As New MemoryStream() 
        picpictree.BackgroundImage.Save(ms, picpictree.BackgroundImage.RawFormat) 
        Dim data3 As Byte() = ms3.GetBuffer() 
        Dim p3 As New SqlParameter("@pic3", SqlDbType.Image) 
        p3.Value = data3 
        mycomm.Parameters.Add(p3) 
        Dim ms4 As New MemoryStream() 
        picpicfour.BackgroundImage.Save(ms4, picpicfour.BackgroundImage.RawFormat) 
        Dim data4 As Byte() = ms4.GetBuffer() 
        Dim p4 As New SqlParameter("@pic4", SqlDbType.Image) 
        p4.Value = data4 
        mycomm.Parameters.Add(p4) 
        mycomm.CommandText = g 
        mycommand.ExecuteNonQuery() 
        mycomm.ExecuteNonQuery() 

       End Using 
      End Using 
     End Using 
    End Using 

行:Dim g As string ...图片已保存。如何从数据库检索图像到图片框?

+0

方法什么是你的image列中的数据类型转换'数据库'? 'Varbinary(max)'或'Image' –

+0

我的DataType是图像。 – Ali

+0

请发布不起作用的代码。该代码与您的问题无关。它也泄漏。 – Plutonix

回答

0

你应该这样做。

注:这是你的C#实现根据您的需要为VB

public DataTable ExecuteSimpleSelectQuery(string ConnectionString,string _Query, string DataTableName) 
     { 
       SqlConnection conn = new SqlConnection(ConnectionString); 
       SqlCommand cmd = new SqlCommand(); 

       DataTable dt = new DataTable(DataTableName); 
       if (_Query != string.Empty) 
       { 
        try 
        { 
         using (SqlDataAdapter SDA = new SqlDataAdapter(_Query,ConnectionString)) 
         { 
          SDA.Fill(dt); 
         } 
        } 
        catch (Exception ex) 
        { 
         throw ex; 
        } 
       } 
       return dt; 
      } 
     /* Below method will convert byte[] to image */ 

public Image Convert_To_Image(object _ImageValue) 
     { 
      if (_ImageValue == null) 
      { 
       return null; 
      } 
      byte[] imgArray = (byte[])_ImageValue; 
      MemoryStream mem = new MemoryStream(imgArray); 

      return Image.FromStream(mem); 
     } 

拨打以上这样

string Query= "Select YourimageColumn from yourtable"; 
DataTable dt= ExecuteSimpleSelectQuery("your connection string",Query,"DataTableName"); 
var image = dt.Rows[0]["YourimageColumn "]; 
/* If you did not convert image to byte[] before save then simply do this*/ 
yourpicturebox.Image = image; 
/* If you converted your image before save then do something like this*/ 
yourpicturebox.Image = Convert_To_Image(image); 
相关问题