【问题标题】:How to retrieve image from Sql database in picture box如何从图片框中的Sql数据库中检索图像
【发布时间】:2015-07-12 05:19:42
【问题描述】:

我编写了存储在数据库中的代码,但如何检索?

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

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

【问题讨论】:

  • DataBase 中图像列的 DataType 是什么? Varbinary(max)Image
  • 我的数据类型是 image 。
  • 请贴出无效的代码。该代码与您的问题无关。它也会泄漏。

标签: sql vb.net sql-server-2008


【解决方案1】:

你应该这样做。

注意:这是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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    相关资源
    最近更新 更多