简体   繁体   English

C#字符串查询为SQL Server参数放空

[英]c# string query put null for sql server parameter

I know this question was asked many times, but i am not able to find a solution for it 我知道这个问题被问过很多次,但是我找不到解决方案

This is my code 这是我的代码

  string query = @"SELECT *
    FROM SMSMessage
    WHERE (respondCode  IS @respondCode)
and (sentOn > '08/26/2016')
       ";
                //string query = "select * from SMSMessage";
                SqlConnection con = new SqlConnection(Utilities.getConnectionString());
                SqlCommand cmd = new SqlCommand(query, con);
                cmd.Parameters.AddWithValue("@respondCode", DBNull.Value);

I want the responseCode to be null, 我希望responseCode为null,

I am getting error: 我收到错误消息:

syntax error near @responseCode @responseCode附近的语法错误

when I do this responseCode is NULL , there is no syntax error, but the query for some reaonse doesn't bring any result 当我这样做responseCode is NULL ,没有语法错误,但某些原因的查询没有带来任何结果

Help please 请帮助

I guess you want this 我想你要这个

Where (respondCode = @respondCode or @respondCode is null)
  and sentOn > '08/26/2016'

When a value is passed to @respondCode parameter the records will be filter based on @respondCode and sentOn > '08/26/2016' . 当将值传递给@respondCode参数时,将基于@respondCodesentOn > '08/26/2016'过滤记录。

When nothing is passed to @respondCode parameter (ie) NULL , then records will be filtered only based on sentOn > '08/26/2016' 当什么都没有传递给@respondCode参数(即NULL ,将仅基于sentOn > '08/26/2016'过滤记录

As mentioned in comments by Steve, If you need records only when respondCode is NULL then no need of that variable just hardcode the NULL condition in Where clause 如史蒂夫(Steve)的评论中所述,如果仅当respondCodeNULL时才需要记录,则不需要该变量,只需在Where子句中对NULL条件进行硬编码即可。

Where respondCode is null
  and sentOn > '08/26/2016'

I would use directly IS NULL and not passing any parameter, but the most important change is how do you apply the date constant in your query statement. 我将直接使用IS NULL而不传递任何参数,但是最重要的更改是如何在查询语句中应用日期常量。
Assuming you use the Italian locale in your sql server database I would use 假设您在sql服务器数据库中使用意大利语语言环境,我将使用

string query = @"SELECT * SMSMessage
                 WHERE respondCode  IS NULL
                 AND (sentOn > CONVERT(DateTime, '26/08/2016', 105))

T-SQL Convert docs T-SQL转换文档

On the contrary I would look carefully to the value passed for the sentOn condition. 相反,我会仔细查看为sendOn条件传递的值。 If this value changes dynamically it is better to use a parameter for this value. 如果此值动态变化,则最好为该值使用参数。 In this way the query optimizer of sql server will be able to build a better (faster) execution plan 这样,SQL Server的查询优化器将能够构建更好(更快)的执行计划

string query = @"SELECT * SMSMessage
                 WHERE respondCode  IS NULL
                 AND sentOn > @dateLimit";

SqlConnection con = new SqlConnection(Utilities.getConnectionString());
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add("@dateLimit", SqlDbType.DateTime).Value = new DateTime(2016, 8, 26);

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

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