dataset更新例子

using   System;   
  //   use   ADO.NET   namespace   
  using   System.Data;   
  //   use   OLE   DB   .NET   Data   Provider   
  using   System.Data.OleDb;   
   
  class   DataAddRowExample   
  {   
  public   static   void   Main()     
  {   
  //   connect   to   NWIND   MS   Access   example   with   OLE   DB   
  OleDbConnection   thisConnection   =   new   OleDbConnection(   
  @"Provider=Microsoft.Jet.OLEDB.4.0;Data   Source=C:/TMP/NWIND.MDB");   
  //   open   connection   
  thisConnection.Open();   
  //   create   DataAdapter   object   for   update   and   other   operations   
  OleDbDataAdapter   thisAdapter   =   new   OleDbDataAdapter(     
  "SELECT   CustomerID,   CompanyName   FROM   Customers",   thisConnection);   
  //   create   CommandBuilder   object   to   build   SQL   commands   
  OleDbCommandBuilder   thisBuilder   =   new   OleDbCommandBuilder(thisAdapter);   
  //   create   DataSet   to   contain   related   data   tables,   rows,   and   columns   
  DataSet   thisDataSet   =   new   DataSet();   
  //   fill   DataSet   using   query   defined   previously   for   DataAdapter   
  thisAdapter.Fill(thisDataSet,   "Customers");   
   
  Console.WriteLine("#   rows   before   change:   {0}",   
  thisDataSet.Tables["Customers"].Rows.Count);   
   
  DataRow   thisRow   =   thisDataSet.Tables["Customers"].NewRow();   
  thisRow["CustomerID"]   =   "ZACZI";   
  thisRow["CompanyName"]   =   "Zachary   Zithers   Ltd.";   
  thisDataSet.Tables["Customers"].Rows.Add(thisRow);   
   
  Console.WriteLine("#   rows   after   change:   {0}",   
  thisDataSet.Tables["Customers"].Rows.Count);   
   
  thisAdapter.Update(thisDataSet,   "Customers");   
   
   
  Console.WriteLine("name   after   change:   {0}",   
  thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]);   
   
  thisConnection.Close();   
  }   
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值