我想获取IsAdmin值与SQL查询(此查询返回1行或0)。但我得到这个错误{“invalide阅读暂定有没有存在的任何数据。”} 这是我的代码如何在ASP.net中获取数据MVC
public static bool Login (string iduser, string password, bool IsAdmin)
{
bool auth = false;
string query = string.Format("Select IsAdmin from [user] where iduser = '{0}' AND mdp = '{1}' ;", iduser, password);
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader re = cmd.ExecuteReader();
auth = re.HasRows;
if (auth) { IsAdmin = re.GetBoolean(0); } // the error is on this line (this line will alow me to get IsAdmin Value If the user exist)
con.Close();
return auth;
}
也许有人使用SQL Injection攻击了您的网站并删除了所有用户?另外,SqlDataReader是IDisposable。 – 2012-04-02 21:15:12
这不是您的问题的答案,但是您的SQL查询方式很容易受到SQL注入攻击。您应该始终使用参数化查询。 – 2012-04-02 21:16:36
哦,这让我渴望输入像''的密码; drop table [user]; - ' – 2012-04-03 10:44:27