下面代码是我刚刚调试通过的
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()
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)
'******************************************************************
'函数: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
'******************************************************************
'函数: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
添加文件
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
首先确定文件的路径 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 存入数据库的对应字段中 ~