我正在尝试为会计应用程序发出 C# 中的插入语句,但我偶然遇到了一些问题,说它在第 88 行有一个错误,恰好落在这里
private void button2_Click(object sender, EventArgs e)
{
using (SqlConnection cn = new SqlConnection(constring))
{
cn.Open();
if (choice.Text == "DEPOSIT")
{
double newAccBal = Convert.ToDouble(opening_amount.Text) + Convert.ToDouble(amount.Text);
string newBal = newAccBal.ToString();
string sql = "insert into credit (fullname,accountNo,opening_amount,amount,desc,newBal) values (@fullname,@accountNo,@opening_amount,@amount,@desc,@newBal)";
using (SqlCommand cmd = new SqlCommand(sql, cn))
{
cmd.Parameters.AddWithValue("@fullname", fullname.Text);
cmd.Parameters.AddWithValue("@accountNo", textBox3.Text);
cmd.Parameters.AddWithValue("@opening_amount", opening_amount.Text);
cmd.Parameters.AddWithValue("@amount", amount.Text);
cmd.Parameters.AddWithValue("@desc", desc.Text);
cmd.Parameters.AddWithValue("@newBal", newBal);
try
{
var msg = MessageBox.Show("Information to be Sent for Deposit" + Environment.NewLine + "Please Confirm to Continue?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (msg == DialogResult.Yes)
{
cmd.ExecuteNonQuery(); <------------------------------ This Area
string confirmation = "Full Name : '"+fullname.Text+"' "+Environment.NewLine+" Depositing Amount : '"+amount.Text+"' "+Environment.NewLine+" Description : '"+desc.Text+"' "+Environment.NewLine+" New Balance : '"+newBal+"'";
MessageBox.Show("Deposit Successful" + Environment.NewLine + "Information has been Saved for Records" + Environment.NewLine + "Confirmation is as follows" + Environment.NewLine + confirmation ,"Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
string sql2 = "update account_info set opening_amount = '"+newBal+"' where id='"+id.Text+"'";
using (SqlCommand cmd2 = new SqlCommand(sql2, cn))
{
cmd2.ExecuteNonQuery();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
}
说 desc 附近的语法不正确并指向那行 cmd.ExecuteNonQuery()
,我错过了什么?
DESC 是 SQL 中的保留字,是降序的缩写,用于 ORDER BY 子句。在 SQL 语句中将其括在方括号中。
您将其中一列命名为 desc
。但是,这是一个保留的词。您必须选择不同的列名称(可能是一个明智的选择),或者使用一种方法来确保 DBMS 识别您的意思是您的列,而不是保留字。您可以使用[
和]
来批注名称:[desc]
应该有效。