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);
}