储存:传入确定图片的唯一序列号id 和图片的保存路径filePath。
private bool AddImg(string id,string filePath)
{
try
{
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(strConn);
OleDbCommand cmd = new OleDbCommand("insert into F_TelImg(id, val) VALUES (@ID, @Img)", cn);
cmd.Parameters.Add("@ID", OleDbType.VarChar).Value = id;
cmd.Parameters.Add("@Img", OleDbType.Binary, photo.Length).Value = photo;
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
catch()
{
return false;
}
return true; ;
}
显示:需要创建独立的显示图片的aspx文件,下面函数是该页面的页面导入事件。通过img.aspx?id=xxx 传入参数来显示对应id的图片。srxljl
protected void Page_Load(object sender, EventArgs e)
{
if (Request["id"] != null && Request["id"] != "")
{
// table.GetDataSet()是封装好的读取数据库数据的函数srxljl
DataSet ds = table.GetDataSet("Img", "select val from Img where id='" + Request["id"] + "'");
while (ds!=null)
{
Response.Clear();
Response.ContentType="image/jpeg"; // 确定图片显示的格式srxljl
Response.BinaryWrite((byte[])ds.Tables["Img"].Rows[0]["val"]);
Response.End();
}
}
}