上方廣告

Lady Kukki 手作糖霜餅乾

2008年12月21日 星期日

SqlDataSource 撰寫在後端程式碼的運用

SqlDataSource的運用方法通常由精靈產生主要用在aspx頁面,下面的例子則是撰寫在.cs檔的用法,舉查詢為例,其它的用法大致上相同

SqlDataSource sqlDataSource1 = new SqlDataSource();
sqlDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

string strsql = "select d.*, c.* from ProductClass c right join( " +
  "select * from ProductBase a left join " +
  "(select ProID as bProID, IsAfresh , Photo3 from ProductInfo)b " +
  "on a.ProID= bProID where a.DepID=@DepID) d " +
  "on c.ClassID = d.ClassID order by d.ProID Desc";

sqlDataSource1.SelectCommand = strsql;

sqlDataSource1.SelectParameters.Add("DepID", Session["DeptID"].ToString());
//sqlDataSource1.DataSourceMode = SqlDataSourceMode.DataSet

GridView1.DataSource = sqlDataSource1;
GridView1.DataKeyNames = new string[] { "ProID" };//主鍵
GridView1.DataBind();

sqlDataSource1.Dispose();