//首先引入
using System.Data;
using System.Data.OleDb;
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=F:\tem\green_for_test.mdb"); //然后实例化数据库连接对象,其中Data Source=后面是你access数据库的路径
OleDbDataAdapter Adapter = new OleDbDataAdapter("SELECT * FROM access_database", con); //要查询的话就实例化OleDbDataAdapter对象,里面两个参数分别是你的sql语句,上面实例化的数据库连接对象con,OleDbDataAdapter对象会自动管理连接对象的关闭
DataTable table = new DataTable(); //然后定义datatable对象,用于存放查询结果
Adapter.Fill(table); //通过Adapter 对象的Fill方法执行查询,同时将结果放入table中
datagridview.DataSource = table; //然后就是将table绑定到datagridview中
//完成,希望能帮到你
你这么问?你会什么呀?应该买本书看看,最基本的问题也这样问不太好的!
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
///
/// condb 的摘要说明
///
public class DBHelper
{
private OleDbConnection conn;
private OleDbDataAdapter oda = new OleDbDataAdapter();
private OleDbCommand cmd;
private DataSet myds = new DataSet();
public DBHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + @"/db/dbtest.mdb");
}
public DataSet getDS(string strSQL)
{
myds = new DataSet();
oda = new OleDbDataAdapter(strSQL, conn);
oda.Fill(myds);
return myds;
}
public DataSet getDS2(string strSQL, int si, int mi)
{
conn.Open();
myds = new DataSet();
oda = new OleDbDataAdapter(strSQL, conn);
oda.Fill(myds, si, mi, "tab1");
conn.Close();
return myds;
}
public bool setDS(string strSQL)
{
conn.Open();
cmd = new OleDbCommand(strSQL, conn);
cmd.ExecuteNonQuery();
conn.Close();
return true;
}
}
}
上面是数据库连接,下面是绑定数据
datagriveiw.source= getDS2("你的sql语句").Tables[0].DefaultView;
datagriveiw.bind() ;