Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.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# c在不起作用的条件下_C#_Sql_Sql Server - Fatal编程技术网

C# c在不起作用的条件下

C# c在不起作用的条件下,c#,sql,sql-server,C#,Sql,Sql Server,我没有犯错误 但是列表框显示未过滤的值我想要在时间和结束之间获取值 String start_cd; String end_cd; int time_start_int; int time_end_int; opencon(); SqlCommand res = new SqlCommand("SELECT ID,Available,Type," + start_cd + "," + end_cd + " FROM " + going + " WHER

我没有犯错误 但是列表框显示未过滤的值我想要在时间和结束之间获取值

   String start_cd;
   String end_cd;
   int time_start_int;
   int time_end_int;
    opencon();

     SqlCommand res = new SqlCommand("SELECT ID,Available,Type," + start_cd + "," + end_cd + " FROM " + going + " WHERE " + start_cd + "!=0 or " + end_cd + "!=0 and " + start_cd + " >= " + time_start_int + " and " + start_cd + " <= " + time_end_int + "", con);
    SqlDataAdapter sda_res = new SqlDataAdapter(res);
    DataTable dt_res = new DataTable();
    sda_res.Fill(dt_res);

    listBox1.DataSource=dt_res;
    listBox1.DisplayMember="ID";

    listBox2.DataSource = dt_res;
    listBox2.DisplayMember = start_cd;
您错过了字符串变量的“单引号”。

您需要在单独的表达式中比较time\u start\u int和time\u end\u int与start\u cd,如下所示

SqlCommand res = new SqlCommand("SELECT ID,Available,Type,"'+ start_cd +'","' +
        end_cd +'" FROM going  
       WHERE "'+ start_cd +'"!=0 or "'+ end_cd +'"!=0 and " + 
       time_start_int + " <= "'+ start_cd +'" <= " + time_end_int + "", con);

请记住,使用字符串连接SQL语句会使代码容易受到SQL注入攻击。您可以参考以获得一些关于如何避免SQL注入攻击的提示。

首先,我使用括号表示or,因为and将首先计算,可能会导致删除所有筛选器,在第二部分中,我写时间\u start\u int+人们已经在您前面的问题中告诉您,连接字符串以创建查询是一种老式且有风险的做法。即使您坚持编写带有SQL注入问题的代码,也请花点时间编辑您的示例,这样就不需要滚动,滚动示例以尝试猜测错误是不必要的困难。+0:虽然可能是正确的,但没有解释什么是错误的你做到了,更重要的是为什么。我在“FOT”附近得到了错误的语法。FOT=start\u cdI希望这些变化能为您提供一些关于我所做工作的线索。
SqlCommand res = new SqlCommand("SELECT ID,Available,Type," + start_cd + "," + 
    end_cd + " FROM " + going + 
   " WHERE " + start_cd + "!=0 or " + end_cd + "!=0 and " + 
   time_start_int + " <= " + start_cd + " and " +
   start_cd + " <= " + time_end_int + "", 
   con);
SqlCommand res = new SqlCommand("SELECT ID,Available,Type," + start_cd + "," + 
    end_cd + " FROM " + going + 
   " WHERE (" + start_cd + "!=0 or " + end_cd + "!=0 ) and " + 
   time_start_int + " <= " + start_cd + " and " + start_cd + " <= " + time_end_int + "", con);