Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查数据库表Asp.net C中的值是否为#_C#_Asp.net_Stored Procedures - Fatal编程技术网

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

C# 检查数据库表Asp.net C中的值是否为#,c#,asp.net,stored-procedures,C#,Asp.net,Stored Procedures,我有一个存储过程,可以计算一个值在数据库中的次数。但是,在代码隐藏for循环中,当它到达 (int)命令。ExecuteScalar()循环停止并要求为@invoice2提供参数 存储过程: select count(*) as CountInvoice where invoice1=@invoice1 or @invoice2=@invoice`2 代码隐藏: using (SqlCommand command = new SqlCommand("sp_Count", conn)) {

我有一个存储过程,可以计算一个值在数据库中的次数。但是,在代码隐藏for循环中,当它到达
(int)命令。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();
    }
使用(SqlCommand命令=新的SqlCommand(“sp_Count”,conn))
{
foreach(TextBox TextBox在placehldr1.Controls.OfType()中)
{
计数+=1;
command.CommandType=CommandType.storedProcess;
字符串invoice=textBox.Text.TrimEnd();
字符串参数=string.Format(“@invoice{0}”,count);
command.Parameters.AddWithValue(参数,发票);
int invoiceCount=(int)command.ExecuteScalar();
如果(发票计数>0)
{
lblError.Text=“发票编号已存在”;
返回;
}
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();
    }
选择count(*)作为CountInvoice,其中invoice1=@invoice1或invoice2=@invoice2
使用(SqlCommand=newsqlcommand(“sp_Count”,conn))
{
foreach(TextBox TextBox在placehldr1.Controls.OfType()中)
{
count1+=1;
count2+=1;
command.CommandType=CommandType.storedProcess;
字符串invoice=textBox.Text.TrimEnd();
字符串参数1=string.Format(“@invoice1”,count1);
字符串参数2=string.Format(“@invoice2”,count2);
command.Parameters.AddWithValue(参数,发票);
int invoiceCount=(int)command.ExecuteScalar();
如果(发票计数>0)
{
lblError.Text=“发票编号已存在”;
返回;
}
command.Parameters.Clear();
}

请将存储过程和整个代码放在此处请提供更多详细信息如果您执行脚本时一次只提供一个参数,您的脚本将定义它所需的两个参数。那么如何提供第二个参数呢parameter@JᴀʏMᴇᴇ; 要消除每次运行一个参数的情况,需要做些什么