1、using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;/using System.Data.OleDb;namespace WindowsApplication1public partial class Form1 : Form/1.连接对象OleDbConnection con;public Form1()InitializeComp
2、onent();private void Form1_Load(object sender, EventArgs e)trycon = new OleDbConnection();con.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:students.mdb“;con.Open();/OleDbCommand/2. 数据命令对象 OleDbDataAdapterString s = “select * from students“;OleDbDataAdapter ad = new OleDbDataAda
3、pter(s, con);/3. 表格对象 DataTableDataTable dt = new DataTable();/4. 把数据填充到表格ad.Fill(dt);/5. 显示dataGridView1.DataSource = dt;catch (Exception exp) private void button1_Click(object sender, EventArgs e)private void button2_Click(object sender, EventArgs e)String name = textBox1.Text;try/OleDbCommand/2.
4、数据命令对象 OleDbDataAdapterString s = “select * from students“;if (name != “) s = s + “ where sname like '%“ + name + “%'“;OleDbDataAdapter ad = new OleDbDataAdapter(s, con);/3. 表格对象 DataTableDataTable dt = new DataTable();/4. 把数据填充到表格ad.Fill(dt);/5. 显示dataGridView1.DataSource = dt;catch (Exception exp) MessageBox.Show(exp.Message); private void Form1_FormClosing(object sender, FormClosingEventArgs e)/窗体关闭是执行con.Close();