//多表多行多列的情况
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, PR imarykey,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中的字符串比较是不是界别大小写。