简体   繁体   English

记录数不等

[英]Varying number of records

I am executing a SQL Server stored procedure from my C# code which essentially pulls some data from a database based on the supplied condition. 我正在从我的C#代码执行SQL Server存储过程,该过程实际上是根据提供的条件从数据库中提取了一些数据。

DataSet GetAllxxxxxByDate(string entityValue,string companyValue)
{
    using (var sqlConn = new SqlConnection(GetConnectionString()))
    {
        using (var cmd = new SqlCommand())
        {
            var data = new DataSet();
            cmd.CommandText = “myStoredprodecure”;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = sqlConn;

            var eVal = string.IsNullOrWhiteSpace(entityValue) ? string.Empty : entityValue;
            cmd.Parameters.AddWithValue("@entity_value", eVal);

            var company = string.IsNullOrWhiteSpace(companyValue) ? string.Empty : companyValue;
            cmd.Parameters.AddWithValue("@company", company);

            var sqlDataAdaptor = new SqlDataAdapter(cmd);
            sqlDataAdaptor.Fill(data);

            return data;
         }
     }
}

Here entityValue , companyValue are comma separated strings, formed dynamically within C# code and pass it to stored procedure. 在这里, entityValuecompanyValue是逗号分隔的字符串,在C#代码中动态形成并将其传递给存储过程。

Eg: 例如:

’first’,’second’,’third’

And the stored procedure uses these values to fill the NOT IN condition defined within it. 并且存储过程使用这些值来填充其中定义的NOT IN条件。

The issue is that, I am getting inconsistent number of records when I execute the code. 问题是,当我执行代码时,记录数不一致。

Following is a quick screenshot where first WHERE clause return 3 records and second WHERE clause return 1 record. 以下是一个快速屏幕截图,其中第一个WHERE子句返回3条记录,第二个WHERE子句返回1条记录。 The input values for the first WHERE clause is been filled from c# code and the second is been filled manually to test. 第一个WHERE子句的输入值由c#代码填充,第二个WHERE子句的输入值由手动填充以进行测试。

C#执行结果和手动执行结果

The only difference, which I can spot is number of quotes. 我能发现的唯一区别是报价数量。

Question: can someone help me to zero in the issue or the difference in these give WHERE clause ? 问题:有人可以帮助我将问题零或将WHERE子句中的区别归零吗?

Well, you don't show what entity_value is in your results, but the difference between the two is you're adding single quotes around the literal values: 好吧,您没有显示结果中的entity_value ,但是两者之间的区别是您在文字值周围添加了单引号:

N'''FSEC''' in SQL is the literal valiue 'FSEC' SQL中的N'''FSEC'''是字面值'FSEC'

'FSEC' in SQL is just FSEC (without the quotes). SQL中的'FSEC'只是FSEC (不带引号)。

My guess is that records 2004981 and 2004982 have a value of FSEC (without the quotes) for entity_value . 我的猜测是,记录2004981和2004982具有的价值FSEC为(不带引号) entity_value

If you're adding parameter values from C# code, don't add quotes around them like you would if you were building a string. 如果要从C#代码中添加参数值,请不要像在构建字符串时那样在其周围添加引号。 SQL will treat the values as strings without needing string qualifiers. SQL会将值视为字符串,而无需字符串限定符。

EDIT 编辑

Okay, I just read this statement: 好吧,我刚刚读了下面的声明:

Here entityValue, companyValue are comma separated string 在这里,entityValue,companyValue是逗号分隔的字符串

You can't just pass in a comma-delimited string to an IN clause. 您不能只将逗号分隔的字符串传递给IN子句。 To search for multiple values there are a few options: 要搜索多个值,有几个选项:

  • Add commas to each end and use LIKE: 在两端添加逗号并使用LIKE:

     Where (',' + @entity_value +',' LIKE '%,' + entity_value + ',%') 
  • Parse the string into a temporary table, than use that table in your IN clause 将字符串解析为临时表,而不是在IN子句中使用该表

  • Pass the values as a table-valued parameter and use that in your IN clause. 将值作为表值参数传递,并在IN子句中使用它。
  • Build the SQL statement as a string and execute it with EXEC 将SQL语句构建为字符串并使用EXEC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM