无法将ms-access连接到窗体

时间:2014-03-03 17:09:43

标签: c# database winforms ms-access

在我的窗口应用程序中,我想在ms-access中插入用户名和密码,但是当我尝试将其插入数据库时​​,它会显示如下错误。 enter image description here

这是我的插入数据的代码

public partial class Form1 : Form
    {
        OleDbConnection con;
        OleDbCommand com;
        string path = "Provider=Microsoft.ACE.OLEDB.10.0; Data Source=|DataDirectory|\\insert.accdb;";


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            con = new OleDbConnection(path);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                con = new OleDbConnection(path);   // this is modified
                con.Open();
                string query = "insert into user(username,User_Password) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "')";

                com = new OleDbCommand(query);
                com.Connection = con;

                com.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


    }

0 个答案:

没有答案