Error: nvarchar is incompatible with image vb.net
Error: nvarchar is incompatible with image vb.net
当我更新 table 时出现错误 'nvarchar is incompatable with image'
在我的 table 中,图像字段数据类型是图像
这是我的代码
Dim ms2, ms3, ms4, ms5, ms6 As New MemoryStream
Dim arrPic1() As Byte = Nothing
Dim arrPic2() As Byte = Nothing
Dim arrPic3() As Byte = Nothing
Dim arrPic4() As Byte = Nothing
Dim arrPic5() As Byte = Nothing
If PCTOA.Image IsNot Nothing Then
Dim bm2 As Bitmap = New Bitmap(PCTOA.Image)
bm2.Save(ms2, PCTOA.Image.RawFormat)
arrPic1 = ms2.GetBuffer()
End If
If PctPO.Image IsNot Nothing Then
Dim bm3 As Bitmap = New Bitmap(PctPO.Image)
bm3.Save(ms3, PctPO.Image.RawFormat)
arrPic2 = ms3.GetBuffer()
End If
If PCTChallan.Image IsNot Nothing Then
Dim bm4 As Bitmap = New Bitmap(PCTChallan.Image)
bm4.Save(ms4, PCTChallan.Image.RawFormat)
arrPic3 = ms4.GetBuffer()
End If
If PCTInv.Image IsNot Nothing Then
Dim bm5 As Bitmap = New Bitmap(PCTInv.Image)
bm5.Save(ms5, PCTInv.Image.RawFormat)
arrPic4 = ms5.GetBuffer()
End If
If PCTTC.Image IsNot Nothing Then
Dim bm6 As Bitmap = New Bitmap(PCTTC.Image)
bm6.Save(ms6, PCTTC.Image.RawFormat)
arrPic5 = ms6.GetBuffer()
End If
'Query = "update companydetail set OASign=@ImageOA where companycode=@compcode"
'Dim myCommand As New System.Data.SqlClient.SqlCommand(Query, SQLDBCon)
'myCommand.Parameters.AddWithValue("@ImageOA", arrPic1)
'myCommand.Parameters.AddWithValue("@compcode", "1")
'myCommand.ExecuteNonQuery()
'SQLDBCon.Close()
Query = "update companydetail set OASign=@ImageOA,POSign=@ImagePO,ChallanSign=@ImageChallan,InvoiceSign=@ImageInv,TCSign=@ImageTC where companycode=@compcode"
Dim myCommand As New System.Data.SqlClient.SqlCommand(Query, SQLDBCon)
myCommand.Parameters.AddWithValue("@ImageOA", arrPic1)
myCommand.Parameters.AddWithValue("@ImagePO", arrPic2)
If arrPic3 IsNot Nothing Then
myCommand.Parameters.AddWithValue("@ImageChallan", arrPic3)
Else
myCommand.Parameters.AddWithValue("@ImageChallan", "0x00")
'Dim p As New SqlParameter("@ImageChallan", SqlDbType.Image)
'p.Value = "0x00"
'cmd.Parameters.Add(p)
End If
myCommand.Parameters.AddWithValue("@ImageInv", arrPic4)
myCommand.Parameters.AddWithValue("@ImageTC", arrPic5)
myCommand.Parameters.AddWithValue("@compcode", "1")
myCommand.ExecuteNonQuery()
这是我的表格
当我不在 challan 中上传图片时,必须在该字段中插入空值。
这怎么可能。?
谁能帮我?
提前谢谢你
Don't use AddWithValue
Just don't
基本上您无法控制此处使用的 SQL 服务器数据类型:
myCommand.Parameters.AddWithValue("@ImageChallan", "0x00")
对我和代码来说,“0x00”看起来像一个字符串。所以 AddWithValue
最终使用了字符串类型,而数据库说这是错误的类型。相反,明确指定类型。类似于:
Dim param = New SqlParameter("@ImageChallan", SqlDbType.Image)
param.Value = DBNull.Value
myCommand.Parameters.Add(param)
这样您就可以准确指定要使用的数据类型,并使用 DBNull
表示空值。
当我更新 table 时出现错误 'nvarchar is incompatable with image' 在我的 table 中,图像字段数据类型是图像
这是我的代码
Dim ms2, ms3, ms4, ms5, ms6 As New MemoryStream
Dim arrPic1() As Byte = Nothing
Dim arrPic2() As Byte = Nothing
Dim arrPic3() As Byte = Nothing
Dim arrPic4() As Byte = Nothing
Dim arrPic5() As Byte = Nothing
If PCTOA.Image IsNot Nothing Then
Dim bm2 As Bitmap = New Bitmap(PCTOA.Image)
bm2.Save(ms2, PCTOA.Image.RawFormat)
arrPic1 = ms2.GetBuffer()
End If
If PctPO.Image IsNot Nothing Then
Dim bm3 As Bitmap = New Bitmap(PctPO.Image)
bm3.Save(ms3, PctPO.Image.RawFormat)
arrPic2 = ms3.GetBuffer()
End If
If PCTChallan.Image IsNot Nothing Then
Dim bm4 As Bitmap = New Bitmap(PCTChallan.Image)
bm4.Save(ms4, PCTChallan.Image.RawFormat)
arrPic3 = ms4.GetBuffer()
End If
If PCTInv.Image IsNot Nothing Then
Dim bm5 As Bitmap = New Bitmap(PCTInv.Image)
bm5.Save(ms5, PCTInv.Image.RawFormat)
arrPic4 = ms5.GetBuffer()
End If
If PCTTC.Image IsNot Nothing Then
Dim bm6 As Bitmap = New Bitmap(PCTTC.Image)
bm6.Save(ms6, PCTTC.Image.RawFormat)
arrPic5 = ms6.GetBuffer()
End If
'Query = "update companydetail set OASign=@ImageOA where companycode=@compcode"
'Dim myCommand As New System.Data.SqlClient.SqlCommand(Query, SQLDBCon)
'myCommand.Parameters.AddWithValue("@ImageOA", arrPic1)
'myCommand.Parameters.AddWithValue("@compcode", "1")
'myCommand.ExecuteNonQuery()
'SQLDBCon.Close()
Query = "update companydetail set OASign=@ImageOA,POSign=@ImagePO,ChallanSign=@ImageChallan,InvoiceSign=@ImageInv,TCSign=@ImageTC where companycode=@compcode"
Dim myCommand As New System.Data.SqlClient.SqlCommand(Query, SQLDBCon)
myCommand.Parameters.AddWithValue("@ImageOA", arrPic1)
myCommand.Parameters.AddWithValue("@ImagePO", arrPic2)
If arrPic3 IsNot Nothing Then
myCommand.Parameters.AddWithValue("@ImageChallan", arrPic3)
Else
myCommand.Parameters.AddWithValue("@ImageChallan", "0x00")
'Dim p As New SqlParameter("@ImageChallan", SqlDbType.Image)
'p.Value = "0x00"
'cmd.Parameters.Add(p)
End If
myCommand.Parameters.AddWithValue("@ImageInv", arrPic4)
myCommand.Parameters.AddWithValue("@ImageTC", arrPic5)
myCommand.Parameters.AddWithValue("@compcode", "1")
myCommand.ExecuteNonQuery()
这是我的表格
当我不在 challan 中上传图片时,必须在该字段中插入空值。 这怎么可能。? 谁能帮我? 提前谢谢你
Don't use AddWithValue
Just don't
基本上您无法控制此处使用的 SQL 服务器数据类型:
myCommand.Parameters.AddWithValue("@ImageChallan", "0x00")
对我和代码来说,“0x00”看起来像一个字符串。所以 AddWithValue
最终使用了字符串类型,而数据库说这是错误的类型。相反,明确指定类型。类似于:
Dim param = New SqlParameter("@ImageChallan", SqlDbType.Image)
param.Value = DBNull.Value
myCommand.Parameters.Add(param)
这样您就可以准确指定要使用的数据类型,并使用 DBNull
表示空值。