簡體   English   中英

檢查數據庫表Asp.net C#中的值

[英]Check if value in database table Asp.net C#

我有一個存儲過程,該過程將計算一個值在數據庫中的次數。 但是在到達(int)command.ExecuteScalar(); 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();
    }

這樣的事情會更好地起作用,因為它將兩個參數都考慮在內。

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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM