String strConn = "data source=.;user id=aa;password=aa;initial Catalog=northwind";
using (SqlConnection conn = new SqlConnection(strConn))
{
String strCmd = "Select ProductID,ProductName from Products where categoryID=@categoryID";
using (SqlDataAdapter da = new SqlDataAdapter(strCmd, conn))
{
da.SelectCommand.Parameters.AddWithValue("@categoryID", 1);
DataSet ds = new DataSet();
da.Fill(ds, "dsProducts");
////新增,刪除,修改才要用
//SqlCommandBuilder builder = new SqlCommandBuilder(da);
////新增
//builder.GetInsertCommand();
//DataTable productsTable = ds.Tables["dsProducts"];//修改會用這這行
//DataRow newRow = productsTable.NewRow();
//newRow["ProductName"] = "IP5S";
//newRow["ProductID"] = "1";
//productsTable.Rows.Add(newRow);
////修改
//builder.GetUpdateCommand();
//productsTable.Rows[1]["ProductName"] = "KEVIN";
////刪除
//builder.GetDeleteCommand();
//ds.Tables["dsProducts"].Rows[12].Delete();
////最後更新
//da.Update(ds, "dsProducts"); //最後更新
//查詢用到的迴圈
foreach (DataRow theRow in ds.Tables["dsProducts"].Rows)
{
Response.Write(theRow[0].ToString() + "," + theRow["ProductName"].ToString() + "<br>");
}
沒有留言:
張貼留言