C#DataRow注意的問題

using System;
using System.Data;
using System.Data.SqlClient;
 using System.Collections;
 using System.Text;
namespace AddingData {
/// /// Class1 的摘要說明。
 ///
class Class1 {
/// /// 應用程序的主入口點。
/// [STAThread]
static void Main(string[] args) {
SqlConnection thisConnection = new SqlConnection( "Server=(local); Integrated Security= True;Database = northwind");
SqlDataAdapter thisAdapter = new SqlDataAdapter( "SELECT CustomerID, CompanyName FROM Customers", thisConnection);
SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
 DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet,"Customers");
Console.WriteLine("#rowsbeforechange:{0}",thisDataSet.Tables["Customers"].Rows.Count);
DataRow thisRow = thisDataSet.Tables["Customers"].NewRow();
DataTable dtt = thisDataSet.Tables["Customers"]; DataColumn thisColumn = new DataColumn();
thisRow["CustomerID"] = "ZACZI";
thisRow["CompanyName"] = "Zachary Zithers Ltd";
dtt.Rows.Add(thisRow); DataRow thisrow = thisDataSet.Tables["Customers"].NewRow(); Console.WriteLine("# rows after change:{0}",thisDataSet.Tables["Customers"].Rows.Count);
 thisAdapter.Update(thisDataSet,"Customers");
//在這裏要注意,這裏會引發異常,有可能會出現主鍵約束的問題
DataTable dt = thisDataSet.Tables["Customers"];
DataRow rowTen = dt.Rows[9];
Object CustomerID = rowTen["CustomerID"];
Object CompanyName = rowTen["CompanyName"];
Console.WriteLine("before CusteomerID: {0} CompanyName: {1}",rowTen["CustomerID"],rowTen["CompanyName"]); // CompanyName = "aa";這裏需要注意。因爲不可以這樣給他賦值,我試了幾次,發現只能這樣給裏面賦值: rowTen["CompanyName"]="aa"); Console.WriteLine("changedCusteomerID:{0}CompanyName:{1}",rowTen["CustomerID"],rowTen["CompanyName"]="aa");
thisAdapter.Update(thisDataSet,"Customers"); thisConnection.Close(); Console.Write("Program finished ,press Enter/Return to continue"); Console.ReadLine();
}
}
}
發佈了34 篇原創文章 · 獲贊 0 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章