//本段代码为部分核心代码,详细代码请参考下面的网址: 1. A Practical Guide to .NET DataTables, DataSets and DataGrids http://www.codeproject.com/csharp/PracticalGuideDataGrids3.asp 2.Professional .NET Framework - Chapter 10 http://www.codeproject.com/books/1861005563_10.asp 3.<<c#数据库入门经典>>第8~10章 清华大学出版社翻译的www.wrox.com的系列教材// Assign the table containing both Elements and // Isotopes to dt using index 0 DataTable dt = ds.Tables[0]; // Create two DataTable variables dtElements and dtIsotopes and // assign tables contained in the DataSet elementDS Tables // collection using TableNames as indexes. DataTable dtElements = elementDS.Tables[“Elements”]; DataTable dtIsotopes = elementDS.Tables[“Isotopes”]; DataRow drElement; DataRow drIsotope; int prevAtomicNbr = 0; foreach (DataRow dr in dt.Rows) { if(prevAtomicNbr != (int)dr["AtomicNbr"]) { //need only one row per AtomicNbr in Table[“Elements”] // Fill an element row with data from dt. prevAtomicNbr = (int)dr["AtomicNbr"]; drElement = dtElements.NewRow(); drElement["Atomic Number"] = dr["AtomicNbr"]; drElement["Element"] = dr["Element"]; drElement["Symbol"] = dr["Symbol"]; dtElements.Rows.Add(drElement); } // Fill an isotope row with data from dt. drIsotope = dtIsotopes.NewRow(); drIsotope["Isotope Number"] = dr["IsotopeNbr"]; drIsotope["Symbol"] = dr["Symbol"]; drIsotope["Atomic Number"] = dr["AtomicNbr"]; drIsotope["Percent Abundance"] = dr["PctAbundance"]; drIsotope["Atomic Mass"] = dr["AtomicMass"]; dtIsotopes.Rows.Add(drIsotope); }// Create temporary DataSet variable. DataSet dsChanges; // GetChanges for modified rows only. dsChanges = ds.GetChanges(DataRowState.Modified); // Check the DataSet for errors. if(!dsChanges.HasErrors) { // No errors were found, update the DBMS with the SqlDataAdapter da // used to create the DataSet. da.RowUpdating += new SqlRowUpdatingEventHandler(OnRowUpdating); da.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated); int res = da.Update(dsChanges); // returns the number of rows updated da.RowUpdating -= new SqlRowUpdatingEventHandler(OnRowUpdating); da.RowUpdated -= new SqlRowUpdatedEventHandler(OnRowUpdated); }
1. A Practical Guide to .NET DataTables, DataSets and DataGrids
http://www.codeproject.com/csharp/PracticalGuideDataGrids3.asp
2.Professional .NET Framework - Chapter 10
http://www.codeproject.com/books/1861005563_10.asp
3.<<c#数据库入门经典>>第8~10章
清华大学出版社翻译的www.wrox.com的系列教材// Assign the table containing both Elements and
// Isotopes to dt using index 0
DataTable dt = ds.Tables[0];
// Create two DataTable variables dtElements and dtIsotopes and
// assign tables contained in the DataSet elementDS Tables
// collection using TableNames as indexes.
DataTable dtElements = elementDS.Tables[“Elements”];
DataTable dtIsotopes = elementDS.Tables[“Isotopes”];
DataRow drElement;
DataRow drIsotope;
int prevAtomicNbr = 0;
foreach (DataRow dr in dt.Rows)
{
if(prevAtomicNbr != (int)dr["AtomicNbr"])
{ //need only one row per AtomicNbr in Table[“Elements”]
// Fill an element row with data from dt.
prevAtomicNbr = (int)dr["AtomicNbr"];
drElement = dtElements.NewRow();
drElement["Atomic Number"] = dr["AtomicNbr"];
drElement["Element"] = dr["Element"];
drElement["Symbol"] = dr["Symbol"];
dtElements.Rows.Add(drElement);
}
// Fill an isotope row with data from dt.
drIsotope = dtIsotopes.NewRow();
drIsotope["Isotope Number"] = dr["IsotopeNbr"];
drIsotope["Symbol"] = dr["Symbol"];
drIsotope["Atomic Number"] = dr["AtomicNbr"];
drIsotope["Percent Abundance"] = dr["PctAbundance"];
drIsotope["Atomic Mass"] = dr["AtomicMass"];
dtIsotopes.Rows.Add(drIsotope);
}// Create temporary DataSet variable.
DataSet dsChanges;
// GetChanges for modified rows only.
dsChanges = ds.GetChanges(DataRowState.Modified);
// Check the DataSet for errors.
if(!dsChanges.HasErrors)
{
// No errors were found, update the DBMS with the SqlDataAdapter da
// used to create the DataSet.
da.RowUpdating += new SqlRowUpdatingEventHandler(OnRowUpdating);
da.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);
int res = da.Update(dsChanges); // returns the number of rows updated
da.RowUpdating -= new SqlRowUpdatingEventHandler(OnRowUpdating);
da.RowUpdated -= new SqlRowUpdatedEventHandler(OnRowUpdated);
}
然后用adapter对dataSet进行更新
adapter.UpDate(dataSet);
adapter需要select,update,delete,insert四个command可以通过sqlCommandBuilder生成.在adapter的配置向导内会自动生成关于第一次给的那张表的四个command,如果后来换了表操作就必须用commandBuilder重新生成command并给adapter赋上。