从c#到Access DB的INSERT语句中出现语法错误

从c#到Access DB的INSERT语句中出现语法错误,c#,database,ms-access,C#,Database,Ms Access,当我尝试插入时,会出现一些错误(insert语句中的语法错误) 我正在尝试不同的方法来处理参数或直接值 plz 转义保留关键字user 使用参数化查询 避免sql注入 使用一次性物品 尝试以下方法: con.Open(); cmd = new OleDbCommand("insert into login (user, password) values ('" +textBox1 .Text + "','" + textBox2 .Text + "');",con);

当我尝试插入时,会出现一些错误(insert语句中的语法错误) 我正在尝试不同的方法来处理参数或直接值 plz

  • 转义保留关键字
    user
  • 使用参数化查询
  • 避免sql注入
  • 使用一次性物品
尝试以下方法:

 con.Open();
        cmd = new OleDbCommand("insert into login (user, password) values ('" +textBox1 .Text + "','" + textBox2 .Text + "');",con);
        cmd.CommandType = CommandType.Text;
        int temp = cmd.ExecuteNonQuery();
        if (temp > 0)
        {
           textBox1.Text = null;
            textBox2.Text = null;
            MessageBox.Show("Record Successfuly Added");
        }
        else
        {
            MessageBox.Show("Record Fail to Added");
        }
        con.Close();

我认为这是因为像
Login
user
password
这样的关键词,试图在它们周围放上方括号
[]
,然后再次执行相同的查询。正如@M.Ali所指出的,旁注:1)使用
textBox1.Text=string.Empty
而不是
textBox1.Text=null
。2) 您可以在查询执行后立即关闭连接(
int temp=cmd.ExecuteNonQuery();con.close();
)。它现在可以正常工作,但当我打开数据库文件时,我看不到任何信息。我该怎么办?我是这个的新用户site@Dev.Mahmood检查这个
using (OleDbConnection con = new OleDbConnection(connectionString))
{
    con.Open();
    using (var cmd = new SqlCommand(
    "insert into login ([user], [password]) values (@user, @pass);",
    con))
    {
        cmd.Parameters.Add(new OleDbParameter("@user", textBox1.Text ));
        cmd.Parameters.Add(new OleDbParameter("@pass", textBox1.Text ));

        if (temp > 0)
        {
            textBox1.Text = String.Empty;
            textBox2.Text = String.Empty;
            MessageBox.Show("Record Successfuly Added");
        }
        else
        {
            MessageBox.Show("Record Fail to Added");
        }
    }
}