我搜索了如何在这里插入一个帮助我的图像,现在我需要将一个文件插入到我的数据库中,请帮助我,我的数据类型为LONGBLOB列名(二进制)和nvarchar(50)列名称(文件),但无法插入文件并给我一个错误,说我在sql语法中有错误。任何帮助将非常感谢。谢谢
这是我的上传代码
' Extract File name
Dim FullFileName() As String = OpenFileDialog1.FileName.Split("\")
Dim fname As String = FullFileName.Last.ToString()
Try
' read data file
Dim fileContent() As Byte
Dim FSteream As New FileStream(OpenFileDialog1.FileName, FileMode.Open)
Dim BReader As New BinaryReader(FSteream)
fileContent = BReader.ReadBytes(FSteream.Length)
FSteream.Close()
BReader.Close()
' insert file to db
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "insert into file_tbl(file,binary) values (@name,@data)"
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@name", fname)
cmd.Parameters.AddWithValue("@data", fileContent)
cmd.ExecuteNonQuery()
' '----
Catch ex As Exception
MsgBox(ex.Message + " inside upload button")
Finally
MsgBox("File : " & fname & " Uploaded .")
conn.Close()
cmd.Dispose()
End Try
答案 0 :(得分:0)
不是将文件转换为字节,而是将文件保存到公共文件夹,并将链接保存到数据库表字段中的文件。