asp.为什么不在web窗体中处理业务而要创建一个业务逻辑类,怎样把逻辑...
发布网友
发布时间:2024-10-24 12:59
我来回答
共2个回答
热心网友
时间:2024-10-29 15:43
1.
对应数据库实体类
[Serializable]
public class mytable
{
private String _pkid = "";
public String pkid
{
get { return _pkid; }
set { _pkid = value; }
}
private String _a = "";
public String a
{
get { return _a; }
set { _a = value; }
}
}
2.操作类:可以将此类放在业务逻辑层,(如果还要细化,可以将此层放在数据库实体操作层)
public class mytableDAO
{
private String _ConnectionString;
public mytableDAO(String ConnectionString)
{
this._ConnectionString = ConnectionString;
}
public int New(Entities.MsgRecord obj)
{
String sql = "insert into mytable(pkid,a) values(@pkid,@a)";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@a", obj.a);
cmd.Parameters.AddWithValue("@pkid",
String.Empty.Equals(obj.pkid) ? System.Guid.NewGuid().ToString() : obj.pkid);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
public int Update(Entities.mytable obj)
{
String sql = "Update mytable Set a=@a Where pkid=@ObjectID";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@a", obj.a);
cmd.Parameters.AddWithValue("@pkid", obj.pkid);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
public int Del(Entities.mytable obj)
{
String sql = "delete from mytable Where pkid=@ObjectID";
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@ObjectID", obj.pkid);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
public int Del(String where)
{
String sql = String.Format("delete from mytable Where {0}", where.ToLower().Replace("update", "").Replace("delete", ""));
SqlConnection cn = new SqlConnection(this._ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
public DataTable Query(String where)
{
String sql = String.Format("select * from mytable Where {0}", where.ToLower().Replace("update", "").Replace("delete", "").Replace("insert", "").Replace(";", "").Replace("--", "").Replace("exec", ""));
try
{
SqlDataAdapter da = new SqlDataAdapter(sql, new SqlConnection(this._ConnectionString));
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
catch
{
return null;
}
}
}
3.界面层,即.在ui中调用:数据库操作类,如(mytableDao dao=new mytableDao(conn);)
3.1 新增:
private void NewData()
{
String conn=System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString
mytableDao dao=new mytableDao(conn);
mytable obj=new mytable();
obj.a=this.txtbox_a.Text;
obj.b=this.txtbox_b.Text;
obj.c=this.txtbox_c.Text;
dao.New(obj);
}
3.2 取数据绑定GridView
private void LoadData_InitGridView()
{
String conn=System.Configuration.ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString
mytableDao dao=new mytableDao(conn);
Table dt=dao.Query(String.Format(" a ='{0}'",this.txtbox_a.Text));
//gridview
this.GridView1.DataSource=dt;
this.GridView1.DataBind();
//textbox control
this.textbox_a.Text=dt.Rows[0][0].ToString();
}
热心网友
时间:2024-10-29 15:45
怎样把逻辑单元连接到用户界面:可以理解是界面层调用业务逻辑层相关业务,简单的例子你可以实例化业务逻辑类,然后在界面调用这个对象的相关方法就可以了。
这是一个分层思想,想知道为什么可以从最经典的三层架构学起