错误“无法将值null插入图像列msg 515 16级状态2行1”, [英] error "cannot insert the value null into image column msg 515 level 16 state 2 line 1",

查看:91
本文介绍了错误“无法将值null插入图像列msg 515 16级状态2行1”,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我在将图像存储到mssql服务器时遇到问题我同时尝试了内存流和BLOB以及两个scinarios我都得到了相同的错误无法将值null插入图像列msg 515 16级状态2行1 ,任何建议都非常欢迎谢谢。



Hi I am having a problem storing image to mssql server I tried both Memory stream and BLOB as well and in both scinarios I get the same error "cannot insert the value null into image column msg 515 level 16 state 2 line 1", any suggestion is more than welcome Thanks.

 Dim sqlstring = " begin tran;"
 sqlstring &= " INSERT INTO tbl_customers (stnname, cardnum,
 family_name,  city, fam_mem_nu, id_num, mrkz_num) VALUES
 ('" & stn & "','" & cd & "', '" & fmnm & "','" & ct & "', '"
 & fnum & "', '" & idn & "', '" & cntr & "')"
 sqlstring &= "INSERT INTO tbl_customers(imag) SELECT * FROM
 OPENROWSET(BULK N'c:\temp\tempimg', SINGLE_BLOB) imag ;  "
 sqlstring &= "commit tran;"
 sql.CommandText = sqlstring
 sql.Connection = conn
 conn.Open()
 Dim ms As New MemoryStream

 img = CameraControl1.SnapshotSourceImage


 img.Save("c:\temp\tempimg", Imaging.ImageFormat.Png)

'CameraControl1.SnapshotSourceImage.Save
' PictureBox3.Image.Save(ms, PictureBox3.Image.RawFormat)
 ' ms.ToArray()
' Dim data As Byte() = ms.GetBuffer()
'  Dim p As New SqlClient.SqlParameter("@img", SqlDbType.VarBinary)
' p.Value = data
' sql.Parameters.Add(p)
Dim x As Integer = sql.ExecuteNonQuery

推荐答案

如评论中所述,您需要在图像列上允许空值或在一行中插入。
As mentioned in the comments, you need to either allow nulls on image column or do the insert in one line.


除了SQL注入之外其他人也提到的问题哟你的查询;这使得它很容易受到任何黑客攻击,你试图打破你的查询,看看你的引擎盖下的东西。问题是您尝试插入表中的图像(对象)是一个空对象,这就是为什么要向您显示错误的原因,它试图为您提供两个选项以允许在您可以通过打开SSMS(SQL Server Management Studio)或运行SQL命令来允许字段上的空值来实现的字段(列)。否则你可以创建一个条件块(块 if-else 语句)以确保如果图像是null对象,那么你不要将它插入表中,这会阻止完全出现这个问题。
Apart from the SQL Injection problem that others have also mentioned in your query; which makes it a lot vulnerable to any hacker you attempts to break you query to have a look at your under-the-hood stuff. The problem is that the image (object) that you're trying to insert into the table is a null object, that is why the error is being displayed to you, which is trying to give you two options to either Allow null values in the field (column) which you simply just can by opening the SSMS (SQL Server Management Studio) or by either running an SQL command, to allow the null values on the field. Otherwise you can create a conditional block (block of if-else statements) to ensure that if the image is null object, then you do not insert it into the table which would prevent any occurance of this problem at all.


这篇关于错误“无法将值null插入图像列msg 515 16级状态2行1”,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆