改了 还是不行 我想知道怎么打开上传到SQL里面的word文件?
程序代码:
Imports System Imports System.Drawing Imports System.Collections Imports Imports System.Windows.Forms Imports System.Data Imports System.Drawing.Imaging Imports Imports System.Data.SqlClient Public Class Form1 Private fs As FileStream Private ds As DataSet Private conn As New SqlConnection("SQLSERVER连接字符串,楼主自已写吧,注意在服务器中使用名为ceshi的数据库") Private currentpos As Integer = 9 Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click Dim Opendlg As New OpenFileDialog Opendlg.Title = "Select a picture file" Opendlg.Filter = "(*.doc)|*doc|(*.jpg)|*.jpg|全部文件|*.*" Opendlg.ShowDialog() lblPath.Text = Opendlg.FileName.ToString() fs = New FileStream(Opendlg.FileName.ToString(), IO.FileMode.Open, IO.FileAccess.Read) End Sub Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click '读入文件数据 Dim imgData(fs.Length - 1) As Byte fs.Read(imgData, 0, fs.Length - 1) fs.Close() Dim tempAdapter As SqlDataAdapter Dim tempDataset As New DataSet '打开数据库连接 conn.Open() '创建一个临时表用于存储上传的数据 Dim sqlcmd As New SqlCommand sqlcmd.Connection = conn = "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tmpFile]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[tmpFile]" sqlcmd.ExecuteNonQuery() = "CREATE TABLE [tmpFile] ([id] [int] IDENTITY (1, 1) NOT NULL ,[photo] [Image] NULL) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]" sqlcmd.ExecuteNonQuery() tempAdapter = New SqlDataAdapter("SELECT * FROM tmpFile WHERE 1=0", conn) Dim cb As New SqlCommandBuilder(tempAdapter) tempAdapter.Fill(tempDataset) '插入一条记录 Dim tempDataRow As DataRow tempDataRow = tempDataset.Tables(0).NewRow() tempDataRow("photo") = imgData tempDataset.Tables(0).Rows.Add(tempDataRow) tempAdapter.Update(tempDataset) '数据上传结束 Windows.Forms.Application.DoEvents() '下面的代码将上传的数据以文件形式存入SQLSERVER服务器的磁盘中 '下面的代码需要在窗体中画四个文本框: 'txtServer——服务器名称,txtUserName——登录用户名,——登录密码,txtDestFileName——需要保存到服务器的完整路径及文件名 '调用服务器端textcopy.exe,将上传的数据以文件形式存入SQLSERVER服务器的磁盘中 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 ceshi"'ceshi为数据库名 strExec = strExec & " /T tmpFile"'tmpFile为创建的临时表 strExec = strExec & " /C photo"'photo为创建的临时表tmpFile中的image字段 strExec = strExec & " /F " & txtDestFileName.Text strExec = strExec & " /W " & Chr(34) & "where id=1" & Chr(34) strExec = strExec & " /O" = "exec master.dbo.xp_cmdshell '" & strExec & "'" sqlcmd.ExecuteNonQuery() Windows.Forms.Application.DoEvents() '释放对象 sqlcmd.Dispose() tempAdapter.Dispose() tempDataset.Dispose() fs.Close() conn.Close() MsgBox("OK,数据上传完毕") End Sub End Class
上面的代码用于SQLSERVER客户端将本地文件上传并保存到服务器端指定的文件夹
至于如何限定客户端上传文件的大小及如果在数据库中存入上传文件的路径的代码,楼主自已应当能够完成
楼主拿去研究吧,以上代码在VS2008+SQLSERVER2000中测试通过
楼主下一步要做的,就是多用户并发的问题(创建临时表)以及服务器中文件已经存在的问题。