检查数据库表中的值是否为Asp.net C#

时间:2018-06-19 10:37:29

标签: c# asp.net stored-procedures

我有一个存储过程可以计算值在数据库中的次数。但是当它到达时for循环中的代码 tf.layers循环停止并要求为@ invoice2提供参数。

存储过程:

(int)command.ExecuteScalar();

代码背后:

select count(*) as CountInvoice where invoice1=@invoice1 or @invoice2=@invoice`2

1 个答案:

答案 0 :(得分:0)

将这两个参数都考虑在内,这样的工作效果会更好吗?

select count(*) as CountInvoice where invoice1=@invoice1 or invoice2=@invoice2


using (SqlCommand command = new SqlCommand("sp_Count", conn))
{

    foreach (TextBox textBox in placehldr1.Controls.OfType<TextBox>())
    {

        count1 += 1;
        count2 += 1;
        command.CommandType = CommandType.StoredProcedure;
        string invoice = textBox.Text.TrimEnd();
        string parameter1 = string.Format("@invoice1", count1);
        string parameter2 = string.Format("@invoice2", count2);
        command.Parameters.AddWithValue(parameter, invoice);
        int invoiceCount = (int)command.ExecuteScalar();

        if (invoiceCount > 0)
        {

            lblError.Text = "Invoice number already exist";
            return;
        }
        command.Parameters.Clear();
    }