插入代码:
‘把图片存入数据库代码
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()