Vb将word文档存入 SQL Server
以前的写的一个用vb 存 word /image 到sql server 中,字段类型image.
一,用vb 开发客户端
1, 新建一个标准工程,默认名为[工程1]。在该工程中会自动创建一个窗体,默认名为[Form1].
2, 单击[工程]菜单下的[部件]选项,在弹出的列表中选中microsoft common dialog control 6.0 (sp6),Microsoft ado data control 6.0 (sp6),点击应用,确定.
3, 在[Form1]窗体中放置一个textbox控件,三个commandbutton控件,一个adodc控件,一个commondialog控件。
以下是源程序:
Dim Constr As String 'ODBC路径
Dim FileName As String '文件名
Const BLOCKSIZE = 4096 '每次读写块的大小
Dim ADOCon As New ADODB.Connection 'ADODB Connection对象
Dim ADORst As New ADODB.Recordset 'ADODB Recordset 对象
Dim ADOFld As ADODB.Field 'ADODB Field 对象
Private Sub SaveToDB(ByRef Fld As ADODB.Field, DiskFile As String)
On Error GoTo SelectError: '错误处理, 没有选取文件
Dim byteData() 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 & " 无 内 容 或 不 存 在 !"
Else
NumBlocks = FileLength \ BLOCKSIZE '得到数据块的个数
LeftOver = FileLength Mod BLOCKSIZE '得到剩余字节数
Fld.Value = Null
ReDim byteData(BLOCKSIZE) '重新定义数据块的大小
For i = 1 To NumBlocks
Get SourceFile, , byteData() ' 读到内存块中
Fld.AppendChunk byteData() '写入FLD
Next i
ReDim byteData(LeftOver) '重新定义数据块的大小
Get SourceFile, , byteData() '读到内存块中
Fld.AppendChunk byteData() '写入FLD
Close SourceFile '关闭源文件
End If
SelectError:
Exit Sub
End Sub
Private Sub Command4_Click()
Dim Filter As String
On Error GoTo OpenError: 'Stops errors, like opening a false file path
Filter = "Aplication Files (*.doc)|*.doc;|" 'This is the normal way of opening a file with your own ext.
Filter = Filter + "JPEG Files (*.jpg)|*.jpg;|" 'Shows JPG Files(This is how you normally open a file)
'Filter = Filter + "All Formats(*.*)|*.alw;*.jpg;|" 'Show both formats at once
'Filter = Filter + "All Formats(*.*)|*.*;|"
CommonDialog1.Filter = Filter 'This is how you make the filter show in the filter section
CommonDialog1.ShowOpen 'Show the dialog now
CommonDialog1.FilterIndex = 1 'Makes the *.doc extention come up first as default
Text2.Text = CommonDialog1.FileName 'The file from the dialog is loaded in the picture box
OpenError:
Exit Sub
End Sub
Private Sub Form_Load()
Constr = "Provider=MSDASQL.1;Password=spica;Persist Security Info=True;User ID=spica;Data Source=dr;database=dr"
ADOCon.Open Constr '连接到数据库
'ADOCon.CursorLocation = adUseClient
ADORst.Open "select * from office", ADOCon, adOpenDynamic, adLockOptimistic
Set Adodc1.Recordset = ADORst
End Sub
Private Sub Command1_Click()
Dim filepath As String
FileName = CommonDialog1.FileTitle '取得文件名
filepath = CommonDialog1.FileName '取得文件绝对路径
If filepath = "" Then
titlestring = "错误!"
contentstring = "请先选取文件!"
MsgBox contentstring, vbInformation + vbDefaultButton + vbOKOnly, titlestring '注意msgbox的用法,可参考相关资料
End If
ADORst.AddNew '建立新记录
Set ADOFld = ADORst("content") '给ADODB.Field对象赋值
Call SaveToDB(ADOFld, FileName)
ADORst("filename") = FileName
ADORst("addtime") = Time
End Sub
Private Sub Command2_Click()
End
End Sub
其它的如 用asp /php读取就不在这里罗索了,如果哪位朋友要的,给我发邮件就行了。
