C#之DataSet与Datatable

来源:互联网 发布:日语电子词典推荐知乎 编辑:程序博客网 时间:2024/05/20 12:51

    文章来源于:http://tech.ddvip.com/2009-04/1240297901116084.html

 

 

 1、创建DataSet对象:

  DataSet ds = new DataSet("DataSetName");

  2、查看调用SqlDataAdapter.Fill创建的结构

   da.Fill(ds,"Orders");

   DataTable tbl = ds.Table[0];

   foreach(DataColumn col in tbl.Columns)

   Console.WriteLine(col.ColumnName);

  3、查看SqlDataAdapter返回的数据

  ①、DataRow对象

   DataTable tbl = ds.Table[0];

   DataRow row = tbl.Row[0];

   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);

  4、校验DataSet中的数据

  ①、校验DataColumn的属性:ReadOnly,AllowDBNull,MaxLength,Unique

  ②、DataTable对象的Constrains集合:UiqueConstraints,Primarykey,ForeignkeyConstraints

  通常不必刻意去创建ForeignkeyConstraints,因为当在DataSet的两个DataTable对象之间创建关系时会创建一个。

  ③、用SqlDataAdapter.Fill模式来检索模式信息

  5、编写代码创建DataTable对象

  ①、创建DataTable对象:DataTable tbl = new DataTable("TableName");

  ②、将DataTable添加到DataSet对象的Table集合

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
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添加列

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
DataTable tbl = ds.Tables.Add("Orders");
 
  DataColumn col =tbl.Columns.Add("OrderID",typeof(int));
 
  col.AllowDBNull = false;
 
  col.MaxLength = 5;
 
  col.Unique = true;
 
  tbl.PrimaryKey = new DataColumn[]{tbl.Columns["CustomersID"]};

   当设置主键时,AllowDBNull自动设置为False;

  ④、处理自动增量列

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
DataSet ds = new DataSet();
 
DataTable tbl = ds.Tables.Add("Orders");
 
DataColumn col = 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

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
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对象来提交交给数据库的待定的更改。

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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;

 

0 0
原创粉丝点击