DataSet详细用法最全面.docx
《DataSet详细用法最全面.docx》由会员分享,可在线阅读,更多相关《DataSet详细用法最全面.docx(13页珍藏版)》请在冰豆网上搜索。
DataSet详细用法最全面
Forpersonaluseonlyinstudyandresearch;notforcommercialuse
Forpersonaluseonlyinstudyandresearch;notforcommercialuse
DataSet用法详细
一、特点介绍
1、处理脱机数据,在多层应用程序中很有用。
2、可以在任何时候查看DataSet中任意行的内容,允许修改查询结果的方法。
3、处理分级数据
4、缓存更改
5、XML的完整性:
DataSet对象和XML文档几乎是可互换的。
二、使用介绍
1、创建DataSet对象:
DataSetds=newDataSet("DataSetName");
2、查看调用SqlDataAdapter.Fill创建的结构
da.Fill(ds,"Orders");
DataTabletbl=ds.Table[0];
foreach(DataColumncolintbl.Columns)
Console.WriteLine(col.ColumnName);
3、查看SqlDataAdapter返回的数据
①DataRow对象
DataTabletbl=ds.Table[0];
DataRowrow=tbl.Row[0];
Console.WriteLine(ros["OrderID"]);
②检查存储在DataRow中的数据
DataTabletbl=row.Table;
foreach(DataColumncolintbl.Columns)
Console.WriteLine(row[col]);
③检查DatTable中的DataRow对象
foreach(DataRowrowintbl.Rows)
DisplayRow(row);
4、校验DataSet中的数据
①校验DataColumn的属性:
ReadOnly,AllowDBNull,MaxLength,Unique
②DataTable对象的Constrains集合:
UiqueConstraints,Primarykey,ForeignkeyConstraints
通常不必刻意去创建ForeignkeyConstraints,因为当在DataSet的两个DataTable对象之间创建关系时会创建一个。
③用SqlDataAdapter.Fill模式来检索模式信息
5、编写代码创建DataTable对象
①创建DataTable对象:
DataTabletbl=newDataTable("TableName");
②将DataTable添加到DataSet对象的Table集合
DataSetds=newDataSet();
DataTabletbl=newDataTable("Customers");
ds.Tables.Add(tbl);
DataSetds=newDataSet();
DataTabletbl=ds.Tables.Add("Customers");
DataTable对象只能存在于至多一个DataSet对象中。
如果希望将DataTable添加到多个DataSet中,就必须使用Copy方法或Clone方法。
Copy方法创建一个与原DataTable结构相同并且包含相同行的新DataTable;Clone方法创建一个与原DataTable结构相同,但没有包含任何行的新DataTable。
③为DataTable添加列
DataTabletbl=ds.Tables.Add("Orders");
DataColumncol=tbl.Columns.Add("OrderID",typeof(int));
col.AllowDBNull=false;
col.MaxLength=5;
col.Unique=true;
tbl.PrimaryKey=newDataColumn[]{tbl.Columns["CustomersID"]};
当设置主键时,AllowDBNull自动设置为False;
④处理自动增量列
DataSetds=newDataSet();
DataTabletbl=ds.Tables.Add("Orders");
DataColumncol=tbl.Columns.Add("OrderID",typeof(int));
col.AutoIncrement=true;
col.AutoIncrementSeed=-1;
col.AutoIncrementStep=-1;
col.ReadOnly=true;
⑤添加基于表达式的列
tbl.Columns.Add("ItemTotal",typeof(Decimal),"Quantity*UnitPrice");
6、修改DataTable内容
①添加新DataRow
DataRowrow=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对象来提交交给数据库的待定的更改。
DataRowrowCustomer;
rowCustomer=ds.Tables["Custoemrs"].Rows.Find("ANTON");
if(rowCustomer==null)
//没有查找客户
else
{
rowCustomer["CompanyName"]="NewCompanyName";
rowCustomer["ContactName"]="NewContactName";
}
//推荐使用这种方式
DataRowrowCustomer;
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}
DataRowrowCustomer;
rowCustomer=ds.Tables["Customers"].Rows.Find("ALFKI");
rowCustomer.ItemArray=aCustomer;
③处理DataRow的空值
//查看是否为空
DataRowrowCustomer;
rowCustomer=ds.Tables["Customers"].Rows.Find("ALFKI");
if(rowCustomer.IsNull("Phone"))
Console.WriteLine("It''''sNull");
else
Console.WriteLine("It''''snotNull");
//赋予空值
rowCustomer["Phone"]=DBNull.Value;
④删除DataRow
DataRowrowCustomer;
rowCustomer=ds.Tables["Customers"].Rows.Find("ALFKI");
rowCustomer.Delete();
⑤清除DataRow
DataRowrowCustomer=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
privatevoidDemonstrateRowState()
{
//RunafunctiontocreateaDataTablewithonecolumn.
DataTablemyTable=MakeTable();
DataRowmyRow;
//CreateanewDataRow.
myRow=myTable.NewRow();
//Detachedrow.
Console.WriteLine("NewRow"+myRow.RowState);
myTable.Rows.Add(myRow);
//Newrow.
Console.WriteLine("AddRow"+myRow.RowState);
myTable.AcceptChanges();
//Unchangedrow.
Console.WriteLine("AcceptChanges"+myRow.RowState);
myRow["FirstName"]="Scott";
//Modifiedrow.
Console.WriteLine("Modified"+myRow.RowState);
myRow.Delete();
//Deletedrow.
Console.WriteLine("Deleted"+myRow.RowState);
}
⑦检查DataRow中的挂起更改
DataRowrowCustomer;
rowCustomer=ds.Tables["Customers"].Rows.Find("ALFKI");
rowCustomer["CompanyName"]="NewCompanyName";
stringstrNewCompanyName,strOldCompanyName;
Console.WriteLine(rowCustomer["CompanyName",DataRowVersion.Current]);
Console.WriteLine(rowCustomer["CompanyName",DataRowVersion.Original]);
⑧遍历DataSet
foreach(DataTabledtindataSet.Tables)
foreach(DataRowdrindt.Rows)
foreach(DataColumndcindr.Table.Columns)
Cons