1. UserInfo
#region 用户实体
public class UserInfo
{
#region 属性-----------------------------------------------------------
#region 个人信息
private Int32 m_UserID;//用户ID
private String m_UserName; //用户名
/// <summary>
/// 用户ID
/// </summary>
public Int32 UserID
{
get { return m_UserID; }
set { m_UserID = value; }
}
///<summary>
///用户登录时使用的名称
///</summary>
public String UserName
{
get { return m_UserName; }
set { m_UserName = value; }
}
private Byte[] _Photo;
///<summary>
///照片
///</summary>
public Byte[] Photo
{
get { return _Photo; }
set { _Photo = value; }
}
private string _ImageType;
///<summary>
///照片格式
///</summary>
public String ImageType
{
get { return _ImageType; }
set { _ImageType = value; }
}
#endregion
#endregion
#region 构造函数-------------------------------------------------------
/// <summary>
/// 构造函数
/// </summary>
public UserInfo()
{ }
/// <summary>
/// 设置用户信息
/// </summary>
public UserInfo(DataRow dataRow)
{
m_UserID = (int)dataRow["UserID"];
m_UserName = (string)dataRow["UserName"];
_ImageType = dataRow["ImageType"] == System.DBNull.Value ? string.Empty : dataRow["ImageType"].ToString();
_Photo = dataRow["Photo"] == System.DBNull.Value ? new byte[0] : (byte[])dataRow["Photo"];
}
#endregion
}
#endregion
public class UserInfo
{
#region 属性-----------------------------------------------------------
#region 个人信息
private Int32 m_UserID;//用户ID
private String m_UserName; //用户名
/// <summary>
/// 用户ID
/// </summary>
public Int32 UserID
{
get { return m_UserID; }
set { m_UserID = value; }
}
///<summary>
///用户登录时使用的名称
///</summary>
public String UserName
{
get { return m_UserName; }
set { m_UserName = value; }
}
private Byte[] _Photo;
///<summary>
///照片
///</summary>
public Byte[] Photo
{
get { return _Photo; }
set { _Photo = value; }
}
private string _ImageType;
///<summary>
///照片格式
///</summary>
public String ImageType
{
get { return _ImageType; }
set { _ImageType = value; }
}
#endregion
#endregion
#region 构造函数-------------------------------------------------------
/// <summary>
/// 构造函数
/// </summary>
public UserInfo()
{ }
/// <summary>
/// 设置用户信息
/// </summary>
public UserInfo(DataRow dataRow)
{
m_UserID = (int)dataRow["UserID"];
m_UserName = (string)dataRow["UserName"];
_ImageType = dataRow["ImageType"] == System.DBNull.Value ? string.Empty : dataRow["ImageType"].ToString();
_Photo = dataRow["Photo"] == System.DBNull.Value ? new byte[0] : (byte[])dataRow["Photo"];
}
#endregion
}
#endregion
2. 数据层 Framework.Sys.DAL.User:
/// <summary>
/// 添加照片
/// </summary>
public bool AddPhoto(UserInfo currentUser)
{
string sql = "update Sys_Users set [Photo]=@Photo,[ImageType]=@ImageType where [UserID]=@UserID";
SqlParameter[] parameters ={
new SqlParameter("@UserID", SqlDbType.Int, 4),
new SqlParameter("@Photo", SqlDbType.Image, 16), //注意参数类型
new SqlParameter("@ImageType", SqlDbType.NVarChar, 50)};
parameters[0].Value = currentUser.UserID;
parameters[1].Value = currentUser.Photo;
parameters[2].Value = currentUser.ImageType;
int rowsAffected = 0;
RunSQL(sql, ref rowsAffected, parameters);
return (rowsAffected > 0);
}
/// 添加照片
/// </summary>
public bool AddPhoto(UserInfo currentUser)
{
string sql = "update Sys_Users set [Photo]=@Photo,[ImageType]=@ImageType where [UserID]=@UserID";
SqlParameter[] parameters ={
new SqlParameter("@UserID", SqlDbType.Int, 4),
new SqlParameter("@Photo", SqlDbType.Image, 16), //注意参数类型
new SqlParameter("@ImageType", SqlDbType.NVarChar, 50)};
parameters[0].Value = currentUser.UserID;
parameters[1].Value = currentUser.Photo;
parameters[2].Value = currentUser.ImageType;
int rowsAffected = 0;
RunSQL(sql, ref rowsAffected, parameters);
return (rowsAffected > 0);
}
3. 业务层 Framework.Sys.BLL.User:
/// <summary>
/// 添加照片
/// </summary>
public bool AddPhoto(UserInfo currentUser)
{
return dataUser.AddPhoto(currentUser);
}
/// 添加照片
/// </summary>
public bool AddPhoto(UserInfo currentUser)
{
return dataUser.AddPhoto(currentUser);
}
4. UI层:(UserPhotoAdd.aspx)
(1)前台代码:(注意img 的src 属性设为显示图片的页面)
<img src="UserPhoto.aspx" height="200" width="300" />
<br />
<asp:FileUpload ID="upPhoto" runat="server" />
<asp:Button Text=" 添 加 " runat="server" ID="btnAddPhoto" OnClick="btnAddPhoto_Click" />
<br />
<asp:FileUpload ID="upPhoto" runat="server" />
<asp:Button Text=" 添 加 " runat="server" ID="btnAddPhoto" OnClick="btnAddPhoto_Click" />
(2)后台代码:
Framework.Sys.BLL.User myBiz = new Framework.Sys.BLL.User();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAddPhoto_Click(object sender, EventArgs e)
{
Response.Clear();
int imgSize = upPhoto.PostedFile.ContentLength;//图片的大小
string imgType = upPhoto.PostedFile.ContentType;//图片类型
Stream imgStream = upPhoto.PostedFile.InputStream;//读取图片
Byte[] buff = new Byte[imgSize];
//方法1
imgStream.Read(buff, 0, imgSize);
//方法2
//BinaryReader rd = new BinaryReader(imgStream);
//rd.Read(buff, 0, imgSize);
UserInfo user = new UserInfo();
user.UserID = 9;
user.Photo = buff;//***
user.ImageType = imgType;
if (myBiz.AddPhoto(user))
base.WriteLine("添加成功");
else
base.WriteLine("添加失败");
Response.End();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAddPhoto_Click(object sender, EventArgs e)
{
Response.Clear();
int imgSize = upPhoto.PostedFile.ContentLength;//图片的大小
string imgType = upPhoto.PostedFile.ContentType;//图片类型
Stream imgStream = upPhoto.PostedFile.InputStream;//读取图片
Byte[] buff = new Byte[imgSize];
//方法1
imgStream.Read(buff, 0, imgSize);
//方法2
//BinaryReader rd = new BinaryReader(imgStream);
//rd.Read(buff, 0, imgSize);
UserInfo user = new UserInfo();
user.UserID = 9;
user.Photo = buff;//***
user.ImageType = imgType;
if (myBiz.AddPhoto(user))
base.WriteLine("添加成功");
else
base.WriteLine("添加失败");
Response.End();
}
二、从数据库读取图片
1. 数据层:(Framework.Sys.DAL.User)
public DataRow Retrieve(int userID)
{
string sql = "SELECT Sys_Users.* WHERE UserID ={0}";
sql = string.Format(sql, userID);
using (DataSet users = RunSQL(sql, "Users"))
{
return users.Tables[0].Rows[0];
}
}
{
string sql = "SELECT Sys_Users.* WHERE UserID ={0}";
sql = string.Format(sql, userID);
using (DataSet users = RunSQL(sql, "Users"))
{
return users.Tables[0].Rows[0];
}
}
2. 业务层:(Framework.Sys.BLL.User)
public UserInfo Retrieve(int userID)
{
DataRow userRow = dataUser.Retrieve(userID);
return new UserInfo(userRow);
}
{
DataRow userRow = dataUser.Retrieve(userID);
return new UserInfo(userRow);
}
3. UI层:(UserPhoto.aspx)
(1)前台代码:无
(2)后台代码:
Framework.Sys.BLL.User myBiz = new Framework.Sys.BLL.User();
protected void Page_Load(object sender, EventArgs e)
{
HttpContext.Current.Response.ClearContent();
try
{
UserInfo user = myBiz.Retrieve(9);
Response.ContentType = user.ImageType;
Response.BinaryWrite(user.Photo);
}
catch (Exception exc)
{
throw new Framework.AppException(exc.Message);
}
HttpContext.Current.Response.End();
}
protected void Page_Load(object sender, EventArgs e)
{
HttpContext.Current.Response.ClearContent();
try
{
UserInfo user = myBiz.Retrieve(9);
Response.ContentType = user.ImageType;
Response.BinaryWrite(user.Photo);
}
catch (Exception exc)
{
throw new Framework.AppException(exc.Message);
}
HttpContext.Current.Response.End();
}