首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

如何用OLEDB连接ACCESS

2012-01-15 
怎么用OLEDB连接ACCESS最好写下代码谢谢[解决办法]OleDbConnection connnew OleDbConnection(ProviderM

怎么用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(); 

热点排行