我有一个存储过程,可以计算一个值在数据库中的次数。然而,在 for 循环后面的代码中,当它到达循环时会
(int)command.ExecuteScalar();停止并要求为 @invoice2 提供参数。
存储过程:
select count(*) as CountInvoice where invoice1=@invoice1 or @invoice2=@invoice`2
代码背后:
using (SqlCommand command = new SqlCommand("sp_Count", conn))
{
foreach (TextBox textBox in placehldr1.Controls.OfType<TextBox>())
{
count += 1;
command.CommandType = CommandType.StoredProcedure;
string invoice = textBox.Text.TrimEnd();
string parameter = string.Format("@invoice{0}", count);
command.Parameters.AddWithValue(parameter, invoice);
int invoiceCount = (int)command.ExecuteScalar();
if (invoiceCount > 0)
{
lblError.Text = "Invoice number already exist";
return;
}
command.Parameters.Clear();
}