寫入數據庫FileStream fs=File.OpenRead(filename);
content=new byte[fs.Length];
fs.Read(content, 0,content.Length);
fs.Close();讀出
byte[] b = (byte[])ds.Tables[0].Rows[0][0];
MemoryStream m = new MemoryStream(b);
Bitmap bb = new Bitmap(m);
pictureBox.Image = bb;
報錯
Bitmap bb = new Bitmap(m);
使用無效的參數
content=new byte[fs.Length];
fs.Read(content, 0,content.Length);
fs.Close();讀出
byte[] b = (byte[])ds.Tables[0].Rows[0][0];
MemoryStream m = new MemoryStream(b);
Bitmap bb = new Bitmap(m);
pictureBox.Image = bb;
報錯
Bitmap bb = new Bitmap(m);
使用無效的參數
using System;
using System.IO;
using System.Data;
using System.Data.OleDb;class BLOBDemo
{
[STAThread]
static void Main(string[] args)
{
Add("Test","2.jpg");
}public static void Add(string categoryName, string filePath)
{FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);byte [] photo = br.ReadBytes((int)fs.Length);br.Close();
fs.Close();OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=northwind.mdb");
OleDbCommand cmd = new OleDbCommand("INSERT INTO 类别(类别名称, 图片) VALUES (@CategoryName, @Picture)", cn);cmd.Parameters.Add("@CategoryName", OleDbType.VarChar,15).Value = categoryName;
cmd.Parameters.Add("@Picture", OleDbType.Binary, photo.Length).Value = photo;cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
}
using System;
using System.IO;
using System.Data;
using System.Data.OleDb;class BLOBDemo
{
[STAThread]
static void Main(string[] args)
{OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=northwind.mdb");
OleDbCommand cmd = new OleDbCommand("Select 类别ID,图片 FROM 类别 where 类别名称='Test'", cn);FileStream fs;
BinaryWriter bw;//缓冲区大小
const int bufferSize = 100;
byte [] outByte = new byte[bufferSize];
//GetBytes返回的字节数量
long retval = 0;
//BLOB输出的起始位置
long startIndex = 0;string id = "";cn.Open();OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);while(dr.Read())
{
id = dr.GetValue(0).ToString();fs = new FileStream(id + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(fs);startIndex = 0;
retval = dr.GetBytes(1, startIndex, outByte, 0, bufferSize);
while(retval == bufferSize)
{
bw.Write(outByte);
bw.Flush();
startIndex += bufferSize;
retval = dr.GetBytes(1, startIndex, outByte, 0, bufferSize);
}bw.Write(outByte, 0, (int)retval - 1);
bw.Flush();bw.Close();
fs.Close();
}dr.Close();
cn.Close();
}
}
byte[] b = (byte[])ds.Tables[0].Rows[0][0];
MemoryStream m = new MemoryStream(b, true);
m.Read( b, 0, b.Length );
m.Position = 0;
Bitmap bb = new Bitmap(m);
pictureBox.Image = bb;