Csharp/CSharp Tutorial/ADO.Net/MySql — различия между версиями

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

Версия 15:31, 26 мая 2010

ODBC MySqlConnection

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Data.Odbc;
public class MainClass : System.Windows.Forms.Form
{
  private System.Windows.Forms.DataGrid dataGrid1;
  public MainClass()
  {
    InitializeComponent();
  }
  private void InitializeComponent()
  {
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    ((System.ruponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.SuspendLayout();
    // 
    // dataGrid1
    // 
    this.dataGrid1.DataMember = "";
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid1.Location = new System.Drawing.Point(0, 8);
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.Size = new System.Drawing.Size(376, 288);
    this.dataGrid1.TabIndex = 0;
    // 
    // MainClass
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(384, 302);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                    this.dataGrid1});
    this.Load += new System.EventHandler(this.MainClass_Load);
    ((System.ruponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new MainClass());
  }
  private void MainClass_Load(object sender, System.EventArgs e)
  {
    string connectionString = @"Driver={MySQL};SERVER=localhost;DATABASE=NorthwindMySQL;";
    OdbcConnection conn= new OdbcConnection(connectionString);
    conn.Open();
    OdbcDataAdapter da = new OdbcDataAdapter ("SELECT CustomerID, ContactName, ContactTitle FROM Customers", conn);            
    DataSet ds = new DataSet("Cust");     
    da.Fill(ds, "Customers");
    dataGrid1.DataSource = ds.DefaultViewManager;
    conn.Close();
  }
}

Use OdbcDataAdapter to read data from ODBC MySql connection

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Data.Odbc;
public class MainClass : System.Windows.Forms.Form
{
  private System.Windows.Forms.DataGrid dataGrid1;
  public MainClass()
  {
    InitializeComponent();
  }
  private void InitializeComponent()
  {
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    ((System.ruponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.SuspendLayout();
    // 
    // dataGrid1
    // 
    this.dataGrid1.DataMember = "";
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid1.Location = new System.Drawing.Point(0, 8);
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.Size = new System.Drawing.Size(376, 288);
    this.dataGrid1.TabIndex = 0;
    // 
    // MainClass
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(384, 302);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                    this.dataGrid1});
    this.Load += new System.EventHandler(this.MainClass_Load);
    ((System.ruponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new MainClass());
  }
  private void MainClass_Load(object sender, System.EventArgs e)
  {
    string connectionString = @"Driver={MySQL};SERVER=localhost;DATABASE=NorthwindMySQL;";
    OdbcConnection conn= new OdbcConnection(connectionString);
    conn.Open();
    OdbcDataAdapter da = new OdbcDataAdapter ("SELECT CustomerID, ContactName, ContactTitle FROM Customers", conn);            
    DataSet ds = new DataSet("Cust");     
    da.Fill(ds, "Customers");
    dataGrid1.DataSource = ds.DefaultViewManager;
    conn.Close();
  }
}