国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C# DataSet和DataTable詳解

2019-11-17 04:01:02
字體:
來源:轉載
供稿:網友
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 Exception 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松溪县| 大理市| 太谷县| 科尔| 万年县| 馆陶县| 绍兴县| 错那县| 隆德县| 莲花县| 湖州市| 武川县| 云南省| 花莲市| 通江县| 鄂尔多斯市| 南陵县| 敦煌市| 潮州市| 合山市| 图们市| 东乡县| 涿州市| 安国市| 阳泉市| 进贤县| 霍林郭勒市| 仪陇县| 澎湖县| 科尔| 房产| 香河县| 阿坝| 兰州市| 井冈山市| 伊春市| 剑阁县| 金坛市| 东方市| 民乐县| 阜新市|