private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
using (OleDbConnection conn = new OleDbConnection(Con))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand("UPDATE ACTB SET FirstName = @FirstName, LastName = @LastName, GrossIncome = @GrossIncome, LessTNT = @LessTNT, TaxableIncomeCE = @TCI, " +
"TaxableIncomePE = @ADDTI, GrossTaxableIncome = @GTI, LessTE = @LessTE, LessPPH = @LessPPH, NetTax = @LessNTI," +
"TaxDue = @TD, HeldTaxCE = @TWCE, HeldTaxPE = @TWPE, TotalTax = @TATW WHERE ID = @ID", conn);
// OleDbCommand cmd = new OleDbCommand("UPDATE ACTB SET (FirstName, LastName, GrossIncome, LessTNT, TaxableIncomeCE, TaxableIncomePE, GrossTaxableIncome, LessTE, LessPPH, NetTax, TaxDue, HeldTaxCE, HeldTaxPE, TotalTax) = " +
// "VALUES(@FirstName, @LastName, @GrossIncome, @LessTNT, @TCI, @ADDTI, @GTI, @LessTE, @LessPPH, @LessNTI, @TD, @TWCE, @TWPE, @TATW)", conn);
cmd.Parameters.AddWithValue("@ID", txtID.Text);
cmd.Parameters.AddWithValue("@FirstName", txtFirst.Text);
cmd.Parameters.AddWithValue("@LastName", txtLast.Text);
cmd.Parameters.AddWithValue("@GrossIncome", Convert.ToDouble(txtGross.Text));
cmd.Parameters.AddWithValue("@LessTNT", Convert.ToDouble(txtLessTNT.Text));
cmd.Parameters.AddWithValue("@TCI", Convert.ToDouble(txtTCI.Text));
cmd.Parameters.AddWithValue("@ADDTI", Convert.ToDouble(txtADDTI.Text));
cmd.Parameters.AddWithValue("@GTI", Convert.ToDouble(txtGTI.Text));
cmd.Parameters.AddWithValue("@LessTE", Convert.ToDouble(txtLessTE.Text));
cmd.Parameters.AddWithValue("@LessPPH", Convert.ToDouble(txtLessPPH.Text));
cmd.Parameters.AddWithValue("@LessNTI", Convert.ToDouble(txtLessNTI.Text));
cmd.Parameters.AddWithValue("@TD", Convert.ToDouble(txtTD.Text));
cmd.Parameters.AddWithValue("@TWCE", Convert.ToDouble(txtTWCE.Text));
cmd.Parameters.AddWithValue("@TWPE", Convert.ToDouble(txtTWPE.Text));
cmd.Parameters.AddWithValue("@TATW", Convert.ToDouble(txtTATW.Text));
cmd.ExecuteNonQuery();
conn.Close();
}
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
lblSuccess.Text = "Successfully updated the records!";
Restore();
}
}
一直在找错了,但我找不到。它不会更新Restore()
处理btw的表。没有错误。
虽然它确实有一个“连接字符串未正确初始化”,并且更新语句错误更早。
答案 0 :(得分:1)
OleDbCommand
没有Name值参数的概念。所以你必须以与查询相同的顺序定义它们。
所以第一个参数应该是
cmd.Parameters.AddWithValue("@FirstName", txtFirst.Text);
cmd.Parameters.AddWithValue("@LastName", txtLast.Text);
//and so on ...
//last one
cmd.Parameters.AddWithValue("@ID", txtID.Text);
此外,您可以使用@
在多行上书写字符串。
P.S使用时无需关闭()连接,这由Dispose()