//多表多行多列的情況
foreach (DataTable dt in YourDataset.Tables) //遍歷所有的datatable
{
foreach (DataRow dr in dt.Rows) ///遍歷所有的行
foreach (DataColumn dc in dt.Columns) //遍歷所有的列
Console.WriteLine(“{0}, {1}, {2}”, dt.TableName,
dc.ColumnName, dr[dc]); //表名,列名,單元格數據
}
//遍歷一個表多行多列
foreach(DataRow mDr in dataSet.Tables[0].Rows )
{
foreach(DataColumn mDc in dataSet.Tables[0].Columns)
{
Console.WriteLine(mDr[mDc].ToString());
}
}
//遍歷一個表多行一列
foreach(DataRow row in DataSet1.Tables[0].Rows)
{
Console.WriteLine(row[0].ToString());
}
//一行一列
ds.Tables[0].Rows[0]["字段"]
一、創造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中的字符串比較是不是界別大小寫。