微软stringradiobuttonnullexceptiondataset
一般我们都采用分层架构,
先思考业务,要解决什么样的业务问题,再去思考技术实现。
l 正向思维:
界面(UI)—>业务逻辑(BLL)—>数据逻辑(DAL)—>数据实体(Model)—>数据存储(Db)
l 反向思维:
数据存储(Db)—>数据实体(Model)—>数据逻辑(DAL)—>业务逻辑(BLL)—>界面(UI)。
从数据库推导到界面,从界面推导到数据库。搞清楚类之间的关系。数据之间传递关系。
界面(UI):由控件组成。系统与用户交互的窗口。
业务逻辑(BLL):封装业务处理功能。对象的组合,应用。
数据逻辑(DAL):对数据的操作(算法)。(增删改查的多样性),对象函数,方法。
数据实体(Model):操作的数据结构。对象属性
数据存储(Db):数据的存储持久化。(表)。
体会:程序=数据结构+算法,程序=类对象+类对象之间的关系。
我们的很多项目都是基于微软企业库5.0搭建的。具体说一下我们在企业级项目中是怎么应用的。下面是架构图:
1.基于微软企业库的工具类
封装了微软企业库访问数据库常用的最基本的操作。
2.Model层(数据库表映射)
封装了很多属性。对应数据库表c_hbbb_code
代码:
[c-sharp] view plaincopy
odeusing System;
amespace SFS.Entity.Model
// summary
// c_hbbb_code
// summary
[Serializable]
ublic class c_hbbb_code
{
ublic c_hbbb_code()
{ }
rivate int _CodeID = 0;
// <summary>
//
// </summary>
ublic int CodeID
{
et { return _CodeID; }
et { _CodeID = value; }
}
rivate string _CODENO = "";
// <summary>
//
// </summary>
ublic string CODENO
{
et { return _CODENO; }
et { _CODENO = value; }
}
rivate string _CODENAME = "";
// <summary>
//
// </summary>
ublic string CODENAME
{
et { return _CODENAME; }
et { _CODENAME = value; }
}
rivate string _ISMX = "";
// <summary>
//
// </summary>
ublic string ISMX
{
et { return _ISMX; }
et { _ISMX = value; }
}
rivate string _ParentCodeNO = "";
// <summary>
//
// </summary>
ublic string ParentCodeNO
{
et { return _ParentCodeNO; }
et { _ParentCodeNO = value; }
}
rivate string _ZT = "";
// <summary>
//
// </summary>
ublic string ZT
{
et { return _ZT; }
et { _ZT = value; }
}
}
3.Dal层(封装数据库操作增,删,改,查)
[c-sharp] view plaincopy
odenamespace SFS.DAL
ublic class c_hbbb_code
{
rivate SFS.Utility.DbUtility.DbHelper db = null;
// <summary>
// 是否存在该记录
// </summary>
ublic bool Exists(string CodeID)
{
tring strSql = "SELECT count(1) FROM c_hbbb_code WHERE CodeID=@CodeID ";
DbCommand dbCommand = db.GetSqlStringCommand(strSql);
db.AddInParameter(dbCommand, "CodeID", DbType.String, CodeID);
nt result;
bject obj = db.ExecuteScalar(dbCommand);
nt.TryParse(obj.ToString(), out result);
f (result == 1)
{
eturn true;
}
lse
{
eturn false;
}
}
// <summary>
// 增加一条数据
// </summary>
ublic int Add(SFS.Entity.Model.c_hbbb_code model)
{
eturn Add(model, null);
}
// <summary>
// 增加一条数据
// </summary>
ublic int Add(SFS.Entity.Model.c_hbbb_code model, DbTransaction transaction)
{
ry
{
tring strSql = "INSERT INTO c_hbbb_code(CodeID,CODENO,CODENAME,ISMX,ParentCodeNO,ZT)VALUES(@CodeID,@CODENO,@CODENAME,@ISMX,@ParentCodeNO,@ZT)";
DbCommand dbCommand = db.GetSqlStringCommand(strSql);
db.AddInParameter(dbCommand, "CodeID", DbType.Int32, model.CodeID);
db.AddInParameter(dbCommand, "CODENO", DbType.String, model.CODENO);
db.AddInParameter(dbCommand, "CODENAME", DbType.String, model.CODENAME);
db.AddInParameter(dbCommand, "ISMX", DbType.String, model.ISMX);
db.AddInParameter(dbCommand, "ParentCodeNO", DbType.String, model.ParentCodeNO);
db.AddInParameter(dbCommand, "ZT", DbType.String, model.ZT);
f (transaction == null)
{
eturn db.ExecuteNonQuery(dbCommand);
}
lse
{
eturn db.ExecuteNonQuery(dbCommand, transaction);
}
}
atch (AddException addex)
{
addex.DeveloperMessage = "添加c_hbbb_code记录失败!" + addex.Message + "在" + this.ToString() + ".Add(SFS.Entity.Model.c_hbbb_code model)中";
addex.UserMessage = "添加c_hbbb_code记录失败!";
hrow addex;
}
}
// <summary>
// 更新一条数据
// </summary>
ublic int Update(SFS.Entity.Model.c_hbbb_code model)
{
eturn Update(model, null);
}
// <summary>
// 更新一条数据
// </summary>
ublic int Update(SFS.Entity.Model.c_hbbb_code model, DbTransaction transaction)
{
ry
{
tring strSql = "UPDATE c_hbbb_code SET CODENO=@CODENO,CODENAME=@CODENAME,ISMX=@ISMX,ParentCodeNO=@ParentCodeNO,ZT=@ZT WHERE CodeID=@CodeID ";
DbCommand dbCommand = db.GetSqlStringCommand(strSql);
db.AddInParameter(dbCommand, "CodeID", DbType.Int32, model.CodeID);
db.AddInParameter(dbCommand, "CODENO", DbType.String, model.CODENO);
db.AddInParameter(dbCommand, "CODENAME", DbType.String, model.CODENAME);
db.AddInParameter(dbCommand, "ISMX", DbType.String, model.ISMX);
db.AddInParameter(dbCommand, "ParentCodeNO", DbType.String, model.ParentCodeNO);
db.AddInParameter(dbCommand, "ZT", DbType.String, model.ZT);
f (transaction == null)
{
eturn db.ExecuteNonQuery(dbCommand);
}
lse
{
eturn db.ExecuteNonQuery(dbCommand, transaction);
}
}
atch (UpdateException updateex)
{
updateex.DeveloperMessage = "修改c_hbbb_code记录失败!" + updateex.Message + "在" + this.ToString() + ".Update(SFS.Entity.Model.c_hbbb_code model)中";
updateex.UserMessage = "修改c_hbbb_code记录失败!";
hrow updateex;
}
}
// <summary>
// 删除一条数据
// </summary>
ublic int Delete(string CodeID)
{
eturn Delete(CodeID, null);
}
// <summary>
// 删除一条数据
// </summary>
ublic int Delete(string CodeID, DbTransaction transaction)
{
ry
{
f (string.IsNullOrEmpty(CodeID))
{
NOIsNullOrEmptyException exception = new NOIsNullOrEmptyException();
exception.DeveloperMessage = "CodeID编号不能为空!在" + this.ToString() + ".Delete(string CodeID)中";
exception.UserMessage = "CodeID编号不能为空!";
hrow exception;
}
tring strSql = "DELETE FROM c_hbbb_code WHERE CodeID=@CodeID ";
DbCommand dbCommand = db.GetSqlStringCommand(strSql);
db.AddInParameter(dbCommand, "CodeID", DbType.String, CodeID);
f (transaction == null)
{
eturn db.ExecuteNonQuery(dbCommand);
}
lse
{
eturn db.ExecuteNonQuery(dbCommand, transaction);
}
}
atch (DeleteException deleteex)
{
deleteex.DeveloperMessage = "删除c_hbbb_code记录失败!" + deleteex.Message + "在" + this.ToString() + ".Delete(string CodeID)中";
deleteex.UserMessage = "删除c_hbbb_code记录失败!";
hrow deleteex;
}
}
// <summary>
// 得到一个对象实体
// </summary>
ublic SFS.Entity.Model.c_hbbb_code GetModel(string CodeID)
{
f (string.IsNullOrEmpty(CodeID))
{
NOIsNullOrEmptyException exception = new NOIsNullOrEmptyException();
exception.DeveloperMessage = "CodeID编号不能为空!在" + this.ToString() + ".GetModel(string CodeID)中";
exception.UserMessage = "CodeID编号不能为空!";
hrow exception;
}
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT CodeID,CODENO,CODENAME,ISMX,ParentCodeNO,ZT ");
strSql.Append(" FROM c_hbbb_code ");
strSql.Append(" WHERE CodeID=@CodeID ");
DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
db.AddInParameter(dbCommand, "CodeID", DbType.String, CodeID);
SFS.Entity.Model.c_hbbb_code model = null;
sing (IDataReader dataReader = db.ExecuteReader(dbCommand))
{
f (dataReader.Read())
{
model = ReaderBind(dataReader);
}
}
eturn model;
}
// <summary>
// 获得数据列表
// </summary>
ublic DataSet GetDataSet(string strWhere)
{
ry
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT CodeID,CODENO,CODENAME,ISMX,ParentCodeNO,ZT ");
strSql.Append(" FROM c_hbbb_code ");
f (strWhere.Trim() != "")
{
strSql.Append(" WHERE " + strWhere);
}
eturn db.ExecuteDataSet(CommandType.Text, strSql.ToString());
}
atch (SelectException selectex)
{
selectex.DeveloperMessage = "查询记录失败!" + selectex.Message + "在" + this.ToString() + ".GetDataSet(string strWhere)中";
selectex.UserMessage = "查询记录失败!";
hrow selectex;
}
}
// <summary>
// 获得数据列表(比DataSet效率高,推荐使用)
// </summary>
ublic List<SFS.Entity.Model.c_hbbb_code> GetList(string strWhere)
{
ry
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT CodeID,CODENO,CODENAME,ISMX,ParentCodeNO,ZT ");
strSql.Append(" FROM c_hbbb_code ");
f (strWhere.Trim() != "")
{
strSql.Append(" WHERE " + strWhere);
}
List<SFS.Entity.Model.c_hbbb_code> list = new List<SFS.Entity.Model.c_hbbb_code>();
sing (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
hile (dataReader.Read())
{
list.Add(ReaderBind(dataReader));
}
}
eturn list;
}
atch (SelectException selectex)
{
selectex.DeveloperMessage = "查询记录失败!" + selectex.Message + "在" + this.ToString() + ".GetList(string strWhere)中";
selectex.UserMessage = "查询记录失败!";
hrow selectex;
}
}
// <summary>
// 对象实体绑定数据
// </summary>
ublic SFS.Entity.Model.c_hbbb_code ReaderBind(IDataReader dataReader)
{
SFS.Entity.Model.c_hbbb_code model = new SFS.Entity.Model.c_hbbb_code();
bject obj;
obj = dataReader["CodeID"];
f (obj != null && obj != DBNull.Value)
{
model.CodeID = (int)obj;
}
obj = dataReader["CODENO"];
f (obj != null && obj != DBNull.Value)
{
model.CODENO = obj.ToString();
}
obj = dataReader["CODENAME"];
f (obj != null && obj != DBNull.Value)
{
model.CODENAME = obj.ToString();
}
obj = dataReader["ISMX"];
f (obj != null && obj != DBNull.Value)
{
model.ISMX = obj.ToString();
}
obj = dataReader["ParentCodeNO"];
f (obj != null && obj != DBNull.Value)
{
model.ParentCodeNO = obj.ToString();
}
obj = dataReader["ZT"];
f (obj != null && obj != DBNull.Value)
{
model.ZT = obj.ToString();
}
eturn model;
}
}
4.Bll层(跟具体业务有关的逻辑)
[c-sharp] view plaincopy
odenamespace SFS.BLL
// <summary>
// 业务逻辑类bllADDRESS 的摘要说明。
// </summary>
ublic class c_hbbb_code
{
rivate readonly SFS.DAL.c_hbbb_code dal = new SFS.DAL.c_hbbb_code();
ublic c_hbbb_code()
{ }
#region 成员方法
// <summary>
// 是否存在该记录
// </summary>
ublic bool Exists(string CodeID)
{
ry
{
eturn dal.Exists(CodeID);
}
atch (ExceptionBase ex)
{
SFS.Utility.ExceptionUtility.ExceptionHelper.HandleException(ex, Policy.BLLPolicy);
eturn false;
}
}
// <summary>
// 增加一条数据
// </summary>
ublic int Add(SFS.Entity.Model.c_hbbb_code model)
{
ry
{
eturn dal.Add(model);
}
atch (ExceptionBase ex)
{
SFS.Utility.ExceptionUtility.ExceptionHelper.HandleException(ex, Policy.BLLPolicy);
eturn 0;
}
}
// <summary>
// 更新一条数据
// </summary>
ublic int Update(SFS.Entity.Model.c_hbbb_code model)
{
ry
{
eturn dal.Update(model);
}
atch (ExceptionBase ex)
{
SFS.Utility.ExceptionUtility.ExceptionHelper.HandleException(ex, Policy.BLLPolicy);
eturn 0;
}
}
// <summary>
// 删除一条数据
// </summary>
ublic int Delete(string CodeID)
{
ry
{
eturn dal.Delete(CodeID);
}
atch (ExceptionBase ex)
{
SFS.Utility.ExceptionUtility.ExceptionHelper.HandleException(ex, Policy.BLLPolicy);
eturn 0;
}
}
// <summary>
// 得到一个对象实体
// </summary>
ublic SFS.Entity.Model.c_hbbb_code GetModel(string CodeID)
{
ry
{
eturn dal.GetModel(CodeID);
}
atch (ExceptionBase ex)
{
SFS.Utility.ExceptionUtility.ExceptionHelper.HandleException(ex, Policy.BLLPolicy);
eturn null;
}
}
// <summary>
// 获得数据列表
// </summary>
ublic DataSet GetDataSet(string strWhere)
{
ry
{
eturn dal.GetDataSet(strWhere);
}
atch (ExceptionBase ex)
{
SFS.Utility.ExceptionUtility.ExceptionHelper.HandleException(ex, Policy.BLLPolicy);
eturn null;
}
}
// <summary>
// <summary>
// 获得数据列表
// </summary>
ublic List<SFS.Entity.Model.c_hbbb_code> GetList(string strWhere)
{
ry
{
eturn dal.GetList(strWhere);
}
atch (ExceptionBase ex)
{
SFS.Utility.ExceptionUtility.ExceptionHelper.HandleException(ex, Policy.BLLPolicy);
eturn null;
}
}
#endregion 成员方法
}
5.UI层(用户界面)
常用界面功能代码
加载数据
private void LoadData(string SJJG)
{
SFS.Bll.c_hbbb_code objC_SUBCOMPANY=new SFS.Bll.c_hbbb_code();
this.GridView1.DataSource = objC_SUBCOMPANY.GetList(“”);
this.GridView1.DataBind();
}
分页
protected voidGridView1_PageIndexChanging(objectsender,GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
LoadData(“”);
}
绑定时自动加入序号
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButton rb = (RadioButton)e.Row.FindControl("rbSelect");
rb.Attributes.Add("onclick", "CheckOne(this);");
e.Row.Cells[1].Text = Convert.ToString(e.Row.RowIndex + 1);
}
}
添加
protected void btnAdd_Click(object sender, EventArgs e)
{
Response.Redirect("Add.aspx?JGDM=" + SJJG);
}
删除
protected void btnUpdate_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
RadioButton radio = row.FindControl("rbSelect") as RadioButton;
if (radio != null && radio.Checked)
{
Label lblID = (Label)row.FindControl("lblJGDM");
Response.Redirect("Delete.aspx?JGDM=" + SJJG);
break;
}
}
}
修改
protected void btnUpdate_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
RadioButton radio = row.FindControl("rbSelect") as RadioButton;
if (radio != null && radio.Checked)
{
Label lblID = (Label)row.FindControl("lblJGDM");
Response.Redirect("Update.aspx?JGDM=" + SJJG);
break;
}
}
}
一般写界面代码逻辑顺序