当前位置: 代码迷 >> VB Dotnet >> 好手帮帮忙啊从数据库读取图片images = Image.FromStream(ms)提示“参数无效”
  详细解决方案

好手帮帮忙啊从数据库读取图片images = Image.FromStream(ms)提示“参数无效”

热度:83   发布时间:2016-04-25 02:10:28.0
高手帮帮忙啊!从数据库读取图片images = Image.FromStream(ms)提示“参数无效”
本帖最后由 zhangwanglan 于 2012-05-07 20:57:13 编辑
插入代码:
‘把图片存入数据库代码
Dim filename As String
            If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                filename = OpenFileDialog1.FileName
                Dim fs As FileStream
                fs = New FileStream(filename, FileMode.Open)
                Dim imagebyte() As Byte
                ReDim imagebyte(CInt(fs.Length - 1))
                fs.Read(imagebyte, 0, CInt(fs.Length))
                Dim db As String = "Data Source=lenovo-pc;Initial Catalog=wode;User ID=sa;password=123456" 'Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
                Dim conn As New SqlConnection
                conn.ConnectionString = db
                conn.Open()
                Dim comm As New SqlCommand
                comm.CommandText = "insert into dbo.图片 values ('imagebyte')"
                comm.Connection = conn
                comm.ExecuteNonQuery()
                MsgBox("tupiancuru ")
            End If
从数据库读取图片显示代码
Dim db As String = "Data Source=lenovo-pc;Initial Catalog=wode;User ID=sa;password=123456" 'Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
        Dim conn As New SqlConnection
        conn.ConnectionString = db
        conn.Open()
        Dim comm As New SqlCommand
        comm.CommandText = "select 数据 from 图片"
        comm.Connection = conn
 Dim reader As SqlDataReader
        reader = comm.ExecuteReader()
        If reader.Read() Then
            Dim ms As New MemoryStream(DirectCast(reader("数据"), Byte()))
            Dim images As Image
            images = Image.FromStream(ms)
            Me.BackgroundImage = images
        End If

------解决思路----------------------
 comm.CommandText = "insert into dbo.图片 values ('imagebyte')"
你这插入的时候就是错误的

你应该

comm.CommandText = "insert into dbo.图片 values (@xx)"
comm.Parameters.AddWithValue("@xx",imagebyte)
comm.ExecuteNonQuery()
  相关解决方案