如何使用密码和.mdb,accdb数据库

时间:2017-11-08 18:16:17

标签: c#

我想将不同的访问数据库(它们具有相同的表,相同的列)连接到我的系统以生成报告,并且此数据库具有密码。我使用openfileDialog从我的文件中获取此数据库,我想连接.mdb.accdb,但是当我使用Microsoft.jet.oledb.12.0时,我收到错误< / p>

  

microsoft.jet.olebd.12.0提供程序未在本地计算机上注册

这是我的代码:

 private void tileBrowse_Click(object sender, EventArgs e)
 {
        OpenFileDialog fdlg = new OpenFileDialog();
        fdlg.Title = "C# Corner Open file Dialog";
        fdlg.InitialDirectory = @"C:\Desktop";
        fdlg.Filter = "(*.mdb)|*.mdb|(*.accdb)|*accdb";
        //fdlg.Filter = "All Files (*.*)|*.mdb|*.accdb" + "MS-Access Database Files (*.accdb)|*.accdb, (*.mdb)|*.mdb";

        if (fdlg.ShowDialog() == DialogResult.OK)
        {
            txtFilePath.Text = fdlg.FileName;
        }
 }

 private void txtRead_Click(object sender, EventArgs e)
 {
     //connection string 
     String strDSN = "Provider=Microsoft.Jet.OLEDB.12.0;" + "Data Source=" + txtFilePath.Text; 

     try
     {
            //Create a connection and open it 
            OleDbConnection conn = new OleDbConnection(strDSN);
            conn.Open();

            String strSQl = "SELECT * FROM Student;";

            //create data adapter
            OleDbDataAdapter myCmd = new OleDbDataAdapter(strSQl, conn);

            // Create and fill dataset
            DataSet dtSet = new DataSet();
            myCmd.Fill(dtSet);
            DataTable dt = dtSet.Tables[0];
            dataGridView1.DataSource = dtSet.Tables[0];

            //add items to the list box
            listBox1.Items.Add("ID,F_Name,S_Name," + "Age");
            listBox1.Items.Add("=====================================");

            foreach (DataColumn dc in dt.Columns)
            {
                listBox1.Items.Add(dc.ColumnName + "," + dc.DataType + "," + dc.Unique + "," + dc.AutoIncrement + "," + dc.AllowDBNull);
            }

            OleDbCommand cmd = new OleDbCommand("SELECT student.ID, student.F_name, student.S_name, student.Age, Teachers_Info.Teachers_ID, Teachers_Info.Firstname, Teachers_Info.Surname, Teachers_Info.Teachers_Subject, Teachers_Info.Teachers_code FROM student, Teachers_Info ORDER BY student.ID;", conn);
            OleDbDataReader Reader = cmd.ExecuteReader();

            //if (Reader.HasRows)
            if (Reader.HasRows)
            {
                Reader.Read();
                txtID.Text = (Reader["ID"].ToString());
                txtF_name.Text = (Reader["F_name"].ToString());
                txtS_name.Text = (Reader["S_name"].ToString());
                txtAge.Text = (Reader["Age"].ToString());

                txtTeachers_ID.Text = (Reader["Teachers_ID"].ToString());
                txtFirstname.Text = (Reader["Firstname"].ToString());
                txtSurname.Text = (Reader["Surname"].ToString());
                txtTeachers_Subject.Text = (Reader["Teachers_Subject"].ToString());
                txtTeachers_code.Text = (Reader["Teachers_code"].ToString());
            }

            //close connection
            conn.Close();
        }
        catch (Exception exp)
        {
            MessageBox.Show(exp.Message.ToString());
        }
}

0 个答案:

没有答案