遇到一个问题,我用vb.net操作的数据库,插入新信息,代码如下
Private Shared ConnStr As String = "server=localhost;database=test; pooling=false"
Dim photo() As Byte = GetImage(_photoFilePath)‘转化为二进制函数
Dim connection As MySqlConnection = New MySqlConnection(ConnStr)
Try
Dim cmd As MySqlCommand = New MySqlCommand( _
"INSERT INTO master_section_position (mspId,DeleteMark, secRegion, " & _
"secId, secRelativeParts,mspName, mspPic) " & _
"Values(null,default,$secRegion, ?secId, ?secRelativeParts, " & _
"?MSPName, ?MSPPic)", connection)
cmd.Parameters.Add("$secRegion", _
MySqlDbType.Enum, 10).Value = _secRegion
cmd.Parameters.Add("?secId", _
MySqlDbType.Int32, 10).Value = _secId
cmd.Parameters.Add("?secRelativeParts", _
MySqlDbType.VarChar, 130).Value = _secRelativeParts
cmd.Parameters.Add("?MSPName", _
MySqlDbType.VarChar, 100).Value = _MSPName cmd.Parameters.Add("?MSPPic", _
MySqlDbType.LongBlob, photo.Length).Value = photo
connection.Open()
Return cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try
connection.Close()Public Shared Function GetImage(ByVal filepath As String) As Byte()
'打开现有文件以便读
Dim stream As FileStream = New FileStream(filepath, FileMode.Open, FileAccess.Read)
'用特定的编码将基元数据类型读作二进制值
Dim reader As BinaryReader = New BinaryReader(stream)
'将当前流中的所有字节读入字节数组中
Dim img() As Byte = reader.ReadBytes(stream.Length)
'关闭当前阅读器及基础流
reader.Close()
'关闭当前流并释放与之关联的所有资源
stream.Close()
'返回字节数组
Return img出现这样的错误 ---- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '杩旀嫇?5?鐨曢博Uu寮瞊 榇?璐?8涔?v鐭庨⒖绲C?閲憋箰c铏媞鍏堕粩p閳?璀??/?' at line 1不插入MSPPic这个图片字段时,可以插入,加了这个字段 就有上面的错误 但是我找不到是哪出问题的 。。求解
Private Shared ConnStr As String = "server=localhost;database=test; pooling=false"
Dim photo() As Byte = GetImage(_photoFilePath)‘转化为二进制函数
Dim connection As MySqlConnection = New MySqlConnection(ConnStr)
Try
Dim cmd As MySqlCommand = New MySqlCommand( _
"INSERT INTO master_section_position (mspId,DeleteMark, secRegion, " & _
"secId, secRelativeParts,mspName, mspPic) " & _
"Values(null,default,$secRegion, ?secId, ?secRelativeParts, " & _
"?MSPName, ?MSPPic)", connection)
cmd.Parameters.Add("$secRegion", _
MySqlDbType.Enum, 10).Value = _secRegion
cmd.Parameters.Add("?secId", _
MySqlDbType.Int32, 10).Value = _secId
cmd.Parameters.Add("?secRelativeParts", _
MySqlDbType.VarChar, 130).Value = _secRelativeParts
cmd.Parameters.Add("?MSPName", _
MySqlDbType.VarChar, 100).Value = _MSPName cmd.Parameters.Add("?MSPPic", _
MySqlDbType.LongBlob, photo.Length).Value = photo
connection.Open()
Return cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try
connection.Close()Public Shared Function GetImage(ByVal filepath As String) As Byte()
'打开现有文件以便读
Dim stream As FileStream = New FileStream(filepath, FileMode.Open, FileAccess.Read)
'用特定的编码将基元数据类型读作二进制值
Dim reader As BinaryReader = New BinaryReader(stream)
'将当前流中的所有字节读入字节数组中
Dim img() As Byte = reader.ReadBytes(stream.Length)
'关闭当前阅读器及基础流
reader.Close()
'关闭当前流并释放与之关联的所有资源
stream.Close()
'返回字节数组
Return img出现这样的错误 ---- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '杩旀嫇?5?鐨曢博Uu寮瞊 榇?璐?8涔?v鐭庨⒖绲C?閲憋箰c铏媞鍏堕粩p閳?璀??/?' at line 1不插入MSPPic这个图片字段时,可以插入,加了这个字段 就有上面的错误 但是我找不到是哪出问题的 。。求解
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货