在FROM子句中遇到“语法错误”

时间:2017-10-05 10:19:25

标签: c# ms-access-2010

string type = "Cash";
string type1 = "Card";
if (radioButton1.Checked)
{
    myDB.Open();
    OleDbDataAdapter adapter = new OleDbDataAdapter(@"SELECT * FROM Transaction", myDB);
    adapter.Fill(dt);
    OleDbCommand cmd = new OleDbCommand(@"INSERT INTO Transaction(Type, Amount) Values ('" + type + "', " + label3.Text + ")", myDB);
    cmd.ExecuteNonQuery();
}
else if(radioButton2.Checked)
{
    OleDbDataAdapter adapter = new OleDbDataAdapter(@"SELECT * FROM Transaction", myDB);
    adapter.Fill(dt);
    OleDbCommand cmd = new OleDbCommand(@"INSERT INTO Transaction(Type, Amount) Values ('" + type1 + "', " + label3.Text + ")", myDB);
    cmd.ExecuteNonQuery();
}
myDB.Close();

1 个答案:

答案 0 :(得分:2)

Transaction是一个SQL关键字!你需要把它放在方括号中:

string type = "Cash";
string type1 = "Card";
if (radioButton1.Checked)
{
  myDB.Open();
  OleDbDataAdapter adapter = new OleDbDataAdapter(@"SELECT * FROM [Transaction]", myDB);
  adapter.Fill(dt);
  OleDbCommand cmd = new OleDbCommand(@"INSERT INTO [Transaction](Type, Amount) Values ('" + type + "', " + label3.Text + ")", myDB);
  cmd.ExecuteNonQuery();
}
else if(radioButton2.Checked)
{
  OleDbDataAdapter adapter = new OleDbDataAdapter(@"SELECT * FROM [Transaction]", myDB);
adapter.Fill(dt);
  OleDbCommand cmd = new OleDbCommand(@"INSERT INTO [Transaction](Type, Amount) Values ('" + type1 + "', " + label3.Text + ")", myDB);
  cmd.ExecuteNonQuery();
}
myDB.Close();