很早写的代码,你看看吧此实例应用于Com+,所以没有写保存成功提示等等的操作,是用OUT做的,去掉了
保存图片到数据库中
SqlCommand UpdateImageCommand = COMSystem.SelectData.Conn.CreateCommand();
UpdateImageCommand.CommandText = "EXECUTE UpdateImage @员工编号,@照片";
UpdateImageCommand.Parameters.Add("@员工编号",SqlDbType.VarChar,20,"员工编号");
UpdateImageCommand.Parameters["@员工编号"].Value = UserID;
UpdateImageCommand.Parameters.Add("@照片",SqlDbType.Image,0,"照片");
UpdateImageCommand.Parameters["@照片"].Value = Image;
COMSystem.SelectData.mySqlDataAdapter.UpdateCommand = UpdateImageCommand;
try
{
DataRow EditDataRow = SelectData.myDataSet.Tables["员工基本信息表"].Rows.Find(UserID);
EditDataRow["照片"] = Image;
COMSystem.SelectData.Conn.Open();
COMSystem.SelectData.mySqlDataAdapter.Update(COMSystem.SelectData.myDataSet.Tables["员工基本信息表"]);
}
catch(Exception Error)
{
Console.WriteLine(Error.Message);
}
finally
{
COMSystem.SelectData.Conn.Close();
}打开图片并显示
try{
byte[] buffByte = null;
DataRowView drv = (DataRowView)cm.Current;
if(drv["照片"] != System.DBNull.Value)//当数据库中的照片字段不为null时
{
buffByte = ((byte[])drv["照片"]);
System.IO.MemoryStream ms = new System.IO.MemoryStream(buffByte);
//通过流对象建立Bitmap
System.Drawing.Bitmap bmp = new Bitmap(ms);
this.pictureBox1.Image = bmp;
}
else
{
string strBasePath = Application.StartupPath + "\\Photo\\无.bmp";//给定文件夹中的一张通用照片
System.Drawing.Image img = System.Drawing.Image.FromFile(strBasePath);
System.IO.FileStream fs = new System.IO.FileStream(strBasePath,System.IO.FileMode.Open,System.IO.FileAccess.Read);
buffByte = new byte[fs.Length];
fs.Read(buffByte,0,(int)fs.Length);
System.IO.MemoryStream ms = new System.IO.MemoryStream(buffByte);
//通过流对象建立Bitmap
System.Drawing.Bitmap bmp = new Bitmap(ms);
this.pictureBox1.Image = bmp;
}
}
catch(Exception e)
{
this.pictureBox1.Image = null;
MessageBox.Show("..." + e.Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}