日期:2014-05-18 浏览次数:20785 次
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事件如下:
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; }
------解决方案--------------------
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();