数据绑定表面层
protected void Page_Load( object sender, EventArgs e) ... { if(!this.IsPostBack) ...{ newsbind(); } } void newsbind() ... { DataView dv = CXF.select(100,tableID);//你看这里的dv数据引到了一个封装的类的一个方法并传了2个参数; this.DataGrid1.DataSource = dv; this.DataGrid1.DataBind();//数据绑定到控件 }业务层:下面我们去看看CXF类的select方法是干什么的你要是觉的业务层发的不好,
public static DataView select( int count, string tableID) ... ... { string strselect = ""; switch (tableID)//这里接收表名的参数 ......{ case "hzhb": strselect = string.Format(@"SELECT TOP {0} * FROM [hzhb] ORDER BY [hzhb_id] DESC", count);//{0}是表达式,这里表示查询数据库前count的记录,收到的参数是100,就查100条 break; case "news": strselect = string.Format(@"SELECT TOP {0} * FROM [news] ORDER BY [news_id] DESC", count); break; case "guest": strselect = string.Format(@"SELECT TOP {0} * FROM [guest] ORDER BY [guest_id] DESC", count); break; case "shop": strselect = string.Format(@"SELECT * FROM [product] WHERE categoryID={0} ORDER BY [productid] DESC", count); break; case "PEO": strselect = string.Format(@"SELECT TOP {0} * FROM [PEO] ORDER BY [peo_id] DESC", count); break; case "list": strselect = string.Format(@"SELECT TOP {0} * FROM [list] ORDER BY [list_id] DESC", count); break; case "user": strselect = string.Format(@"SELECT * FROM [user] WHERE typeuser_id={0} ORDER BY [user_id] DESC", count); break; case "typeuser": strselect = string.Format(@"SELECT TOP {0} * FROM [list] ORDER BY [list_id] DESC", count); break; } DataView dv = DB.Get_Select(strselect);//你看这里又掉用了DB类的,get_select方法 return dv; } // 此方法功能强大,可以查询数据库的任一表的记录;数据处理层:下面我们看看DB类的,GET—SELECT方法是什么strselect参数,就是一条SQL字符串。只要一个方法,便可查询任一表的数据并返回
using System.Data.OleDb; public class DB ... { public static OleDbConnection con() ...{ string Constr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|/CXF.ascx"; return new OleDbConnection(Constr); } //数据库连接 public static DataView Get_Select(string strselect) ...{ OleDbDataAdapter sdr = new OleDbDataAdapter(strselect, DB.con()); DataSet ds = new DataSet(); sdr.Fill(ds, "table"); return ds.Tables["table"].DefaultView; }