Use OdbcCommand to read data in Access database and fill the DataGrid : Access « Database ADO.net « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Database ADO.net » AccessScreenshots 
Use OdbcCommand to read data in Access database and fill the DataGrid
 

using System;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.Odbc;


public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.DataGrid dataGrid1;
    private System.Windows.Forms.Button button2;
    public Form1() {
        this.button1 = new System.Windows.Forms.Button();
        this.dataGrid1 = new System.Windows.Forms.DataGrid();
        this.button2 = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
        this.SuspendLayout();

        this.button1.Location = new System.Drawing.Point(8240);
        this.button1.Size = new System.Drawing.Size(10432);
        this.button1.Text = "Get Data From Biblio";
        this.button1.Click += new System.EventHandler(this.button1_Click);

        this.dataGrid1.DataMember = "";
        this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
        this.dataGrid1.Location = new System.Drawing.Point(88);
        this.dataGrid1.Size = new System.Drawing.Size(280224);

        this.button2.Location = new System.Drawing.Point(120240);
        this.button2.Size = new System.Drawing.Size(15232);
        this.button2.Text = "Get Data From Access Northwind";
        this.button2.Click += new System.EventHandler(this.button2_Click);

        this.AutoScaleBaseSize = new System.Drawing.Size(513);
        this.ClientSize = new System.Drawing.Size(292273);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.button2,
                                                                          this.dataGrid1,
                                                                          this.button1});
        ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
        this.ResumeLayout(false);

    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }

    private OdbcConnection cn;

    private void button1_Click(object sender, System.EventArgs e) {
        try {
            cn = new OdbcConnection("DRIVER={SQL Server};SERVER=.;uid=admin;pwd=pw;database=biblio");
            cn.Open();
            OdbcCommand sc = new OdbcCommand("SELECT title, price FROM titles WHERE title LIKE 'Hi%'", cn);
            OdbcDataAdapter da = new OdbcDataAdapter(sc);
            DataSet ds = new DataSet("TitlesDS");
            da.Fill(ds);
            dataGrid1.DataSource = ds.Tables[0];
        catch (Exception ex) {
            Debug.WriteLine(ex.ToString());
        finally {
            cn.Close();
        }
    }

    private void button2_Click(object sender, System.EventArgs e) {
        try {
            cn = new OdbcConnection(@"DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\NorthWind.mdb");
            cn.Open();
            OdbcCommand sc = new OdbcCommand("SELECT * from Employees", cn);
            OdbcDataAdapter da = new OdbcDataAdapter(sc);
            DataSet ds = new DataSet("TitlesDS");
            da.Fill(ds);
            dataGrid1.DataSource = ds.Tables[0];
        catch (Exception ex) {
            MessageBox.Show("You probably need to change the file path to NorthWind.MDB used in the OdbcConnection Constructor\n or move a copy of the NorthWind.mdb to c:\\");
            Debug.WriteLine(ex.ToString());
        finally {
            cn.Close();
        }
    }
}

 
Related examples in the same category
1.Connects to a Microsoft Access databaseConnects to a Microsoft Access database
2.Reading Access Data
3.How to use an OleDbConnection object to connect to an Access database
4.Read query result data from Access database
5.Connect to an Access database file
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.