2017-10-05 98 views
-1
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(); 
+2

欢迎来到Stack Overflow。请阅读[问]以及如何创建[mcve]。 –

回答

2

事务是一个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(); 
+0

完成:对不起,花了5分钟才得到格式正确的答案 – Whismer

+0

这好多了,我们都花时间习惯了格式化 –