两种方式
一:以二进制数据存在数据库中,但若照片较多,数据库体积太大。
数据库类型:image
在ADO.NET中用byte[]操作。
仅仅是复制杨老师的代码过来,供以后参考,还没有测试。
二:数据库存储路径,然后在文件夹中存取图片。我需要这个技术,哎,老师不讲。按我的理解,找个时间自己做看看。
1:在Model添加一个字段
Publicbyte[]Photo
{
get;
set;
}
2:c#代码
//选择添加照片
OpenFileDialogofdPhoto=newOpenFileDialog();
ofdPhoto.Filter="jpg图片|*.jpg|png图ª?片?|*.png";
if(ofdPhoto.ShowDialog()==true)
{
stringfilename=ofdPhoto.FileName;
Employeeemployee=(Employee)gridEmployee.DataContext;//应该是与Grid名为gridEmployee的标签进行数据绑定的
employee.Photo=File.ReadAllBytes(filename);//读取文件的二进制数据
imgPhoto.Source=newBitmapImage(newUri(filename));//imgPhoto是image控件
}
//接着是在DAL文件中针对新增的字段进行代码添加
//显示的话,能数据绑定,但很麻烦。而是使用下面代码调出来
privatevoidShowImg(byte[]imgBytes)
{
MemoryStreamstream=newMemoryStream(imgBytes);
BitmapImagebmpImg=newBitmapImage();
bmpImg.BeginInit();
bmpImg.StreamSource=stream;
bmpImg.EndInit();
imgPhoto.Source=bmpImg;
}