无法从Visual Studio C#插入Access数据库中(insert into语句出错)

时间:2019-02-06 11:06:28

标签: c ms-access

我尝试将方括号[]放入,将值作为参数传递并使用@,但它始终告诉我INSERT INTO语句出错。请帮助这是一个项目。 旁注:我已经添加了数据集并检查了它们的数据绑定,一切看起来都很好。

public partial class Form2 : Form
{
    OleDbConnection Connect = new OleDbConnection();
    public Form2()
    {
        InitializeComponent();
        Connect.ConnectionString = @"Provider=Microsoft Office 12.0 Access Database Engine OLE DB Provider;Data Source=C:\Users\chich\Documents\Visual Studio 2017\Projects\my project\Proper_Database.accdb; Persist Security Info=False;";
    }


    private void button1_Click(object sender, EventArgs e)
        { try
        {
            Connect.Open();
            OleDbCommand Cmd = new OleDbCommand();
            Cmd.Connection = Connect;

            Cmd.CommandText = "insert into User ([Username], [Password], [Firstname], [Lastname]) values('" + usernameTextBox1.Text+ "','" + passwordTextBox1.Text + "','" + firstnameTextBox.Text + "','" + lastnameTextBox.Text + "')";
            Cmd.ExecuteNonQuery();

            MessageBox.Show("It went");
            Connect.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show("An Error occured " + ex);
        }


    Form3 forms = new Form3();
        forms.Show();



    }

谢谢

0 个答案:

没有答案