如何在mysql的字符串查询中插入二进制读取器?
我正在学习如何将图片上传到MySql数据库。在你们都暴跳如雷,告诉我把图片上传到mysql数据库是不好的做法之前,我同意你们的观点,但这是学校的项目,这正是学校想要的 这是我在网上发现的,而且很有效。但我想了解它更多,也许修改它,以适应我的自定义潜艇。图像的数据类型也是如何在mysql的字符串查询中插入二进制读取器?,mysql,vb.net,Mysql,Vb.net,我正在学习如何将图片上传到MySql数据库。在你们都暴跳如雷,告诉我把图片上传到mysql数据库是不好的做法之前,我同意你们的观点,但这是学校的项目,这正是学校想要的 这是我在网上发现的,而且很有效。但我想了解它更多,也许修改它,以适应我的自定义潜艇。图像的数据类型也是blob Dim strFileName as String 'This is the file path Dim con As MySqlConnection = New MySqlConnection(cnn.Connecti
blob
Dim strFileName as String 'This is the file path
Dim con As MySqlConnection = New MySqlConnection(cnn.ConnectionString)
Dim cmd As MySqlCommand
Dim fs As FileStream
Dim br As BinaryReader
Dim FileName As String = strFileName.ToString
Dim ImageData() As Byte
fs = New FileStream(FileName, FileMode.Open, FileAccess.Read)
br = New BinaryReader(fs)
ImageData = br.ReadBytes(CType(fs.Length, Integer))
br.Close()
fs.Close()
If File.Exists(strFileName) = False Then MsgBox("Cannot locate file") : Return
Dim CmdString As String = "INSERT into tbl_maps(map_name,map_floor,map) Values('" & txtMapName.Text & "','" & cmbMapsFloor.Text & "',@Image)"
cmd = New MySqlCommand(CmdString, con)
cmd.Parameters.AddWithValue("Image", ImageData)
con.Open()
现在我的问题是,有没有办法将ImageData
插入MySqlCommand
的字符串中?这就是我通常将数据插入数据库的方式
Public Sub RunQuery()
On Error Resume Next
dr.Close()
InitQuery()
cmd.ExecuteNonQuery()
End Sub
Public Sub InitQuery()
With cmd
.CommandText = sql
.Connection = cnn
End With
End Sub
Private Sub something()
sql = "INSERT into tbl_maps(map_name,map_floor) Values('" & txtMapName.Text & "','" & cmbMapsFloor.Text & "')"
RunQuery()
MsgBox("Data saved successfully") 'or some way to check the data inserted
End Sub
我希望能有这样的东西
sql = "INSERT into tbl_maps(map_name,map_floor,map) Values('" & txtMapName.Text & "','" & cmbMapsFloor.Text & "','" & ImageData & "')"
RunQuery()
但是我知道这是不正确的,因为sql
是一个字符串,ImageData
不是。我只是想寻找一种方法来避免使用cmd.Parameters.AddWithValue(“Image”,ImageData)
或至少将其集成到我的RunQuery
sub中
注:这并不重要,但我喜欢一行代码集。因为我读台词就像读句子一样。例如:
If RecordNotExist() = true then Msgbox("Record not found") : txtsearch.selectall() : txtsearch.focus() : return
翻译:如果没有与搜索查询匹配的记录,请搜索其他记录。告诉您的老师尝试PostgreSQL!!!哈哈,没想到会有这样的评论。谢谢,我去看看。明白为什么当我看到mysql的时候我会想逃跑吗?