将文件插入 MySQL 数据库 [英] Insert a File to MySQL database

查看:58
本文介绍了将文件插入 MySQL 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了如何在此处插入对我有帮助的图像,现在我需要将文件插入到我的数据库中,请帮助我我的列名(二进制)和 nvarchar(50) 的数据类型为 LONGBLOB名称(文件),但无法插入文件并给我一个错误,提示我在 sql 语法中有错误.任何帮助将不胜感激.谢谢

I searched on how to insert an image here that helps me,now I need to insert a file to my database,please help me I have a data type of LONGBLOB for column name (binary) and nvarchar(50) for column name (file),but cannot insert a file and gives me an error that says I have an error in sql syntax.Any help will much appreciated.Thanks

这是我的上传代码

 '          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

推荐答案

不是将文件转换为字节,而是将文件保存到公共文件夹并将文件链接保存在数据库表字段中.

Instead of converting to file into byte, save file to common folder and save link to the file in database table field.

这篇关于将文件插入 MySQL 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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