连接字符串vs2015和access 2016

时间:2017-04-23 09:28:05

标签: c# visual-studio ms-access

我试图将带有两个表的数据库(* .accdb)连接到C#windows窗体应用程序。 有2个表单,一个用于登录验证,第二个表单用于读写数据库, 第一种形式与连接完美配合,但第二种形式与连接有问题

第一种形式是:

namespace DatabaseManagment
{

    public partial class Form1 : Form
    {
        private OleDbConnection connection = new OleDbConnection();
        public Form1()
        {
            InitializeComponent();
            connection.ConnectionString = @"provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Emad\Documents\login.accdb";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                connection.Open();
                CheckConnection.Text = "connection successfull";
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("error" + ex);
            }
        }

和第二种形式是:

   private void btn_save_Click(object sender, EventArgs e)
    {
        try
        {
            connectionstr.Open();
            OleDbCommand commandwrite = new OleDbCommand();
            commandwrite.Connection = connectionstr;
            commandwrite.CommandText = "insert into DB(reactor,class,measure,validity) values ('" + txt_reactorcore.Text + "','" + txt_class.Text + "','" + txt_unitofmeasure.Text + "','" + txt_validitycriteria.Text + "')";
            commandwrite.ExecuteNonQuery();
            MessageBox.Show("saved");
            connectionstr.Close();
        }
        catch (Exception ex)
        {
           MessageBox.Show("error" + ex);
        }

    }

并且错误是: errorsytem.data.oledb.oledbexeption(0x8004005)

0 个答案:

没有答案