`

C# DataSet和DataTable详解(一)

    博客分类:
  • .net
阅读更多
2009-4-21
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集合
    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 = 5; 

    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 = -1; 

    col.AutoIncrementStep = -1; 

    col.ReadOnly = true; 

⑤、添加基于表达式的列
    tbl.Columns.Add("ItemTotal",typeof(Decimal),"Quantity*UnitPrice");
 

6、修改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]);
分享到:
评论

相关推荐

    C# DataSet和DataTable详解

    C# DataSet和DataTable详解

    C#_DataSet和DataTable详解

    C#_DataSet和DataTable详解

    详解C#的DataSet和DataTable

    详解C#的DataSet和DataTable,顾名思义,详细的介绍了DataSet和DataTable两者的关系和区别

    C__DataSet和DataTable详解

    C__DataSet和DataTable详解

    C#中DataSet的用法(很详细)

    DataSet是ADO.NET开发人员为方便数据处理开发出来的,是数据的集合。DataSet的功能:浏览、排序、搜索、过滤、处理分级数据、缓存...DataSet中可包括多个DataTable,可将多个查询结构存到一个DataSet中,方便操作.....

    C#版ADO.NET实践技巧

    包含ADO.net实践技巧、C#数据库入门和C# DataSet和DataTable详解,学习C#操作ADO.Net的很好实用技巧。

    DataGridView 详解 使用技巧 添加、修改、删除数据操作

    对DataGridView进行添加、修改、删除数据操作 DataGridView的使用 DataSet和DataTable详解 DataGridView的使用技巧

    详解DataView用法

    详解DataView用法 DataTable DataSet

    asp.net知识库

    C#中 const 和 readonly 的区别 利用自定义属性,定义枚举值的详细文本 Web标准和ASP.NET - 第一部分 XHTML介绍 在ASP.NET页面中推荐使用覆写(Override)而不是事件处理(Event Handler) 常用编码工具类,支持base...

    详解C#中SqlParameter的作用与用法

    一般来说,在更新DataTable或是DataSet时,如果不采用SqlParameter,那么当输入的Sql语句出现歧义时,如字符串中含有单引号,程序就会发生错误,并且他人可以轻易地通过拼接Sql语句来进行注入攻击。 string sql =...

    亮剑.NET深入体验与实战精要2

    15.5.5 SqlDataRead和Dataset的选择 567 15.5.6 ExecuteNonQuery和 ExecuteScalar的选择 568 15.5.7 数据的绑定DataBinder 568 15.5.8 使用DataReader的注意事项 568 15.5.9 Command对象的使用 569 15.5.10 反复地...

    亮剑.NET深入体验与实战精要3

    15.5.5 SqlDataRead和Dataset的选择 567 15.5.6 ExecuteNonQuery和 ExecuteScalar的选择 568 15.5.7 数据的绑定DataBinder 568 15.5.8 使用DataReader的注意事项 568 15.5.9 Command对象的使用 569 15.5.10 反复地...

Global site tag (gtag.js) - Google Analytics