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文件的大小
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
com.Connection = con
com.CommandText = "设置选择语句"
Try
con.Open()
dr = com.ExecuteReader(CommandBehavior.SequentialAccess)
Catch ex As Exception
MsgBox(ex.Message)
End Try
![](/Images/OutliningIndicators/None.gif)
If dr.Read Then
![](/Images/OutliningIndicators/None.gif)
‘取得WORD的大小
numbyte = dr.GetBytes(0, 0, Nothing, 0, Integer.MaxValue)
![](/Images/OutliningIndicators/None.gif)
’创建文件流来将从数据库读取的WORD保存到当地磁盘
fst = New FileStream("保存的路径和名称.doc", FileMode.CreateNew, FileAccess.Write)
bwr = New BinaryWriter(fst)
![](/Images/OutliningIndicators/None.gif)
Dim arr(numbyte) As Byte
将DATAREAD中的WORD放入数组中
dr.GetBytes(0, 0, arr, 0, CInt(numbyte))
bwr.Write(arr)
bwr.Flush()
bwr.Close()
fst.Close()
![](/Images/OutliningIndicators/None.gif)
End If
![](/Images/OutliningIndicators/None.gif)
con.Close()
![](/Images/OutliningIndicators/None.gif)
Process.Start("winword", "保存的路径名称.doc")
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
有缺点,如果要对文档修改,要注意更新数据库,
还要将保存的WORD文件删除掉,防止再操作有文件重名的错误!
若同时打开多个WORD保存的名称可能要注意不要重复。