Csharp/CSharp Tutorial/ADO.Net/OleDbDataAdapter

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Updating a DataSource

using System;
using System.Data;
using System.Data.OleDb;

class MainClass
{
  static void Main(string[] args)
  {
    OleDbConnection MyConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source = c:\Northwind.mdb");
    OleDbDataAdapter MyAdapter = new OleDbDataAdapter("SELECT * FROM orders", MyConnection);
    DataSet MyDataSet = new DataSet();
    MyAdapter.Fill(MyDataSet, "orders");
    MyDataSet.Tables[0].Rows[1]["ID"] = "1";
    OleDbCommandBuilder MyBuilder = new OleDbCommandBuilder(MyAdapter);
    MyAdapter.Update(MyDataSet.Tables[0]);
  }
}

Using ADO Managed Provider

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Windows.Forms;
   class ADONetForm1 : Form
   {
      public ADONetForm1()
      {
         InitializeComponent();
         string connectionString = "provider=Microsoft.JET.OLEDB.4.0; data source = c:\\nwind.mdb";
         string commandString = "Select CompanyName, ContactName from Customers";
         OleDbDataAdapter DataAdapter = new OleDbDataAdapter(
         commandString, connectionString );
         DataSet DataSet = new DataSet();
         DataAdapter.Fill( DataSet, "Customers" );
         DataTable dataTable = DataSet.Tables[0];
         // for each row in the table, display the info
         foreach ( DataRow dataRow in dataTable.Rows )
         {
            lbCustomers.Items.Add(
               dataRow["CompanyName"] +
               " (" + dataRow["ContactName"] + ")" );
         }
      }
      private void InitializeComponent()
      {
         this.lbCustomers = new System.Windows.Forms.ListBox();
         this.SuspendLayout();
         this.lbCustomers.FormattingEnabled = true;
         this.lbCustomers.Location = new System.Drawing.Point( 13, 13 );
         this.lbCustomers.Name = "lbCustomers";
         this.lbCustomers.Size = new System.Drawing.Size( 267, 225 );
         this.lbCustomers.TabIndex = 0;
         this.AutoScaleBaseSize = new System.Drawing.Size( 5, 13 );
         this.ClientSize = new System.Drawing.Size( 292, 262 );
         this.Controls.Add( this.lbCustomers );
         this.Name = "ADONetForm1";
         this.Text = "ADO.Net Form 1";
         this.ResumeLayout( false );
      }
      private System.Windows.Forms.ListBox lbCustomers;
      [STAThread]
      static void Main()
      {
         Application.Run( new ADONetForm1() );
      }
   }

Using MultiTabled Datasets

using System;
using System.Data;
using System.Data.OleDb;

class MainClass 
{
  static void Main(string[] args)
  {
    OleDbConnection MyConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source = c:\Northwind.mdb");
    OleDbDataAdapter MyAdapter = new OleDbDataAdapter("SELECT * FROM Orders", MyConnection);
    DataSet MyDataSet = new DataSet();
    MyAdapter.Fill(MyDataSet, "Orders");
    foreach (DataTable MyTable in MyDataSet.Tables)
    {
      foreach (DataColumn MyColumn in MyTable.Columns)
      {
        foreach (DataRow MyRow in MyTable.Rows)
        {
        }
      }
    }
  }
}