接触ASP.NET有两个月了,试着看了一下三层的概念,看别人的能理解,但是有点模糊,于是就想把三层应用到自已写
过的一个WEB程序中,虽然程序是没问题的,但是不知道写的对不对,大家帮着看看,希望能提出意见,建议和批评,谢谢!表现层的代码把数据显示到DataGrid中:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
//引用中间层的DT()方法
dg_News.DataSource =hz2.InterService.DT();
dg_News.DataBind ();
}
}中间业务层的代码:
public static DataTable DT()
{
string str_sql="select id,szss,coidlink from 国际商业信息1";
//引用数据层的GetSqlData方法
DataTable dt_sql=Class.DataAccess.GetSqlData (str_sql);
return dt_sql;
}数据层的代码:
public static string ConnectionStrBySql
{
get
{
return System.Configuration.ConfigurationSettings .AppSettings["ConnSql"];
}
}public static DataTable GetSqlData(string sql)
{
DataSet ds = new DataSet();
SqlDataAdapter dsCommand = new SqlDataAdapter();
SqlConnection conn = new SqlConnection(ConnectionStrBySql);
SqlCommand sqlCommand = new SqlCommand(sql,conn);
sqlCommand.CommandType = CommandType.Text;
try
{
conn.Open();
dsCommand.SelectCommand = sqlCommand;
dsCommand.Fill(ds);
}
catch
{
}
finally
{
conn.Close();
dsCommand.Dispose();
}
return ds.Tables[0];
}
过的一个WEB程序中,虽然程序是没问题的,但是不知道写的对不对,大家帮着看看,希望能提出意见,建议和批评,谢谢!表现层的代码把数据显示到DataGrid中:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
//引用中间层的DT()方法
dg_News.DataSource =hz2.InterService.DT();
dg_News.DataBind ();
}
}中间业务层的代码:
public static DataTable DT()
{
string str_sql="select id,szss,coidlink from 国际商业信息1";
//引用数据层的GetSqlData方法
DataTable dt_sql=Class.DataAccess.GetSqlData (str_sql);
return dt_sql;
}数据层的代码:
public static string ConnectionStrBySql
{
get
{
return System.Configuration.ConfigurationSettings .AppSettings["ConnSql"];
}
}public static DataTable GetSqlData(string sql)
{
DataSet ds = new DataSet();
SqlDataAdapter dsCommand = new SqlDataAdapter();
SqlConnection conn = new SqlConnection(ConnectionStrBySql);
SqlCommand sqlCommand = new SqlCommand(sql,conn);
sqlCommand.CommandType = CommandType.Text;
try
{
conn.Open();
dsCommand.SelectCommand = sqlCommand;
dsCommand.Fill(ds);
}
catch
{
}
finally
{
conn.Close();
dsCommand.Dispose();
}
return ds.Tables[0];
}
解决方案 »
- 求asp.net禁止代理服务器访问网站?
- 请大虾相助,关于多人访问网页出错的问题
- 如何在子页面上显示treeview
- Eddie005(♂) №.零零伍 (♂) 请进
- 请问:Visual Web Developer 2005 Express 中不能用 Design 功能的问题?
- 怎么获取客户端的MAC地址
- 我左右移动treeview时,左边的treeview的大小发生了变化,但可显示大小还是没有变。不知如何解决?谢谢!!!
- 如何实现datalist中奇偶行用不同颜色显示?
- 我把AutoGenerateColumns设为false;然后在程序里创建BOUNDCOLUMN来增加列,再用DATAFIELD来绑定,现在想排序,应该怎么弄?
- 一个新手的问题(100分)在线等待
- 关于在DataGrid中进行字段综计的难题!
- 一个很诡异的问题 cookies的有效性的问题
{
string str_sql="select id,szss,coidlink from 国际商业信息1";
//引用数据层的GetSqlData方法
DataTable dt_sql=Class.DataAccess.GetSqlData (str_sql);
return dt_sql;
}
代码复用性...
你这个返回DataTable的方法就只用一次么?
sql语句是表示层传进来的
public static DataTable DT(string str_sql)
{
//引用数据层的GetSqlData方法
DataTable dt_sql=Class.DataAccess.GetSqlData (str_sql);
return dt_sql;
}
------
表示层是不是应该出现SQL语句?
public static string db_sql()
{
return "select id,szss,coidlink from 国际商业信息1";
}public static DataTable GetSqlData(string sql)
{
DataSet ds = new DataSet();
SqlDataAdapter dsCommand = new SqlDataAdapter();
SqlConnection conn = new SqlConnection(ConnectionStrBySql);
SqlCommand sqlCommand = new SqlCommand(sql,conn);
sqlCommand.CommandType = CommandType.Text;
try
{
conn.Open();
dsCommand.SelectCommand = sqlCommand;
dsCommand.Fill(ds);
}
catch
{
}
finally
{
conn.Close();
dsCommand.Dispose();
}
return ds.Tables[0];
}
中间层(写业务逻辑并执行数据层的SQL语句):
public static DataTable DT(string str_sql)
{
DataTable dt_sql=Class.DataAccess.GetSqlData (str_sql);
return dt_sql;
}表现层(把数据表现在控件中):
private void DGBind()
{
dg_News.DataSource =hz2.InterService.DT(hz2.DBManage.db_sql());
dg_News.DataBind ();
}这回中间层的方法DT就可以实现重用了不知道我的想法对不对......
如果是小项目,表示层的东西不多,完全可以将sql语句从表示层传进去,因为一旦有需求变化,表示层要改的地方不多(因为表示层的东西本来就不多),当然,sql语句放在数据层也可以,不过这种情况放在表示层更方便。
对于大一点的项目,我的建议是将sql语句放在数据层,表示层只传页面获取的参数进去..