更新时间:2023-01-12 10:41:12
这里有几个函数我放在一起,可以将任何类型的文件存储到SQL Server数据库中。
它是在Visual Basic中
SQL表1 Doc_ID int NULL 否 NULL
2 FileName nvarchar 255 否 NULL
3 FileExtention nvarchar 10 NO NULL
4 内容 varbinary -1 YES NULL
主要功能私人Sub btn_initUpload_Click(sender As Object,e As System.EventArgs)处理btn_initUpload.Click
Dim filePath As String =
Dim fInfo As FileInfo
Dim fileName As String,fileExt As String
Dim fileBinary()As Byte
Dim cn As SqlClient.SqlConnection = Nothing
Dim cmd As SqlClient.SqlCommand = Nothing
Dim procRtn As Integer
filePath = Me.txt_FilePath.Text
System.Windows.Forms.Cursor.Current = Windows.Forms.Cursors.WaitCursor
if filePath<> 然后
fInfo = FileIO.FileSystem.GetFileInfo(filePath)
fileName = fInfo.Name
fileExt = fInfo.Extension
尝试
fileBinary = Me.ConvertFileToBinary( filePath)
如果SQL_Ops.ConnectToDatabase(cn)= False则退出Sub
如果SQL_Ops.PrepCmdObject(cmd,cn,SQL_StoredProcedure.sp_saveDoc)= False那么
调用MsgBox(访问存储过程时出错。请联系数据库管理员。,vbCritical)
cn.Close()
抛出新的System.Exception(SQL失败)
结束如果
cmd.Parameters(@ FileName)。Value = fileName
cmd.Parameters(@ FileExtention)。Value = fileExt
cmd.Parameters(@ Content) .Value = fileBinary
procRtn = cmd.ExecuteNonQuery()
如果procRtn<> 0和Err.Number = 0然后
Me.FileList.DataBind()
Me.txt_FilePath.Text =
否则
抛出新的System.Exception(Proc Execution Error )
结束如果
Catch ex As Exception
Debug.Print(文件转换失败。)
结束尝试
否则
'什么都不做
结束如果
如果Not IsNothing(cmd)则cmd = Nothing
If Not IsNothing(cn)则cn.Close()
System.Windows .Forms.Cursor.Current = Windows.Forms.Cursors.Default
End Sub
这是将转换的函数任何文件到二进制数据私有函数ConvertFileToBinary(ByVal filePath As String)As Byte()
Dim fs As New FileStream(filePath,FileMode.Open,FileAccess。阅读)
Dim br As New BinaryReader(fs)
Dim convFile As Byt e()= br.ReadBytes(CInt(fs.Length))
br.Close()
fs.Close()
返回convFile
结束函数
how to import data from pdf file into sql server data base is possible...........?
Here's a couple of functions I put together to be able to store any kind of file into a SQL Server database.
It's in Visual Basic
SQL Table1 Doc_ID int NULL NO NULL 2 FileName nvarchar 255 NO NULL 3 FileExtention nvarchar 10 NO NULL 4 Content varbinary -1 YES NULL
Primary FunctionPrivate Sub btn_initUpload_Click(sender As Object, e As System.EventArgs) Handles btn_initUpload.Click Dim filePath As String = "" Dim fInfo As FileInfo Dim fileName As String, fileExt As String Dim fileBinary() As Byte Dim cn As SqlClient.SqlConnection = Nothing Dim cmd As SqlClient.SqlCommand = Nothing Dim procRtn As Integer filePath = Me.txt_FilePath.Text System.Windows.Forms.Cursor.Current = Windows.Forms.Cursors.WaitCursor If filePath <> "" Then fInfo = FileIO.FileSystem.GetFileInfo(filePath) fileName = fInfo.Name fileExt = fInfo.Extension Try fileBinary = Me.ConvertFileToBinary(filePath) If SQL_Ops.ConnectToDatabase(cn) = False Then Exit Sub If SQL_Ops.PrepCmdObject(cmd, cn, SQL_StoredProcedure.sp_saveDoc) = False Then Call MsgBox("An error occured while accessing the stored procedure. Please contact the database administrator.", vbCritical) cn.Close() Throw New System.Exception("SQL Failure") End If cmd.Parameters("@FileName").Value = fileName cmd.Parameters("@FileExtention").Value = fileExt cmd.Parameters("@Content").Value = fileBinary procRtn = cmd.ExecuteNonQuery() If procRtn <> 0 And Err.Number = 0 Then Me.FileList.DataBind() Me.txt_FilePath.Text = "" Else Throw New System.Exception("Proc Execution Error") End If Catch ex As Exception Debug.Print("File conversion failure.") End Try Else 'Do Nothing End If If Not IsNothing(cmd) Then cmd = Nothing If Not IsNothing(cn) Then cn.Close() System.Windows.Forms.Cursor.Current = Windows.Forms.Cursors.Default End Sub
This is the function that will convert any file into binary dataPrivate Function ConvertFileToBinary(ByVal filePath As String) As Byte() Dim fs As New FileStream(filePath, FileMode.Open, FileAccess.Read) Dim br As New BinaryReader(fs) Dim convFile As Byte() = br.ReadBytes(CInt(fs.Length)) br.Close() fs.Close() Return convFile End Function