2009年4月21日 星期二

以程式控制SqlDataSource

System.Web.UI.WebControls.SqlDataSource sds = new SqlDataSource();
sds.ConnectionString = "server=localhost;Database=TEST;user id=123;password=123";

//select----------------------------------------------------------
sds.DataSourceMode = SqlDataSourceMode.DataReader; //or DataSet
sds.SelectCommand = "SELECT * FROM Table1";
SqlDataReader sdr = (SqlDataReader)sds.Select(new System.Web.UI.DataSourceSelectArguments());

while (sdr.Read())
{
this.Label1.Text += sdr["NAME"].ToString() + "/";
}

//insert----------------------------------------------------------
sds.InsertCommand = "insert into Table1(NAME) values('today2')";
int insertCount = sds.Insert();

//update----------------------------------------------------------
sds.UpdateCommand = "update Table1 set NAME = 'today3' WHERE NAME = 'today2'";
int updateCount = sds.Update();

//delete----------------------------------------------------------
sds.DeleteCommand = "delete from Table1 where NAME = 'today3'";
int deleteCount = sds.Delete();

沒有留言:

張貼留言