-
C# DataSet和DataTable詳解
2009-四-21
一、創造DataSet物件:
DataSet ds = new DataSet("DataSetName");
二、查看呼叫SqlDataAdapter.Fill創造的構造
da.Fill(ds,"Orders");
DataTable tbl = ds.Table[零];
foreach(DataColumn col in tbl.Columns)
Console.WriteLine(col.ColumnName);
三、查看SqlDataAdapter回到的資料①、DataRow對象
DataTable tbl = ds.Table[零];
DataRow row = tbl.Row[零];
Console.WriteLine(ros["OrderID"]);
②、稽查儲存在DataRow中的數據
DataTable tbl = row.Table;
foreach(DataColumn col in tbl.Columns)
Console.WriteLine(row[col]);
③、檢察DatTable中的DataRow對象
foreach(DataRow row in tbl.Rows)
DisplayRow(row);
四、校驗DataSet中的資料①、校驗DataColumn的屬性:ReadOnly,AllowDBNull,MaxLength,Unique
②、DataTable物件的Constrains聚合:UiqueConstraints,Primarykey,ForeignkeyConstraints
正常無庸刻意去創辦ForeignkeyConstraints,由於當在DataSet的兩個DataTable對像其間創辦關係時會創造一個。
③、用SqlDataAdapter.Fill方式來檢索方式訊息
五、編纂程式碼創造DataTable對象
①、創辦DataTable物件:DataTable tbl = new DataTable("TableName");
②、將DataTable加入DataSet物件的Table會合
DataSet ds = new DataSet();
DataTable tbl = new DataTable("Customers");
ds.Tables.Add(tbl);
DataSet ds = new DataSet();
DataTable tbl = ds.Tables.Add("Customers");
DataTable物件只得存在於最多一個DataSet物件中。如若希望將DataTable新增至多個DataSet中,就必須套用Copy步驟或Clone步驟。 Copy步驟創辦一個與原DataTable建構雷同而且包孕雷同行的新DataTable;Clone步驟創辦一個與原DataTable構造雷同,但沒包孕任何行的新DataTable。
③、為DataTable平添列
DataTable tbl = ds.Tables.Add("Orders");
DataColumn col =tbl.Columns.Add("OrderID",typeof(int));
col.AllowDBNull = false;
col.MaxLength = 五;
col.Unique = true;
tbl.PrimaryKey = new DataColumn[]{tbl.Columns["CustomersID"]};
應設立主鍵時,AllowDBNull自動設立為False;
④、處置自動增量列
DataSet ds = new DataSet();
DataTable tbl = ds.Tables.Add("Orders");
DataColumn col = tbl.Columns.Add("OrderID",typeof(int));
col.AutoIncrement = true;
col.AutoIncrementSeed = -一;
col.AutoIncrementStep = -一;
col.ReadOnly = true;
⑤、增添基於表達式的列
tbl.Columns.Add("ItemTotal",typeof(Decimal),"Quantity*UnitPrice");
六、批改DataTable內容①、平添新DataRow
DataRow row = ds.Tables["Customers"].NewRow();
row["CustomerID"] = "ALFKI";
ds.Tables["Customers"].Rows.Add(row);
object[] aValues ={"ALFKI","Alfreds","Anders","030-22222"};
da.Tables["Customers"].LoadDataRow(aValues,false);
②、批改現階段行
批改行的內容刑訊內不會自動批改資料庫中呼應的內容,對行所做的批改被視為是過後將應用SqlDataAdapter物件來交付付給資料庫的待定的改動。
DataRow rowCustomer;
rowCustomer = ds.Tables["Custoemrs"].Rows.Find("ANTON");
if(rowCustomer == null)
//沒查尋客戶
else
{
rowCustomer["CompanyName"] ="NewCompanyName";
rowCustomer["ContactName"] ="NewContactName";
}
//推薦施用這種形式
DataRow rowCustomer;
rowCustomer = ds.Tables["Custoemrs"].Rows.Find("ANTON");
if(rowCustomer == null)
//沒查尋客戶
else
{
rowCustomer.BeginEdit();
rowCustomer["CompanyName"] ="NewCompanyName";
rowCustomer["ContactName"] ="NewContactName";
rowCustomer.EndEdit();
}
//null示意不批改該列的數據
obejct[] aCustomer ={null,"NewCompanyName","NewContactName",null}
DataRow rowCustomer;
rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");
rowCustomer.ItemArray = aCustomer;
③、處置DataRow的空值
//查看是不是為空
DataRow rowCustomer;
rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");
if(rowCustomer.IsNull("Phone"))
Console.WriteLine("It's Null");
else
Console.WriteLine("It's not Null");
//授予空值
rowCustomer["Phone"] = DBNull.Value;
④、剔除DataRow
DataRow rowCustomer;
rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");
rowCustomer.Delete();
⑤、驅除DataRow
DataRow rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");
rowCustomer.ItemArray = aCustomer;
da.Tables["Customers"].Remove(rowCustomer);
或許
ds.Tables["Customers"].RemoveAt(intIndex);
⑥、運用DataRow.RowState屬性:Unchanged,Detached,Added,Modified,Deleted
private void DemonstrateRowState()
{ // Run a function to create a DataTable with one column. DataTable myTable = MakeTable();DataRow myRow;
// Create a new DataRow. myRow = myTable.NewRow();// Detached row. Console.WriteLine("New Row " + myRow.RowState);
myTable.Rows.Add(myRow);// New row. Console.WriteLine("AddRow " + myRow.RowState);
myTable.AcceptChanges();// Unchanged row. Console.WriteLine("AcceptChanges " + myRow.RowState);
myRow["FirstName"] = "Scott";// Modified row. Console.WriteLine("Modified " + myRow.RowState);
myRow.Delete();// Deleted row. Console.WriteLine("Deleted " + myRow.RowState);}
⑦、檢察DataRow中的掛起更動
DataRow rowCustomer;
rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");
rowCustomer["CompanyName"] = "NewCompanyName";
string strNewCompanyName,strOldCompanyName;
Console.WriteLine(rowCustomer["CompanyName",DataRowVersion.Current]);
Console.WriteLine(rowCustomer["CompanyName",DataRowVersion.Original]);
一、DataSet
①、屬性
CaseSensitive:用來統制DataTable中的字串比較是不是界別大小寫。
TOP
本文來源:
我的異常網
Java Exception
Dotnet Exception
Oracle