您是第 Web Page Tracking 位访客

水~墨~

昂首阔步,不留一点遗憾!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
Technorati 标签: ADO.NET,LINQ

ADO.NET

 

 

1. DataSet 联合 DataAdapter 更新数据库表的值 (Dataset每列行都有状态标志)、

 

1)更改数据库的值。

      1.首先更改DataSet.Tables[“Name”].Rows[“xx”]的值(此时只是更改了内存的值,数据库未生效。)

      2.然后用DataAdapter.updata方法更新到数据库。

2)删除DataSet某列

     步骤: 1.dataset找到该行  find()方法

               2.删除,并update更新

3)添加一行

    步骤:  1. 查找是否存在,如果有就不能插入了

       2.不存在,就新建datarow,然后DataSet.Rows.Add 添加

       3.DataAdapter.update  方法更新到数据库

 

 

 

2.DataRelation对象

 

 

DataRelation 的一项主要功能就是在 DataSet 中从一个 DataTable 浏览到另一个。 它使您能够在给定相关 DataTable 中的单个 DataRow 的情况下检索一个 DataTable 中的所有相关 DataRow 对象。 例如,当建立客户表和订单表之间的 DataRelation 后,可以使用 GetChildRows 检索特定客户行的所有订单行。

[补充:da.fill   多个数据集  默认名称table0,table1]

 

 

 

 

使用步骤: 1. 填充到DataSet  2个表。  Customer  和 Orders   靠CustomerId关联

                2. 声明 DataRelation cusRel = ds.Relations.Add("CustOrders",ds.Tables[0].Columns["CustomerId"],ds.Tables[1].Columns["CustomerId"]); //此处参数不要换行写

此处第2个参数 为父表的Id

               3.然后Foreach 用 GetChildRows 检索特定客户行的所有订单行

 

 

static private string GetConnectionString()
       {
           // To avoid storing the connection string in your code, 
           // you can retrieve it from a configuration file, using the 
           // System.Configuration.ConfigurationSettings.AppSettings property 
           return "Data Source=(local);Initial Catalog=Northwind;"
               + "Integrated Security=SSPI;";
       }
 
       static void Main(string[] args)
       {
 
 
           // DataRelation
           try
           {
               using (SqlConnection con = new SqlConnection(GetConnectionString()))
               {
 
                   SqlCommand cmd = new SqlCommand("select top 100 * from customers;select * from orders", con);
                   SqlDataAdapter da = new SqlDataAdapter(cmd);
                   DataSet ds = new DataSet();
                   da.Fill(ds);
 
 
 
                   DataRelation cusRel = ds.Relations.Add("CustOrders",ds.Tables[0].Columns["CustomerId"],ds.Tables[1].Columns["CustomerId"]); //此处参数不要换行写
                   foreach (DataRow dr in ds.Tables[0].Rows)
                   {
                       Write("--------------------------"+dr["CustomerID"].ToString()+"--------------------------");
 
                       foreach (DataRow re in dr.GetChildRows(cusRel))
                       {
                           Write(re["OrderId"].ToString());
 
                       }
 
                   }
 
                   //此时开始Linq OVER dataset 直接使用
                   var customers = ds.Tables[0].AsEnumerable();
                   var Customerresult = from a in customers
                                select a["CustomerId"];
                   foreach (var d in Customerresult)
                   {
                       Write(d);
                   }
 
                   //Write(ds.Tables[0].Rows.Count.ToString());
               }
           }
 
           catch (Exception exe)
           {
               Write(exe.Message);
           }

 

 

 

 

 

3.ADO.NET调用存储过程

 

 

   1)设置commandText 存储过程名字

   2)设置参数

   3) 执行

 

 

 

 

 

 

Linq Over DataSet

 

 

实现 IEnumerable<(Of <(T>)>) 泛型接口的数据源可以通过 LINQ 进行查询。 对 DataTable 调用 AsEnumerable 将返回实现泛型 IEnumerable<(Of <(T>)>) 接口的对象,作为 LINQ to DataSet 查询的数据源。

 

 

关键:var customers = ds.Tables[0].AsEnumerable();

 

//此时开始Linq OVER dataset 直接使用
                   var customers = ds.Tables[0].AsEnumerable();
                   var Customerresult = from a in customers
                                select a["CustomerId"];
                   foreach (var d in Customerresult)
                   {
                       Write(d);
                   }
posted on 2012-07-08 23:14  水墨.MR.H  阅读(417)  评论(0编辑  收藏  举报
知识共享许可协议
本博客为水墨原创,基于Creative Commons Attribution 2.5 China Mainland License发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的水墨(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。