微软上写的很详细 http://support.microsoft.com/default.aspx?scid=kb%3Bzh-cn%3B317701将下面的代码插入 Button1 (File to Database) 的 Click 事件过程中。根据需要调整到一个可用示例图像文件的可用路径。此代码可将图像文件从磁盘(使用一个 FileStream 对象)读入 Byte 数组,然后使用一个参数化的 Command 对象将数据插入数据库。try { SqlConnection cn = new SqlConnection(strCn); SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData) VALUES (@BLOBData)", cn); String strBLOBFilePath = @"C:\blue hills.jpg";//Modify this path as needed. //Read jpg into file stream, and from there into Byte array. FileStream fsBLOBFile = new FileStream(strBLOBFilePath,FileMode.Open, FileAccess.Read); Byte[] bytBLOBData = new Byte[fsBLOBFile.Length]; fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length); fsBLOBFile.Close(); //Create parameter for insert command and add to SqlCommand object. SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, bytBLOBData); cmd.Parameters.Add(prm); //Open connection, execute query, and close connection. cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); }catch(Exception ex) {MessageBox.Show(ex.Message);}
7. 将下面的代码插入 Button2 (Database to PictureBox) 的 Click 事件过程。此代码将行从数据库中的 BLOBTest 表检索到一个数据集,复制最新添加的图像到 Byte 数组,然后到 MemoryStream 对象,接着将 MemoryStream 加载到 PictureBox 控件的 Image 属性。try { SqlConnection cn = new SqlConnection(strCn); cn.Open(); //Retrieve BLOB from database into DataSet. SqlCommand cmd = new SqlCommand("SELECT BLOBID, BLOBData FROM BLOBTest ORDER BY BLOBID", cn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds, "BLOBTest"); int c = ds.Tables["BLOBTest"].Rows.Count; if(c>0) { //BLOB is read into Byte array, then used to construct MemoryStream, //then passed to PictureBox. Byte[] byteBLOBData = new Byte[0]; byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]); MemoryStream stmBLOBData = new MemoryStream(byteBLOBData); pictureBox1.Image= Image.FromStream(stmBLOBData); } cn.Close(); } catch(Exception ex) {MessageBox.Show(ex.Message);}
int FileLength = 0;
string FileName=myFile.Value;
HttpPostedFile UpFile=myFile.PostedFile;//获取对由客户端指定的上传文件的访问
FileLength=UpFile.ContentLength;//获取上传文件的字节大小
if(FileLength != 0)
{
string exName=FileName.Substring(FileName.LastIndexOf(".")+1).ToUpper();//截取图片的后缀名
if(exName=="JPG"|| exName=="GIF")//判断图片的类型
{
if(FileLength>102400)//判断图片是否大于100k(根据自己的需要判断大小)
{
Response.Write("<script>alert('对不起,图片大小不能大于100K')</script>");
return false;
}
else
{
string ImageName=DateTime.Now.ToString("yyyyMMddhhmmssfff")+"."+exName;//图片名称设置为保存的时间
Byte[] FileByte = new Byte[FileLength]; //图象文件储存到数组
Stream ObjectStream = UpFile.InputStream;//建立数据流对像,获取一个 Stream 对象,该对象指向一个上载文件,以准备读取该文件的内容。
ObjectStream.Read(FileByte,0,FileLength); //读取图象文件数据
dr["商品图片"] = FileByte;
}
}
else
{
Response.Write("<script>alert('对不起,请选择正确的的图片,必须为JPG或者GIF格式!')</script>");
return false;
}
}
//*********************************************************************************
http://support.microsoft.com/default.aspx?scid=kb%3Bzh-cn%3B317701将下面的代码插入 Button1 (File to Database) 的 Click 事件过程中。根据需要调整到一个可用示例图像文件的可用路径。此代码可将图像文件从磁盘(使用一个 FileStream 对象)读入 Byte 数组,然后使用一个参数化的 Command 对象将数据插入数据库。try
{
SqlConnection cn = new SqlConnection(strCn);
SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData) VALUES (@BLOBData)", cn);
String strBLOBFilePath = @"C:\blue hills.jpg";//Modify this path as needed. //Read jpg into file stream, and from there into Byte array.
FileStream fsBLOBFile = new FileStream(strBLOBFilePath,FileMode.Open, FileAccess.Read);
Byte[] bytBLOBData = new Byte[fsBLOBFile.Length];
fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length);
fsBLOBFile.Close(); //Create parameter for insert command and add to SqlCommand object.
SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false,
0, 0, null, DataRowVersion.Current, bytBLOBData);
cmd.Parameters.Add(prm); //Open connection, execute query, and close connection.
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}catch(Exception ex)
{MessageBox.Show(ex.Message);}
7. 将下面的代码插入 Button2 (Database to PictureBox) 的 Click 事件过程。此代码将行从数据库中的 BLOBTest 表检索到一个数据集,复制最新添加的图像到 Byte 数组,然后到 MemoryStream 对象,接着将 MemoryStream 加载到 PictureBox 控件的 Image 属性。try
{
SqlConnection cn = new SqlConnection(strCn);
cn.Open(); //Retrieve BLOB from database into DataSet.
SqlCommand cmd = new SqlCommand("SELECT BLOBID, BLOBData FROM BLOBTest ORDER BY BLOBID", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "BLOBTest");
int c = ds.Tables["BLOBTest"].Rows.Count; if(c>0)
{ //BLOB is read into Byte array, then used to construct MemoryStream,
//then passed to PictureBox.
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pictureBox1.Image= Image.FromStream(stmBLOBData);
}
cn.Close();
}
catch(Exception ex)
{MessageBox.Show(ex.Message);}