詳:http://cht.gotdotnet.com/quickstart/howto/doc/adoplus/updatedatafromdb.aspx
// Create a new Connection and SqlDataAdapter
SqlConnection myConnection = new SqlConnection("server=(local)\\VSdotNET;Trusted_Connection=yes;database=northwind");
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("Select * from Customers", myConnection);
DataSet myDataSet = new DataSet();
DataRow myDataRow;
// Create command builder. This line automatically generates the update commands for you, so you don't
// have to provide or create your own.
SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);
// Set the MissingSchemaAction property to AddWithKey because Fill will not cause primary
// key & unique key information to be retrieved unless AddWithKey is specified.
mySqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
mySqlDataAdapter.Fill(myDataSet, "Customers");
myDataRow = myDataSet.Tables["Customers"].NewRow();
myDataRow["CustomerId"] = "NewID";
myDataRow["ContactName"] = "New Name";
myDataRow["CompanyName"] = "New Company Name";
myDataSet.Tables["Customers"].Rows.Add(myDataRow);
DataTable 必須透過 NewRow 方法傳回 DataRow。該方法會以適當的 DataTable 結構描述 (Schema) 傳回 DataRow 物件。新的 DataRow 在加入 RowsCollection 之前,是不受資料表支配的。
您可以藉由存取 DataRow 來變更其中的資料。透過 Rows 屬性,您可以在 RowsCollection 中使用資料列的索引:
myDataSet.Tables["Customers"].Rows[0]["ContactName"]="Peach";
|