Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.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
sql字符串(c#)出错_C#_Sql_String - Fatal编程技术网

sql字符串(c#)出错

sql字符串(c#)出错,c#,sql,string,C#,Sql,String,我写了以下脚本(用c#): 当我运行我的程序时,我得到了这样的结果:“criteria Expersion中的数据类型不匹配”。 当然,matchPlayer的数据类型是“text” 那剧本怎么了 谢谢 你忘了引号了。 使用参数化查询是一种很好的做法 string sqlCommand = "SELECT * " + "FROM tblMatches " + "WHERE matchPlayerNick ='

我写了以下脚本(用c#):

当我运行我的程序时,我得到了这样的结果:“criteria Expersion中的数据类型不匹配”。 当然,matchPlayer的数据类型是“text”

那剧本怎么了


谢谢

你忘了引号了。 使用参数化查询是一种很好的做法

string     sqlCommand = "SELECT   * " +
                 "FROM     tblMatches " +
                 "WHERE matchPlayerNick ='" + comboBoxPlayer.Text + "' " +
                 "ORDER BY matchName ";
但是上面的查询在使用
sql注入时易受攻击。如果使用
命令对象和参数
对值进行参数化,则可以防止出现这种情况

请尝试以下代码段:

string content = comboBoxPlayer.Text;
string connStr = "connection string here";
string sqlCommand = @"SELECT   *
                      FROM     tblMatches 
                      WHERE matchPlayerNick = @content
                      ORDER BY matchName";
using (SqlConnection conn = new SqlConnection(connStr))
{
    using(SqlCommand comm = new SqlCommand())
    {
        comm.Connection = conn;
        comm.CommandText = sqlStatement;
        comm.CommandType = CommandType.Text;

        comm.Parameters.AddWithValue("@content", content);

        try
        {
            conn.Open();
            // other codes here
        }
        catch(SqlException e)
        {
            // do something with the exception
            // do not hide it
            // e.Message.ToString()
        }
    }
}
为了正确编码

  • 使用
    使用
    语句处理propr对象
  • 使用
    try catch
    块正确处理对象

这方面有很多问题。请谷歌sql注入。。。但是您要寻找的是文本字段周围缺少单引号。您应该查看参数化查询。注入攻击,注入攻击,注入攻击。如果我键入
;删除TBL匹配项;转到组合框中的
,您可能会遇到问题。@i不客气。即使您传递的值包含单引号,它也不会打断字符串。
string content = comboBoxPlayer.Text;
string connStr = "connection string here";
string sqlCommand = @"SELECT   *
                      FROM     tblMatches 
                      WHERE matchPlayerNick = @content
                      ORDER BY matchName";
using (SqlConnection conn = new SqlConnection(connStr))
{
    using(SqlCommand comm = new SqlCommand())
    {
        comm.Connection = conn;
        comm.CommandText = sqlStatement;
        comm.CommandType = CommandType.Text;

        comm.Parameters.AddWithValue("@content", content);

        try
        {
            conn.Open();
            // other codes here
        }
        catch(SqlException e)
        {
            // do something with the exception
            // do not hide it
            // e.Message.ToString()
        }
    }
}