| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1041 人关注过本帖
标题:三层结构学习问题一:这算不算一个真正的三层结构?
只看楼主 加入收藏
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
结帖率:100%
收藏
 问题点数:0 回复次数:7 
三层结构学习问题一:这算不算一个真正的三层结构?

这算不算一个真正的三层结构?
*.aspx.cs 代码:
using GongChengGuanLiXiTong.BLL;
protected void Button3_Click(object sender, EventArgs e)//新增合同信息
{
GongChengGuanLiHeTongXinXi glht = new GongChengGuanLiHeTongXinXi();
glht.HeTongBianHao = TextBox4.Text.ToString().Trim();
glht.GongChengMingCheng = TextBox5.Text.ToString().Trim();
glht.JiaFang = TextBox6.Text.ToString().Trim();
glht.JiaFangID = Session["GLXM_Id"].ToString();
glht.JiaFangID = "1/2";
glht.YiFang = TextBox7.Text.ToString().Trim();
glht.GongChengZaoJia = float.Parse(TextBox9.Text.ToString().Trim());
glht.GuanLiChouJin = float.Parse(TextBox15.Text.ToString());
//glht.QianYueRiQi = DateTime.Parse(TextBox8.Text.ToString());
glht.QianYueRiQi = DateTime.Parse("2007-7-7");
glht.FuKuanFangShi = TextBox1.Text.ToString().Trim();
glht.BeiZhu = TextBox2.Text.ToString().Trim();
GongChengGuanLiHeTongXinXi.Insert(glht);
//关闭子窗口并返回父窗口并模拟单击父窗口的Button13按扭引发其click事件,实现子窗口提交数据后即时异步刷新父窗口中的GridView控件
Page.ClientScript.RegisterStartupScript(this.GetType(), "Test1", "<script>close();opener.document.all('Button13').click();</script>");
}

搜索更多相关主题的帖子: 结构 glht ToString Trim 合同 
2007-08-13 09:10
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
收藏
得分:0 

BLL 中的类文件--GongChengGuanLiHeTongXinXi.cs

using GongChengGuanLiXiTong.DAL;

namespace GongChengGuanLiXiTong.BLL
{
/// <summary>
/// GongChengGuanLiHeTongXinXi 的摘要说明
/// </summary>
public class GongChengGuanLiHeTongXinXi
{
#region Member Variables (定义私有字段)

private int intGuanLiHeTongID;
private string strHeTongBianHao;
private string strGongChengMingCheng;
private string strJiaFang;
private string strJiaFangID;
private string strYiFang;
private float floGongChengZaoJia;
private float floGuanLiChouJin;
private DateTime dtQianYueRiQi;
private string strFuKuanFangShi;
private string strBeiZhu;

#endregion

public GongChengGuanLiHeTongXinXi()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region Public Properties (定义属性)

public int GuanLiHeTongID
{
get
{
return intGuanLiHeTongID;
}
set
{
intGuanLiHeTongID = value;
}
}

public string HeTongBianHao
{
get
{
return strHeTongBianHao;
}
set
{
strHeTongBianHao = value;
}
}

public string GongChengMingCheng
{
get
{
return strGongChengMingCheng;
}
set
{
strGongChengMingCheng = value;
}
}

public string JiaFang
{
get
{
return strJiaFang;
}
set
{
strJiaFang = value;
}
}

public string JiaFangID
{
get
{
return strJiaFangID;
}
set
{
strJiaFangID = value;
}
}

public string YiFang
{
get
{
return strYiFang;
}
set
{
strYiFang = value;
}
}

public float GongChengZaoJia
{
get
{
return floGongChengZaoJia;
}
set
{
floGongChengZaoJia = value;
}
}

public float GuanLiChouJin
{
get
{
return floGuanLiChouJin;
}
set
{
floGuanLiChouJin = value;
}

}

public DateTime QianYueRiQi
{
get
{
return dtQianYueRiQi;
}
set
{
dtQianYueRiQi = value;
}
}

public string FuKuanFangShi
{
get
{
return strFuKuanFangShi;
}
set
{
strFuKuanFangShi = value;
}
}

public string BeiZhu
{
get
{
return strBeiZhu;
}
set
{
strBeiZhu = value;
}
}

#endregion

#region Public Static Methods (定义方法)

public static GongChengGuanLiHeTongXinXi GetResume(string username)
{
DBAccess db = new DBAccess();
db.AddParameter("@sUserName", username);
SqlDataReader dr = (SqlDataReader)db.ExecuteReader("JobsDb_Resumes_SelectForUser");
if (dr.HasRows)
{
GongChengGuanLiHeTongXinXi r = new GongChengGuanLiHeTongXinXi();
while (dr.Read())
{
r.GuanLiHeTongID = dr.GetInt32(dr.GetOrdinal("glht_id"));
r.HeTongBianHao = dr.GetString(dr.GetOrdinal("htph"));
r.GongChengMingCheng = dr.GetString(dr.GetOrdinal("gcmc"));
r.JiaFang = dr.GetString(dr.GetOrdinal("jiafang"));
r.JiaFangID = dr.GetString(dr.GetOrdinal("jiafang_id"));
r.YiFang = dr.GetString(dr.GetOrdinal("yifang"));
r.GongChengZaoJia = float.Parse(dr.GetString(dr.GetOrdinal("gczj")));
r.GuanLiChouJin = float.Parse(dr.GetString(dr.GetOrdinal("glfzj")));
r.QianYueRiQi = dr.GetDateTime(dr.GetOrdinal("qdrq"));
r.FuKuanFangShi = dr.GetString(dr.GetOrdinal("fktj"));
r.BeiZhu = dr.GetString(dr.GetOrdinal("bz"));
}
dr.Close();
return r;
}
else
{
dr.Close();
GongChengGuanLiHeTongXinXi r = new GongChengGuanLiHeTongXinXi();
r.GuanLiHeTongID = -1;
return r;
}

}

public static GongChengGuanLiHeTongXinXi GetResume(int resumeid)
{
DBAccess db = new DBAccess();
db.AddParameter("@glht_id", resumeid);
string Sql="select * from GuanLiHeTongXinXi where glht_id = " + resumeid;
SqlDataReader dr = (SqlDataReader)db.ExecuteReader(Sql);
if (dr.HasRows)
{
GongChengGuanLiHeTongXinXi r = new GongChengGuanLiHeTongXinXi();
while (dr.Read())
{
r.GuanLiHeTongID = dr.GetInt32(dr.GetOrdinal("glht_id"));
//r.HeTongBianHao = dr.GetString(dr.GetOrdinal("htph"));
r.HeTongBianHao = (string)dr["htph"];
//r.GongChengMingCheng = dr.GetString(dr.GetOrdinal("gcmc"));
r.GongChengMingCheng = (string)dr["gcmc"];
//r.JiaFang = dr.GetString(dr.GetOrdinal("jiafang"));
r.JiaFang = (string)dr["jiafang"];
r.JiaFangID = dr.GetString(dr.GetOrdinal("jiafang_id"));
r.YiFang = dr.GetString(dr.GetOrdinal("yifang"));
r.GongChengZaoJia = Convert.ToSingle(dr.GetDouble(dr.GetOrdinal("gczj")));
r.GuanLiChouJin = Convert.ToSingle(dr.GetDouble(dr.GetOrdinal("glfzj")));
r.QianYueRiQi = dr.GetDateTime(dr.GetOrdinal("qdrq"));
r.FuKuanFangShi = dr.GetString(dr.GetOrdinal("fktj"));
r.BeiZhu = dr.GetString(dr.GetOrdinal("bz"));
}
dr.Close();
return r;
}
else
{
dr.Close();
return new GongChengGuanLiHeTongXinXi();
}

}

public static int Insert(GongChengGuanLiHeTongXinXi r)
{
DBAccess db = new DBAccess();
db.AddParameter("@htph", r.HeTongBianHao);
db.AddParameter("@gcmc", r.GongChengMingCheng);
db.AddParameter("@jiafang", r.JiaFang);
db.AddParameter("@jiafang_id", r.JiaFangID);
db.AddParameter("@yifang", r.YiFang);
db.AddParameter("@gczj", r.GongChengZaoJia);
db.AddParameter("@glfzj", r.GuanLiChouJin);
db.AddParameter("@qdrq", r.QianYueRiQi);
db.AddParameter("@fktj", r.FuKuanFangShi);
db.AddParameter("@bz", r.BeiZhu);
SqlParameter p = new SqlParameter("@glht_id", SqlDbType.Int);
p.Direction = ParameterDirection.Output;
db.AddParameter(p);
string MySql = "insert into GuanLiHeTongXinXi(htph,gcmc,jiafang,jiafang_id,yifang,gczj,glfzj,qdrq,fktj,bz)"
+ " values (@htph,@gcmc,@jiafang,@jiafang_id,@yifang,@gczj,@glfzj,@qdrq,@fktj,@bz)";
int retval = db.ExecuteNonQuery(MySql);
//if (retval <= 0)
//{
// return -1;
//}
//else
//{
// return (int)p.Value;
//}
return retval;

}

public static int Update(GongChengGuanLiHeTongXinXi r)
{
DBAccess db = new DBAccess();
db.AddParameter("@htph", r.HeTongBianHao);
db.AddParameter("@gcmc", r.GongChengMingCheng);
db.AddParameter("@jiafang", r.JiaFang);
db.AddParameter("@jiafang_id", r.JiaFangID);
db.AddParameter("@yifang", r.YiFang);
db.AddParameter("@gczj", r.GongChengZaoJia);
db.AddParameter("@glfzj", r.GuanLiChouJin);
db.AddParameter("@qdrq", r.QianYueRiQi);
db.AddParameter("@fktj", r.FuKuanFangShi);
db.AddParameter("@bz", r.BeiZhu);
string sql = "update GuanLiHeTongXinXi set htph=@htph,gcmc=@gcmc,jiafang=@jiafang,jiafang_id=@jiafang_id,yifang=@yifang,gczj=@gczj,glfzj=@glfzj,qdrq=@qdrq,fktj=@fktj,bz=@bz where glht_id =" + r.GuanLiHeTongID;
return db.ExecuteNonQuery(sql);

}

public static int Delete(int ResumeID)
{
DBAccess db = new DBAccess();
db.AddParameter("@glht_id", ResumeID);
String mySql = "delete from GuanLiHeTongXinXi where glht_id = " + ResumeID;
return db.ExecuteNonQuery(mySql);
}


#endregion
}
}

[此贴子已经被作者于2007-8-13 9:41:51编辑过]


我是2.0超级菜鸟,请多多教导!
2007-08-13 09:12
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
收藏
得分:0 

DAL文件中的类:

public class DBAccess
{
private IDbCommand cmd = new SqlCommand();
private bool handleErrors = false;
private string strLastError = "";
public DBAccess()
{
SqlConnection conn1 = new SqlConnection("data source=(local);database=****;user=****; password=****");
cmd.Connection = conn1;
}
public void AddParameter(string paramname, object paramvalue)
{
SqlParameter param = new SqlParameter(paramname, paramvalue);
cmd.Parameters.Add(param);
}

public void AddParameter(IDataParameter param)
{
cmd.Parameters.Add(param);
}

public int ExecuteNonQuery()
{
int i = -1;
try
{
this.Open();
i = cmd.ExecuteNonQuery();
this.Close();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}

return i;
}
public int ExecuteNonQuery(string commandtext)
{
int i = -1;
try
{
cmd.CommandText = commandtext;
i = this.ExecuteNonQuery();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}

return i;
}
public IDataReader ExecuteReader()
{
IDataReader reader = null;
try
{
this.Open();
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return reader;
}

public IDataReader ExecuteReader(string commandtext)
{
IDataReader reader = null;
try
{
cmd.CommandText = commandtext;
reader = this.ExecuteReader();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}

return reader;
}
private void Open()
{
cmd.Connection.Open();
}

private void Close()
{
cmd.Connection.Close();
}

}
}


我是2.0超级菜鸟,请多多教导!
2007-08-13 09:13
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
收藏
得分:0 

这是我看了一个例子后依葫芦画样套用出来,很多地方还不是很明白,请大家帮忙理清思路。
我的思路是:当在页面向数据库提交一个合同的基本信息时,在表现层先实例化一个“合同类”--业务逻辑层BLL文件夹 中的类文件--GongChengGuanLiHeTongXinXi.cs 中的class: GongChengGuanLiHeTongXinXi 。该GongChengGuanLiHeTongXinXi类中的各个属性对应数据库的各个字段。然后将用户在页面中各个TextBox上填写的内容对应 实例化GongChengGuanLiHeTongXinXi类的对象“glht”(GongChengGuanLiHeTongXinXi glht = new GongChengGuanLiHeTongXinXi();)的属性赋值。接着应用业务逻辑层的GongChengGuanLiHeTongXinXi类的Insert方法,将对象“glht”中各个属性值作为数据库相应字段的参数。最后应用数据访问层DAL中的DBAccess类的ExecuteNonQuery方法执行对数据库的写入数据操作。
想法太生硬,请大家指正错误,谢谢!

[此贴子已经被作者于2007-8-13 9:40:55编辑过]


我是2.0超级菜鸟,请多多教导!
2007-08-13 09:27
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
收藏
得分:0 
三层结构与我原先没有采用三层结构的代码比较,感觉代码多一段:
我原来的代码是将页面中各个TextBox控件的值直接赋值给DataTable中 DataRow 中的各个字段,然后更新DataSet中的DataTable的数据更新到数据库中。
而采用三层结构后,先实例化一个合同类得到一个合同对象,然后根据页面中各个TextBox控件的值给相应的合同对象的属性赋值,然后再将同对象的属性值作为参数传给SqlCommand。感觉上较非三层结构多了一个步骤就是:不是直接将页面中各个TextBox控件的值赋值给数据库的字段。
感觉采用三层结构后用了一个“合同类”才是真正是面向对象的编程。
下面是非三层结构的代码:
protected void Button3_Click(object sender, EventArgs e)//新增合同信息
{
SqlConnection conn1 = new SqlConnection("data source=(local);database=****;user=****; password=****"); //创建连接对象,并指明要连接的数据库名称、用户名、用户密码
SqlDataAdapter da1 = new SqlDataAdapter(); //创建数据适配器
DataSet ds1 = new DataSet(); //创建DataSet(数据集)
//适配器对象的查询命令
SqlCommand cmdSel = new SqlCommand("select * from GuanLiHeTongXinXi", conn1); //创建DataAdapter(数据适配器)
da1.SelectCommand = cmdSel;
string MySql = "insert into GuanLiHeTongXinXi(htph,gcmc,jiafang,jiafang_id,yifang,gczj,glfzj,qdrq,fktj,bz)"
+ " values (@htph,@gcmc,@jiafang,@jiafang_id,@yifang,@gczj,@glfzj,@qdrq,@fktj,@bz)";
//适配器对象的添加命令
SqlCommand cmdInsert = new SqlCommand(MySql, conn1);
//cmdInsert.Parameters.Add("@htID", SqlDbType.Int, 4, "ID");
cmdInsert.Parameters.Add("@htph", SqlDbType.NVarChar, 100, "htph");
cmdInsert.Parameters.Add("@gcmc", SqlDbType.NVarChar, 100, "gcmc");
cmdInsert.Parameters.Add("@jiafang", SqlDbType.NVarChar, 100, "jiafang");
cmdInsert.Parameters.Add("@jiafang_id", SqlDbType.NVarChar, 50, "jiafang_id");
cmdInsert.Parameters.Add("@yifang", SqlDbType.NVarChar, 100, "yifang");
cmdInsert.Parameters.Add("@gczj", SqlDbType.Float, 8, "gczj");
cmdInsert.Parameters.Add("@glfzj", SqlDbType.Float, 8, "glfzj");
cmdInsert.Parameters.Add("@qdrq", SqlDbType.DateTime, 8, "qdrq");
cmdInsert.Parameters.Add("@fktj", SqlDbType.NVarChar, 512, "fktj");
cmdInsert.Parameters.Add("@bz", SqlDbType.NVarChar, 512, "bz");
da1.InsertCommand = cmdInsert;
da1.Fill(ds1, "GuanLiHeTongXinXi"); //使用数据适配器填充数据集
//添加数据
DataRow addrow = ds1.Tables[0].NewRow();
addrow["htph"] = TextBox4.Text.ToString().Trim();
addrow["gcmc"] = TextBox5.Text.ToString().Trim();
addrow["jiafang"] = TextBox6.Text.ToString().Trim();
addrow["jiafang_id"] = Session["GLXM_Id"].ToString();
addrow["yifang"] = TextBox7.Text.ToString().Trim();
addrow["gczj"] = TextBox9.Text.ToString();
addrow["glfzj"] = TextBox15.Text.ToString();
if (TextBox8.Text.ToString().Trim() == "")
{ addrow["qdrq"] = System.DBNull.Value; }
else
{ addrow["qdrq"] = TextBox8.Text.ToString(); }
addrow["fktj"] = TextBox1.Text.ToString().Trim();
addrow["bz"] = TextBox2.Text.ToString().Trim();
ds1.Tables[0].Rows.Add(addrow);
da1.Update(ds1, "GuanLiHeTongXinXi");

}

我是2.0超级菜鸟,请多多教导!
2007-08-13 09:57
cyyu_ryh
Rank: 8Rank: 8
等 级:贵宾
威 望:45
帖 子:1899
专家分:176
注 册:2006-10-21
收藏
得分:0 

用架构,主要是便于更新数据和代码的复用性
把业务和数据分开便于修改
如果笼统的写不利于软件的更新,而且代码比较涌余


有事无事都密我. MSN: cyyu_ryh@hotmail.co.jp E-mail: cyyu_ryh@
2007-08-13 11:06
HankStar
Rank: 1
等 级:新手上路
帖 子:230
专家分:0
注 册:2006-10-4
收藏
得分:0 

用断点过一遍就知道是怎么走了


2007-08-15 01:07
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
收藏
得分:0 

再看了其它相关资料,讲到 SQL 语句应该放在DAL层,但微软的这个例子是将SQL放在BLL层,在BLL层向DAL层传送一个 存诸过程 让 DAL层执行数据库操作。


我是2.0超级菜鸟,请多多教导!
2007-08-17 08:34
快速回复:三层结构学习问题一:这算不算一个真正的三层结构?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033654 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved