用VB和SQL Server实现文件上传(方案例)

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

时间: 2024-12-23 03:23:25

用VB和SQL Server实现文件上传(方案例)的相关文章

VB.Net Socket实现Http文件上传及下载类如何使用

问题描述 VB.Net Socket实现Http文件上传及下载类如何使用 门外汉求指教. 1.平台:SQL 2008 & appserv & mysql,VS2010 2.问题:①为实现文件上传.下载功能: ②参照了http://www.newxing.com/Tech/DotNet/VBDotNet/Socket_213.html 3.提问:①已添加上述网站中WebClient模块,现调用httpClient.UploadFile httpClient.DownLoadFile,WinF

Struts 框架 之 文件上传下载案例

Struts 框架 文件上传 1. 先准备 Struts 环境 (我使用的是struts 2.3.4版本) 导jar包:   jar包的具体作用在前面的文章有讲. 配置 web.xml <!-- Struts核心拦截器 --> <filter> <filter-name>Struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepare

PHP文件上传处理案例分析_php技巧

本文实例讲述了PHP文件上传处理的方法.分享给大家供大家参考,具体如下: 最近遇到一个事,把自己坑了好久,我想说说我开始的想法 PHP的上传机制封装的很完全,基本几行代码就能实现,他的实现流程是这样的 UPLOAD到文件到临时目录中–>使用move_uploadde_file()到指定的目录 这就是PHP上传流程,或者你在中途再进行一些验证.例如判断是不是通过upload方式提交的文档,或者文件的扩展是不是我们允许的 等等一系列验证.我给出简单的代码也算是抛砖引玉了. $targetFolder

JAVA使用commos-fileupload实现文件上传与下载实例解析_java

首先给大家介绍一文件的上传 实体类 import java.sql.Timestamp; /** * * @Decription 文件上传实体类 * */ public class Upfile { private String id;// ID主键 使用uuid随机生成 private String uuidname; // UUID名称 private String filename;//文件名称 private String savepath; // 保存路径 private Timest

一个加强的文件上传(VB.Net)

上传 这个文件上传应该可以满足一般的需要了,其中的添加到数据库记录,您可以按需要省略掉. 代码:upload.aspx <%@ Page Language="VB"%><% @ Import Namespace=" System.IO " %><% @ Import Namespace=" System.DATA " %><%@ import Namespace="System.Data.Sql

人人都是 DBA(V)SQL Server 数据库文件

原文:人人都是 DBA(V)SQL Server 数据库文件 SQL Server 数据库安装后会包含 4 个默认系统数据库:master, model, msdb, tempdb. SELECT [name] ,database_id ,suser_sname(owner_sid) AS [owner] ,create_date ,user_access_desc ,state_desc FROM sys.databases WHERE database_id <= 4; master mas

VS2008连接SQL Server数据库文件出错的解决方案

有园子里的朋友问到一个问题:系统上安装的是vs2008+Sql 2005 developer(没有安装Sql server 2005 Express )用代码直接连SQL Server服务器没有问题,但在项目中数据源改用SQL Server数据库文件时出错,提示错误如下:"与SQL Server文件(*.mdf)的连接要求安装SQL Server2005才能正常工作,请确认是否安装了该组件,--"英文版为"Connections to SQL Server Files (*.

如何分析SQL Server Trace文件

1.问题引出 老鸟为了重点栽培菜鸟,决定交给菜鸟一个艰巨而光荣的任务.这天,菜鸟刚到公司还未坐下,老鸟便劈头盖脸的问道:"你知道,我们如何Trace SQL Server执行语句吗?怎么手动分析这些Trace文件?如何将Trace File与Windows的性能监视器结合,看到每个语句执行时的性能开销?以及如何自动分析SQL Server Trace文件?". 菜鸟还没有反应过来,就被杀死了99%的老细胞,下意识的回答:"啊?". "去研究下吧"

SQL Server Log文件对磁盘的写操作大小是多少

原文:SQL Server Log文件对磁盘的写操作大小是多少 SQL Server 数据库有三种文件类型,分别是数据文件.次要数据文件和日志文件,其中日志文件包含着用于恢复数据库的所有日志信息,SQL Server总是先写日志文件ldf,数据变化写入mdf则可以滞后,所以日志写入的速度在一定程序上决定了SQL Server所能承载的写事务量,那么ldf写入大小是多少呢?   要知道SQL Server写 Log的大小,这里使用工具Process Monitor 这里设置一个Filter,以满足