请各位高手帮看看:用vb.net将文件(word,rar等)转换成二进制的形式存入数据库,然后能进行读取

lusfaver 2006-03-23 09:55:19
请各位高手帮看看:用vb.net将文件(word,rar等)转换成二进制的形式存入数据库,然后能进行读取
...全文
360 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangchaocn 2006-05-23
  • 打赏
  • 举报
回复
mark
liujiweijun 2006-03-27
  • 打赏
  • 举报
回复
你们很强!路过!
yasmong 2006-03-27
  • 打赏
  • 举报
回复
下面代码是我刚刚调试通过的
button1为保存图片,button2为选择要保存的图片,button3为从数据库中读取图片并显示在picturebox中,当然修改一下就可存储到文件中
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim b() As Byte
Dim fs As filestream
Dim br As BinaryReader
Dim strsql As String
Try
fs = New FileStream(finfo.Name, FileMode.Open, FileAccess.Read) '将选择的图像文件读入到文件流
br = New BinaryReader(fs)
b = br.ReadBytes(fs.Length) '将文件内容放到字节数组
Catch ex As Exception
MsgBox("在将文件读入内存时出错:" + ex.Message)
End Try
br.Close()
fs.Close()

strsql = "insert into test(imageName,photo) "
strsql += "values('" & finfo.Name & "',@photo)" '二进制数据用@标识

Dim sqlCn As SqlConnection = New SqlConnection("server=dell;database=study;uid=sa;pwd=;")
sqlCn.Open()
Dim myCommand As SqlCommand = New SqlCommand(strsql, sqlCn)
myCommand.Parameters.Clear()
myCommand.Parameters.Add("@photo", OleDb.OleDbType.Binary)
myCommand.Parameters("@photo").Value = b
myCommand.ExecuteReader()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If (Me.OpenFileDialog1.ShowDialog() = DialogResult.OK) Then
finfo = New FileInfo(Me.OpenFileDialog1.FileName)
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim strsql As String
strsql = "select * from test"
Dim sqlCn As SqlConnection = New SqlConnection("server=dell;database=study;uid=sa;pwd=;")
Dim bw As BinaryWriter
Dim bufferSize As Integer = 100 '定义缓冲区长度为100
Dim outbyte() As Byte = New Byte(bufferSize - 1) {} '定义缓冲区
Dim retval As Long
Dim startIndex As Long = 0
Dim dr As SqlDataReader
Dim myCommand As SqlCommand = New SqlCommand(strsql, sqlCn)
sqlCn.Open()
Dim ms As New System.IO.MemoryStream '定义内存流对象,将数据库中存储的图像数据先放到内存流中
Try
bw = New BinaryWriter(ms)
dr = myCommand.ExecuteReader()
dr.Read()
TextBox1.Text = dr.GetString(1)
startIndex = 0
retval = dr.GetBytes(2, startIndex, outbyte, 0, bufferSize)

Do While retval = bufferSize '循环读取余下的内容,每次读取buffersize个字节
bw.Write(outbyte)
bw.Flush() '清空缓冲区
startIndex += bufferSize '移动位置指针
retval = dr.GetBytes(2, startIndex, outbyte, 0, bufferSize)
Loop

Dim image As Image = image.FromStream(ms)
Me.PictureBox1.Image = image '利用内存流在picturebox中生成图像

bw.Write(outbyte, 0, retval) '写入最后一块数据
bw.Flush()
bw.Close()
ms.Close()
dr.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
华芸智森 2006-03-27
  • 打赏
  • 举报
回复
'******************************************************************
'函数:UpImageToDb
'范围:Public
'作用:返回数据库中相应的图片字段
'参数:
' SqlCnn: SqlClient.SqlConnection.
' FileName:图片文件名
' DataTabName:数据表名
' PhoFiled:图片字段名
' WhereStr:Where条件语句.
'
'返回值:成功:TRUE, 失败:FALSE
'******************************************************************
Public Function UpImageToDb(ByRef SqlCnn As SqlClient.SqlConnection, _
ByVal FileName As String, _
ByVal DataTabName As String, _
ByVal PhoFiled As String, _
ByVal WhereStr As String) As Boolean
Dim StrSql As String

If SqlCnn.State = ConnectionState.Closed Then SqlCnn.Open()

If Len(Dir(FileName)) > 0 Then

Dim St As New FileStream(FileName, FileMode.Open, FileAccess.Read)
Dim Mbr As BinaryReader = New BinaryReader(St)
Dim Buffer(St.Length) As Byte
Dim SqlCommand As New SqlClient.SqlCommand

Try
Mbr.Read(Buffer, 0, CInt(St.Length))
SqlCommand.CommandText = "Update " & DataTabName & " set " & PhoFiled & "=@photo " & WhereStr
SqlCommand.Connection = SqlCnn
Dim Prm As New SqlClient.SqlParameter("@photo", SqlDbType.VarBinary, Int(St.Length), ParameterDirection.Input, False, 0, 0, "", DataRowVersion.Current, Buffer)
SqlCommand.Parameters.Add(Prm)
SqlCommand.ExecuteNonQuery() '执行更新语句
St.Close()
Return True
Catch
Err.Clear()
Return False
End Try
Else
StrSql = "Update " & DataTabName & " SET " & PhoFiled & "=Null " & WhereStr
ExeSqlCmd(SqlCnn, StrSql)
Return False
End If

End Function
华芸智森 2006-03-27
  • 打赏
  • 举报
回复
'******************************************************************
'函数:GetDbBinary
'范围:Public
'作用:将数据库中二进制字段保存到文件.
'参数:
' SqlCnn: SqlClient.SqlConnection.
' StrSql:Sql取值语句.
' FileName 写入的文件名.
'返回值:如果成功. TRUE ,失败. FALSE
'******************************************************************
Public Function GetDbBinary(ByRef SqlCnn As SqlClient.SqlConnection, _
ByVal StrSql As String, _
ByVal FileName As String) As Boolean

If SqlCnn.State = ConnectionState.Closed Then SqlCnn.Open()

Dim Cmd As New SqlClient.SqlCommand(StrSql, SqlCnn)
Dim ByteArr() As Byte

ByteArr = Cmd.ExecuteScalar()
Try
If (ByteArr.Length > 0) Then
Dim fs As FileStream = New FileStream(FileName, FileMode.OpenOrCreate)
Dim w As New BinaryWriter(fs)
w.Write(ByteArr)
w.Close()
fs.Close()
Return True
Else
Return False
End If
Catch
Return False
End Try
End Function
yzg100 2006-03-25
  • 打赏
  • 举报
回复
连接字符串,肯定是你自己数据库的连接字符串了,呵呵。
这个代码对于ACCESS也可以使用,只是ACCESS使用时,操作数据库的方法就得变一下,其它代码照旧。
sprite18 2006-03-24
  • 打赏
  • 举报
回复
连接字符串是什么?
yzg100 2006-03-24
  • 打赏
  • 举报
回复
第一个是数据库设计及操作
第二个是程序中实现的代码
注:SqlHelper类是Microsoft.ApplicationBlocks.Data
yzg100 2006-03-24
  • 打赏
  • 举报
回复
添加文件
Dim of As New OpenFileDialog
of.Filter = "All Files(*.*)|*.*"
If of.ShowDialog() = DialogResult.OK Then
Dim strFile() As String = of.FileName.Split("\")
Dim fileName As String = strFile(strFile.Length - 1)

Dim fs as New FileStream(of.FileName,FileMode.Open,FileAccess.Read)
Dim br as New BinaryReader(fs)
Dim buffer(fs.Length) As Byte
br.Read(Buffer,0,fs.Length)
fs.Close()

Dim sqlP(1) As SqlParameter
sqlP(0)=New SqlParameter("@FileName",SqlDbType.VarChar)
sqlP(0).Value=fileName
sqlp(1)=New SqlParameter("@FileContent",SqlDbType.Image)
sqlp(1).Value=buffer

SqlHelper.ExecuteNonQuery("连接字符串","sp_AddFile",sqlP)
End If

从数据库读取文件并写入硬盘
'注意提取记录时检查记录是否为空
Dim sqlP as New SqlParameter("@Id",SqlDbType.Int)
sqlP.Value=文件ID
Dim dt as DataTable=SqlHelper.ExecuteDataset("连接字符串","sp_getFile_ById",sqlP).Tables(0)
Dim fb As New FolderBrowserDialog
If fb.ShowDialog() = DialogResult.OK Then
Dim fileName as string=fb.SelectedPath() & "\" dt.Rows(0)("FileName")
Dim Content as byte()=dt.Rows(0)("FileContent")
Dim fs as New FileStream(fileName,FileMode.Create)
Dim br As New BinaryWriter(fs)
br.Write(Content)
br.Close()
fs.Close()
End If
yzg100 2006-03-24
  • 打赏
  • 举报
回复
保存文件时:保存文件名,文件后缀和文件内容

表: FileTest
FileId 文件ID (int)
FileName 文件名 (varchar(200))
FileContent 文件内容 (Image)

存储过程:
--获得所有
Create Proc sp_getFile_All
As
Select FileId,FileName,FileContent Form FileTest


--根据ID获取
Create Proc sp_getFile_ById
@Id int
As
FileId,FileName,FileContent Form FileTest Where FileId=@Id
If @@Error=0
Return 1
Else
Return -1

--添加
Create Proc sp_AddFile
@FileName varchar(200),
@FileContent Image
As
Insert Into FileTest(FileName,FileContent)Values(@FileName,@FileContent)
If @@Error=0 And @@RowCount=1
Return @@Identity
Else
Return -1


--修改
Create Proc sp_EditFile
@Id int,
@FileName varchar(200),
@FileContent Image
As
Update FileTest Set FileName=@FileName,FileContent=@FileContent
Where FileId=@Id
If @@Error=0 And @@RowCount=1
Return 1
Else
Return -1

Create Proc sp_DeleteFile
@Id int
As
Delete FileTest Where Id=@Id
If @@Error=0
Return 1
Else
Return -1
lqshadan 2006-03-23
  • 打赏
  • 举报
回复
哈 记得先 定义 fs,br
dim fs as FileStream
dim br as BinaryReader
lqshadan 2006-03-23
  • 打赏
  • 举报
回复
首先确定文件的路径 filepath 然后检查 filepath 下面是否有文件 如果有 那么:
Try
{
fs = New FileStream(filepath, FileMode.Open)
br = New BinaryReader(fs)
Dim obj As New Object
obj = br.ReadBytes(br.BaseStream.Length)
}catch ex as exception{}
然后把obj 存入数据库的对应字段中 ~

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧