尝试将数据插入Microsoft Access数据库

时间:2016-03-10 02:21:35

标签: c# database oledbconnection

我正在尝试使用Windows窗体将数据插入Microsoft Access数据库,但是没有数据插入到数据库中的表中。

这是按钮方法。

private void btnRegister_Click(object sender, EventArgs e)
{
     try
     {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Clinic Inventory System.accdb;
            Persist Security Info=False;";
            conn.Open();
            OleDbCommand command = new OleDbCommand();
            command.CommandText = "INSERT INTO Patient(FirstName,LastName,ContactNumber) VALUES(@fname,@lname,@contactnumber)";
            command.Parameters.AddWithValue("@fname", txtBoxFirstName.Text);
            command.Parameters.AddWithValue("@lname", txtBoxLastName.Text);
            command.Parameters.AddWithValue("@contactnumber", txtBoxContactNumber.Text);
            //command.CommandText = "INSERT INTO Patient (FirstName,LastName,ContactNumber) VALUES('" + txtBoxFirstName.Text + "','" + txtBoxLastName.Text + "','" + txtBoxContactNumber.Text + "')";

            command.Connection = conn;
            command.ExecuteNonQuery();
            conn.Close();

           MessageBox.Show("Patient Registered");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error  " + ex);
            MessageBox.Show("test");
        }
    }

0 个答案:

没有答案