使用image类型字段可以存贮包括图片的大容量文件代码如下 下面的函数使用 DataReader 检索 BLOB 值并将其分配到一个字节数组。因为 BLOB 已经完全在内存中,无需将数据分块,因此此值被分配一个到 Byte 数组。对 GetBytes 方法有两个调用;第一个接收以字节为单位的 BLOB 的长度,并用于确定 Byte 数组的维度。第二个调用检索数据。FileStream 对象用于将 Byte 数组写入磁盘。注意:在 Visual Basic 中,在声明 Byte 数组时,您必须从 BLOB 的长度中减去 1,因为 Visual Basic 声明数组的上限而不是长度。在其他语言如 C# 或 JScript 中,使用的长度值不需要减 1。 Private Sub SqlBlob2File(ByVal DestFilePath As String) Dim PictureCol As Integer = 0 ' the column # of the BLOB field Dim cn As New SqlConnection("server=localhost;integrated security=yes;database=NorthWind") Dim cmd As New SqlCommand("SELECT Picture FROM Categories WHERE CategoryName='Test'", cn) cn.Open() Dim dr As SqlDataReader = cmd.ExecuteReader() dr.Read() Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte dr.GetBytes(PictureCol, 0, b, 0, b.Length) dr.Close() cn.Close() Dim fs As New System.IO.FileStream(DestFilePath, IO.FileMode.Create, IO.FileAccess.Write) fs.Write(b, 0, b.length) fs.Close() End SubPrivate Sub OlDbBlob2File(ByVal DestFilePath As String) Dim PictureCol As Integer = 0 ' the column # of the BLOB field Dim cn As New OleDbConnection("provider=sqloledb;server=localhost;user id=myuser;password=mypassword;initial catalog=NorthWind") Dim cmd As New OleDbCommand("SELECT Picture FROM Categories WHERE CategoryName='Test'", cn) cn.Open() Dim dr As OleDbDataReader = cmd.ExecuteReader() dr.Read() Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte dr.GetBytes(PictureCol, 0, b, 0, b.Length) dr.Close() cn.Close() Dim fs As New System.IO.FileStream(DestFilePath, IO.FileMode.Create, IO.FileAccess.Write) fs.Write(b, 0, b.Length) fs.Close() End Sub
下面的函数使用 DataReader 检索 BLOB 值并将其分配到一个字节数组。因为 BLOB 已经完全在内存中,无需将数据分块,因此此值被分配一个到 Byte 数组。对 GetBytes 方法有两个调用;第一个接收以字节为单位的 BLOB 的长度,并用于确定 Byte 数组的维度。第二个调用检索数据。FileStream 对象用于将 Byte 数组写入磁盘。注意:在 Visual Basic 中,在声明 Byte 数组时,您必须从 BLOB 的长度中减去 1,因为 Visual Basic 声明数组的上限而不是长度。在其他语言如 C# 或 JScript 中,使用的长度值不需要减 1。 Private Sub SqlBlob2File(ByVal DestFilePath As String)
Dim PictureCol As Integer = 0 ' the column # of the BLOB field
Dim cn As New SqlConnection("server=localhost;integrated security=yes;database=NorthWind")
Dim cmd As New SqlCommand("SELECT Picture FROM Categories WHERE CategoryName='Test'", cn)
cn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
dr.GetBytes(PictureCol, 0, b, 0, b.Length)
dr.Close()
cn.Close()
Dim fs As New System.IO.FileStream(DestFilePath, IO.FileMode.Create, IO.FileAccess.Write)
fs.Write(b, 0, b.length)
fs.Close()
End SubPrivate Sub OlDbBlob2File(ByVal DestFilePath As String)
Dim PictureCol As Integer = 0 ' the column # of the BLOB field
Dim cn As New OleDbConnection("provider=sqloledb;server=localhost;user id=myuser;password=mypassword;initial catalog=NorthWind")
Dim cmd As New OleDbCommand("SELECT Picture FROM Categories WHERE CategoryName='Test'", cn)
cn.Open()
Dim dr As OleDbDataReader = cmd.ExecuteReader()
dr.Read()
Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
dr.GetBytes(PictureCol, 0, b, 0, b.Length)
dr.Close()
cn.Close()
Dim fs As New System.IO.FileStream(DestFilePath, IO.FileMode.Create, IO.FileAccess.Write)
fs.Write(b, 0, b.Length)
fs.Close()
End Sub
OleDbDataAdapter thisAdapter= new OleDbDataAdapter("SELECT ....",Connection)
DataSet thisDateSet = new DataSEt();
thisAdapter.Fill(thisDataSet,"表名")
DataRow thisRow = thisDataSet.Tables["表名"].Row[0]);
FileStream thisStream = new FileStream(ImageFile,FileMode,FileAccess,Read);
// ImageFile为你要写入的文件全名
BinaryReader thisReader = new BinaryReader(thisStream);
thisRow["你的OLE字段名"] = thidReader.ReadBytes((int)thisStream.length);
thisAdapter.Update(thisDataSEt,"表名");2 读出来 写好数据连接,操作数据库
OleDbDataReader thisRedaer = thisCommand.ExecuteReader();
byte[] bs = (bytr[])thisReader["你的OLE字段"];
MemoryStream thisStream = new MemoryStream(bs,0,bs.Length,true,true)
PictureBox1.Image = Image.FromStream(thisSream); 要写成磁盘文件时
byte[] bs = (bytr[])thisReader["你的OLE字段"];
FileSream fs = new FileStream("你要存的文件名",FileMode.Create,FileAccess.Write);
fs.Write(bs,0,bs.Length);
应该懂了吧。
你很小气,才给那么点儿分。