日期:2014-05-18  浏览次数:20785 次

怎么用OLEDB连接ACCESS
最好写下代码 谢谢

------解决方案--------------------
OleDbConnection conn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source="+Server.MapPath("XXX.mdb")); 

OleDbDataAdapter mydata=new OleDbDataAdapter(_sql,conn); 

conn.Open()

DataSet ds=new DataSet(); 
mydata.Fill(ds,); 

其他的和mssql查不多的吧
------解决方案--------------------
C# code
private OleDbConnection GetOleDbConnection()
{
    string connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = " + mdbFilename + "; User ID = Admin; Jet OLEDB:Database Password = tcgl200412; Persist Security Info=True; Mode = ReadWrite;";

    OleDbConnection oleDbConn = null;
    try
    {
        oleDbConn = new OleDbConnection(connStr);
        oleDbConn.Open();
        return oleDbConn;
    }
    catch (Exception err)
    {
        MessageBox.Show("连接数据库失败: " + err.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        return null;
    }
}

------解决方案--------------------
加上相应的命名空间 using System.Data.OleDb;

button1的Click事件如下:

C# code

  
        private void button1_Click(object sender, EventArgs e)
        {
            string filepath = "E:\\test.mdb"; 
            string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath;
            OleDbConnection conn = new OleDbConnection(connectionString);
            OleDbDataAdapter da = new OleDbDataAdapter("Select * from 表", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt.DefaultView;
        }

------解决方案--------------------
C# code

            string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=数据库.mdb;Persist Security Info=True;password='密码'";
            OleDbConnection con = new OleDbConnection(strCon);

            con.Open();
            string strSql = "Select * From 表名";
            OleDbCommand command = new OleDbCommand(strSql, con);
            OleDbDataAdapter dap = new OleDbDataAdapter(command);
            DataSet ds = new DataSet();
            dap.Fill(ds);
            con.Close();