连接到MS Access

时间:2018-12-20 11:28:11

标签: c# winforms ms-access

我正在尝试将只有一个插入查询的小型C#程序与MS Access数据库连接。我在Internet上跟踪了一些代码,但我做对了。

public partial class Form1 : Form
{
    OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\HP\Desktop\victoriy\Database1.accdb);

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        OleDbCommand cmd = con.CreateCommand();
        con.Open();
        cmd.CommandText = "Insert into Na(FirstName,LastName)Values('" + textBox1.Text + "','" + textBox2.Text + "')";
        cmd.Connection = con;
        cmd.ExecuteNonQuery();
        MessageBox.Show("Record Submitted", "Congrats");
        con.Close();
    }
}

}

0 个答案:

没有答案