从数据库中打开2进制流WORD文件
Dim con As SqlConnection = new SqlConnection("连接字符串")
Dim com As New SqlCommand
Dim dr As SqlDataReader
Dim fst As FileStream
Dim bwr As BinaryWriter
Dim numbyte As Long ‘用来记录word文件的大小
com.Connection = con
com.CommandText = "设置选择语句"
Try
con.Open()
dr = com.ExecuteReader(CommandBehavior.SequentialAccess)
Catch ex As Exception
MsgBox(ex.Message)
End Try
If dr.Read Then
‘取得WORD的大小
numbyte = dr.GetBytes(0, 0, Nothing, 0, Integer.MaxValue)
’创建文件流来将从数据库读取的WORD保存到当地磁盘
fst = New FileStream("保存的路径和名称.doc", FileMode.CreateNew, FileAccess.Write)
bwr = New BinaryWriter(fst)
Dim arr(numbyte) As Byte
将DATAREAD中的WORD放入数组中
dr.GetBytes(0, 0, arr, 0, CInt(numbyte))
bwr.Write(arr)
bwr.Flush()
bwr.Close()
fst.Close()
End If
con.Close()
Process.Start("winword", "保存的路径名称.doc")
Dim com As New SqlCommand
Dim dr As SqlDataReader
Dim fst As FileStream
Dim bwr As BinaryWriter
Dim numbyte As Long ‘用来记录word文件的大小
com.Connection = con
com.CommandText = "设置选择语句"
Try
con.Open()
dr = com.ExecuteReader(CommandBehavior.SequentialAccess)
Catch ex As Exception
MsgBox(ex.Message)
End Try
If dr.Read Then
‘取得WORD的大小
numbyte = dr.GetBytes(0, 0, Nothing, 0, Integer.MaxValue)
’创建文件流来将从数据库读取的WORD保存到当地磁盘
fst = New FileStream("保存的路径和名称.doc", FileMode.CreateNew, FileAccess.Write)
bwr = New BinaryWriter(fst)
Dim arr(numbyte) As Byte
将DATAREAD中的WORD放入数组中
dr.GetBytes(0, 0, arr, 0, CInt(numbyte))
bwr.Write(arr)
bwr.Flush()
bwr.Close()
fst.Close()
End If
con.Close()
Process.Start("winword", "保存的路径名称.doc")
有缺点,如果要对文档修改,要注意更新数据库,
还要将保存的WORD文件删除掉,防止再操作有文件重名的错误!
若同时打开多个WORD保存的名称可能要注意不要重复。
我的淘宝店:http://hamby.taobao.com