Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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# da.Fill(dt1)中条件表达式中的数据类型不匹配_C# - Fatal编程技术网

C# da.Fill(dt1)中条件表达式中的数据类型不匹配

C# da.Fill(dt1)中条件表达式中的数据类型不匹配,c#,C#,为什么我会收到这个错误 条件表达式中的数据类型不匹配 在线da.Fill(dt1) 始终使用sql参数而不是字符串连接来提供参数。否则,很容易发生sql注入和转换错误 DataTable dt1 = new DataTable(); using(var da = new OleDbDataAdapter("SELECT * FROM MainData where ID=@ID", connection)) { da.SelectCommand.Parameters.Add("@ID",

为什么我会收到这个错误

条件表达式中的数据类型不匹配

在线
da.Fill(dt1)


始终使用sql参数而不是字符串连接来提供参数。否则,很容易发生sql注入和转换错误

DataTable dt1 = new DataTable();
using(var da = new OleDbDataAdapter("SELECT * FROM MainData where ID=@ID", connection))
{
    da.SelectCommand.Parameters.Add("@ID", OleDbType.SmallInt).Value = short.Parse(BestScoreID);
    // no need to open/close the connection with dataadapter.Fill:
    da.Fill(dt1);
}

ID
列的类型是什么?听起来像是数字。如果是,请删除单引号。作为一个更好的方法,使用。这种类型的字符串连接对攻击是开放的。并使用
using
语句来处理您的连接、命令和适配器。
DataTable dt1 = new DataTable();
using(var da = new OleDbDataAdapter("SELECT * FROM MainData where ID=@ID", connection))
{
    da.SelectCommand.Parameters.Add("@ID", OleDbType.SmallInt).Value = short.Parse(BestScoreID);
    // no need to open/close the connection with dataadapter.Fill:
    da.Fill(dt1);
}