stream保存到数据库出错
'emp_pic_image是emp_pic_table表中的字段,类型为image
'我以将剪贴板中的图片粘贴到picturebox2中
'以下是一个保存按钮的内容
Dim ms As New System.IO.MemoryStream
PictureBox2.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Dim MydateSave() As Byte
MydateSave = ms.ToArray
ms.Close()
Dim Mysql As String
Mysql = "update emp_pic_table set emp_image=@Mydate where emp_id='1534280'"
Dim cmd As New SqlClient.SqlCommand
cmd.CommandText = Mysql
cmd.Connection = emp_pic_cn 'emp_pic_cn为我定义的数据库连接
cmd.Parameters.Add("@Mydate", SqlDbType.Image, Int(MydateSave.Length))
cmd.Parameters("@Mydate").Value = MydateSave
cmd.ExecuteNonQuery() '程序执行到此就出错
出错内容如下:
未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
其他信息: 系统错误。
请高手帮我看看,改改代码
问题点数:20、回复次数:5Top
1 楼fangbuge(窗外的雨)回复于 2006-03-17 10:34:20 得分 0
试试
cmd.Parameters.Add("@Mydate", SqlDbType.Image)
Top
2 楼sundyhuang()回复于 2006-03-18 11:52:20 得分 0
Dim emp_pic_cn As New System.Data.SqlClient.SqlConnection
emp_pic_cn.ConnectionString = "workstation id=SGEDP08;packet size=4096;integrated security=SSPI;data source=SGEDP08;persist security info=False;initial catalog=develop"
emp_pic_cn.Open()
Dim ms As New System.IO.MemoryStream
Dim mypath4 As String
Dim myname As String = Trim(emp_id.Text)
mypath4 = "F:\DVC325\visjal_land\picture\" + myname + ".bmp"
PictureBox1.Image.Save(mypath4, System.Drawing.Imaging.ImageFormat.Bmp)
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim MydateSave() As Byte = ms.GetBuffer
If Not IsDBNull(MydateSave) Then
Dim mss As System.IO.MemoryStream = New System.IO.MemoryStream(MydateSave)
PictureBox3.Image = Image.FromStream(mss)
'把把picturebox1的图片,粘贴到picturebox3中
ms.Close()
Dim Mysql As String
Dim MyEmp_id As String
MyEmp_id = Trim(emp_id.Text)
Mysql = "update emp_pic_table set emp_image=@Mydate where emp_id='1534280'"
Dim cmd As New SqlClient.SqlCommand(Mysql, emp_pic_cn)
cmd.Parameters.Add("@Mydate", SqlDbType.Image, Int(MydateSave.Length))
cmd.Parameters("@Mydate").Value = MydateSave
Dim affected As Integer
affected = cmd.ExecuteNonQuery() '执行到这里就出错,把这句注释掉后,可以把picturebox1的图片,粘贴到picturebox3中
emp_pic_cn.Close()
MessageBox.Show("共变动:" & Str(affected) & "笔数据!")
如果保存到数据库中的字段设置中其他字段时,都可以保存,也就是说数据库接连没有问题,"emp_image"是数据库中的image类型字段,没有错
Top
3 楼jvvj(皆唯)回复于 2006-03-18 18:00:32 得分 20
以下代码,是我刚做的一个项目,该字段保存的是RichTextBox的rtf格式.给你做参考.
Private Sub UpDataUserMail(ByVal TempId As Int16)
Dim UdwCnn As New SqlConnection(ConnectString)
Try
UdwCnn.Open()
Dim GDCom As New SqlCommand("update UserMail set Content = @Content where Id= " & TempId, UdwCnn)
Dim userInput As New System.IO.MemoryStream
RichTextBox1.SaveFile(userInput, RichTextBoxStreamType.RichText)
Dim Buffer(userInput.Length - 1) As Byte
userInput.Position = 0
userInput.Read(Buffer, 0, userInput.Length)
Dim prm As New SqlParameter("@Content", SqlDbType.Image, Buffer.Length, ParameterDirection.Input, False, 0, 0, Nothing, DataRowVersion.Current, Buffer)
GDCom.Parameters.Add(prm)
GDCom.ExecuteNonQuery()
Catch ex As Exception
Finally
UdwCnn.Close()
End Try
End SubTop
4 楼sundyhuang()回复于 2006-03-19 09:12:04 得分 0
我添加了
try
cmd.ExecuteNonQuery()
Catch ex As SqlClient.SqlException
MessageBox.Show(ex.ToString)
End Try
出现如下错误:
system.data.sqlclient.sqlException:将截断字符串或二进制数据.
语句已终止.
At System.Data.SqlClient.SqlCommant.ExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream)
At System.Data.SqlClient.SqlCommand.ExecuteNonquery()
At emp_pic.Form1.save_pic_clice(Object sender,eventArgs e)in D:\emp_picture\Form1.vb:line 285)
请高手们帮我看看问题出在哪,我都纳闷好几天了
Top
5 楼sundyhuang()回复于 2006-03-20 11:48:26 得分 0
已找到问题所在,是我的数据库设计有问题,要把文件增长的选择框打勾Top




