我在这段代码中有“必须声明标量变量@BillNo”问题不知道如何解决它。我对 C# 和编程很陌生。我已经评论了其他标量变量以了解但仍然存在问题。当我不使用标量变量但我需要使用它们时插入数据。
private void button1_Click(object sender, EventArgs e)
{
try
{
db = new DBConnection();
String Sqlstr1 = "select UnitPrice from AItem where ItemID = '" + tbItemID.Text + "'";
db.DataRead(Sqlstr1);
int UnitPrice = Convert.ToInt32(db.dr["UnitPrice"]);
int qty = Convert.ToInt32(tbQuantity.Text);
int sum = (UnitPrice * qty);
String fday = Convert.ToString(db.now.AddDays(7));
int billno = DateTime.Now.DayOfYear;
String Sqlstr3 = "insert into Sales (ItemID ,BillNo ,Qty ,Sum, FinalDay) values ('" + tbItemID.Text + "' ,@BillNo,'" + qty + "' ,'" + sum + "' ,'" + fday + "' )";
//db.cmd.Parameters.AddWithValue("@ItemID", tbItemID.Text);
db.cmd.Parameters.AddWithValue("@BillNo" , billno);
//db.cmd.Parameters.AddWithValue("@Qty", tbQuantity.Text);
//db.cmd.Parameters.AddWithValue("@Sum", (UnitPrice * qty));
//db.cmd.Parameters.AddWithValue("@FinalDay", db.now.AddDays(7));
if (db.Execute(Sqlstr3) == 1)
{
MessageBox.Show("added");
}
else
{
MessageBox.Show("something went wrong");
}
}
catch (SqlException m)
{
MessageBox.Show(m.Message);
}
catch (NullReferenceException m)
{
MessageBox.Show(m.Message);
}
catch (InvalidOperationException m)
{
MessageBox.Show(m.Message);
}
}
}