insert into t_biao (xm,xb,zp,nl) values('"& txtxm.text &"','"& txtxb.text &"",'"& PBzp.Image &"','"& txtnl.text &"")结果显示有错误 说是没有为image设置的连接字符串 &
我根本不知道怎么办了 在书上也没见到这样的问题
有哪位朋友碰到过此类问题的 能不能指点我一下 可以让我节省一点时间接着往下赶活 谢谢你们了
Dim conn As New SqlClient.SqlConnection
Dim SQLtile As String
Dim fs As IO.FileStream
数据库链接:
Dim strConn As String = "data source=127.0.0.1;initial catalog=hhdata;user id=sa;password=1234567890;"
conn = New SqlClient.SqlConnection(strConn)
conn.Open()
打开图片:
Dim Opendlg As New OpenFileDialog
Opendlg.Title = "Select a picture file"
Opendlg.Filter = "(*.bmp)|*bmp|(*.jpg)|*.jpg"
Opendlg.ShowDialog()
TextBox2.Text = Opendlg.FileName.ToString()
fs = New IO.FileStream(Opendlg.FileName.ToString(), IO.FileMode.Open, IO.FileAccess.Read)
保存图片:
Dim sqltile As String
sqltile = "INSERT INTO employees (lastname,firstname,photo) VALUES (@lastname,@firstname,@photo)"
Dim cmdkh As New SqlClient.SqlCommand(sqltile, conn)
Dim Data(fs.Length) As Byte
fs.Read(Data, 0, Int(fs.Length))
Dim prm1 As New SqlClient.SqlParameter("@lastname", TextBox2.Text)
Dim prm2 As New SqlClient.SqlParameter("@firstname", TextBox2.Text)
Dim prm3 As New SqlClient.SqlParameter("@photo", SqlDbType.VarBinary, Int(fs.Length), ParameterDirection.Input, False, 0, 0, "", DataRowVersion.Current, Data)
cmdkh.Parameters.Add(prm1)
cmdkh.Parameters.Add(prm2)
cmdkh.Parameters.Add(prm3)
cmdkh.ExecuteNonQuery()
fs.Close()
提取显示图片:
Dim bh As New DataSet
Dim dabh As New SqlClient.SqlDataAdapter
SQLtile = "select * from employees"
dabh.SelectCommand = New SqlClient.SqlCommand(SQLtile, conn)
dabh.Fill(bh)
Dim data() As Byte = bh.Tables(0).Rows(0)("photo")
Dim stmphoto As New IO.MemoryStream(data)
PictureBox1.Image = Image.FromStream(stmphoto)
以上代码在VB2005上测试通过
注:photo的数据类型是 image
[此贴子已经被作者于2007-10-17 11:36:36编辑过]