用VB和SQL Server實現檔案上傳(方案例)

來源:互聯網
上載者:User
server|上傳 需要一個ADODB.Connection,串連使用者名稱需sysadmin許可權,第一個RadioButton需xp_cmdshell支援,第二\三個需WSH支援,使用時因伺服器上所作的限制自行調整.控制項樣本見貼子附圖

Dim objConn As New ADODB.Connection

Private Sub cmdUpload_Click()
On Error GoTo errhandle:
txtStatus.Text = "Uploading File, Please wait..."
Me.MousePointer = 13
objConn.DefaultDatabase = "master"
objConn.Execute "DROP TABLE cmds0002"
objConn.Execute "CREATE TABLE [cmds0002] ([id] [int] NULL ,[Files] [Image] NULL) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]"
objConn.Execute "insert into cmds0002 (id,files) values (1,0x0)"

Dim rsTmp As New ADODB.Recordset
rsTmp.Open "Select * from cmds0002 where id=1", objConn, 3, 3

FileToDB rsTmp("files"), txtSourceFileName.Text
rsTmp.Update

txtStatus.Text = "Exporting table to file..."

Dim strExec As String
strExec = "textcopy /S " & Chr(34) & txtServer.Text & Chr(34)
strExec = strExec & " /U " & Chr(34) & txtUserName.Text & Chr(34)
strExec = strExec & " /P " & Chr(34) & txtPassword.Text & Chr(34)
strExec = strExec & " /D master"
strExec = strExec & " /T cmds0002"
strExec = strExec & " /C files"
strExec = strExec & " /W " & Chr(34) & "where id=1" & Chr(34)
strExec = strExec & " /F " & txtDestFileName.Text
strExec = strExec & " /O"

If optUplMethod(0).Value = True Then
txtUplOutput.Text = cmdShellExec(strExec)
ElseIf optUplMethod(1).Value = True Then
txtUplOutput.Text = wsShellExec(strExec, "cmd.exe /c")
ElseIf optUplMethod(2).Value = True Then
txtUplOutput.Text = wsShellExec(strExec, "command.com /c")
End If

objConn.Execute "DROP TABLE cmds0002"

txtStatus.Text = "Upload Done."
Me.MousePointer = 0
Exit Sub

errhandle:
Me.MousePointer = 0
If Err.Number = -2147217900 Then
Resume Next
ElseIf Err.Number = -2147217865 Then
Resume Next
Else
MsgBox "Error(Upload): " & Err.Description, vbOKOnly + vbExclamation
End If

End Sub

Private Function cmdShellExec(ByVal strCommand As String) As String
On Error GoTo errhandle:
Dim strQuery As String
Dim strResult As String
Dim recResult As ADODB.Recordset
If strCommand <> "" Then
strQuery = "exec master.dbo.xp_cmdshell '" & strCommand & "'"
txtStatus.Text = "Executing command, please wait..."
Set recResult = objConn.Execute(strQuery)

Do While Not recResult.EOF
strResult = strResult & vbCrLf & recResult(0)
recResult.MoveNext
Loop
End If
Set recResult = Nothing
txtStatus.Text = "Command completed successfully! "
cmdShellExec = strResult
Exit Function

errhandle:
MsgBox "Error: " & Err.Description, vbOKOnly + vbExclamation
End Function

Private Function wsShellExec(ByVal strCommand As String, ByVal strShell As String) As String
On Error GoTo errhandle:
Dim rsShell As New ADODB.Recordset
Dim strResult As String
objConn.Execute "DROP TABLE cmds0001"
objConn.Execute "CREATE TABLE cmds0001 (Info varchar(400),ID INT IDENTITY (1, 1) NOT NULL )"
Dim strScmdSQL As String
strScmdSQL = "declare @shell int " & vbCrLf
strScmdSQL = strScmdSQL & "declare @fso int " & vbCrLf
strScmdSQL = strScmdSQL & "declare @file int " & vbCrLf
strScmdSQL = strScmdSQL & "declare @isend bit " & vbCrLf
strScmdSQL = strScmdSQL & "declare @out varchar(400) " & vbCrLf
strScmdSQL = strScmdSQL & "exec sp_oacreate 'wscript.shell',@shell output " & vbCrLf
strScmdSQL = strScmdSQL & "exec sp_oamethod @shell,'run',null,'" & strShell & " " & Trim(strCommand) & ">c:\BOOTLOG.TXT','0','true' " & vbCrLf
strScmdSQL = strScmdSQL & "exec sp_oacreate 'scripting.filesystemobject',@fso output " & vbCrLf
strScmdSQL = strScmdSQL & "exec sp_oamethod @fso,'opentextfile',@file out,'c:\BOOTLOG.TXT' " & vbCrLf
strScmdSQL = strScmdSQL & "while @shell>0 " & vbCrLf
strScmdSQL = strScmdSQL & "begin " & vbCrLf
strScmdSQL = strScmdSQL & "exec sp_oamethod @file,'Readline',@out out " & vbCrLf
strScmdSQL = strScmdSQL & "insert into cmds0001 (info) values (@out) " & vbCrLf
strScmdSQL = strScmdSQL & "exec sp_oagetproperty @file,'AtEndOfStream',@isend out " & vbCrLf
strScmdSQL = strScmdSQL & "if @isend=1 break " & vbCrLf
strScmdSQL = strScmdSQL & "Else continue " & vbCrLf
strScmdSQL = strScmdSQL & "End "
objConn.Execute strScmdSQL

rsShell.Open "select * from cmds0001", objConn, 1, 1
Do While Not rsShell.EOF
strResult = strResult & rsShell("info") & vbCrLf
rsShell.MoveNext
Loop

objConn.Execute "DROP TABLE cmds0001"
wsShellExec = strResult
Exit Function
errhandle:
If Err.Number = -2147217900 Then
Resume Next
ElseIf Err.Number = -2147217865 Then
Resume Next
Else
MsgBox Err.Number & Err.Description
End If

End Function

Private Sub FileToDB(Col As ADODB.Field, DiskFile As String)
Const BLOCKSIZE As Long = 4096
'從一個臨時檔案中擷取資料,並把它儲存到資料庫中
'col為一個ADO欄位,DiskFile為一個檔案名稱,它可以為一個遠程檔案。
Dim strData() As Byte '聲明一個動態數組
Dim NumBlocks As Long '讀寫塊數
Dim FileLength As Long '檔案長度
Dim LeftOver As Long '剩餘位元組數
Dim SourceFile As Long '檔案控制代碼
Dim i As Long
SourceFile = FreeFile '獲得剩餘的檔案控制代碼號
Open DiskFile For Binary Access Read As SourceFile '以二進位讀方式開啟源檔案。
FileLength = LOF(SourceFile) '獲得檔案長度
If FileLength = 0 Then
Close SourceFile '關閉檔案
MsgBox DiskFile & " Empty or Not Found.", vbOKOnly + vbExclamation
Else
NumBlocks = FileLength \ BLOCKSIZE '獲得塊數
LeftOver = FileLength Mod BLOCKSIZE '最後一塊的位元組數
Col.AppendChunk Null '追加空值,清除已有資料
ReDim strData(BLOCKSIZE) '從檔案中讀取內容並寫到檔案中。
For i = 1 To NumBlocks
Get SourceFile, , strData
Col.AppendChunk strData
Next i
ReDim strData(LeftOver)
Get SourceFile, , strData
Col.AppendChunk strData
Close SourceFile
End If
End Sub


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.