【发布时间】:2011-07-19 21:31:43
【问题描述】:
我的代码中出现错误:找不到表 0。我做错了什么?
OdbcCommand cmd = new OdbcCommand("Select * from User where username=? and password=?", cn);
DataSet ds = new DataSet();
//Select the username and password from mysql database in login table
cmd.Parameters.Add("@username", OdbcType.VarChar);
cmd.Parameters["@username"].Value = this.Login1.UserName;
cmd.Parameters.Add("@password", OdbcType.VarChar);
cmd.Parameters["@password"].Value = this.Login1.Password;
//use asp login control to check username and password
//Session["UserID"] = "usrName";
//set the UserID from the User Table unsure how to add this to the sql syntax above
OdbcDataReader dr = default(OdbcDataReader);
// Initialise a reader to read the rows from the login table.
// If row exists, the login is successful
dr = cmd.ExecuteReader();
DataTable dt = ds.Tables[0];
DataRow dp = dt.Rows[0];
if (dt.Rows.Count != 0)
{
Session["UserID"] = Convert.ToString(dp["UserID"]);
e.Authenticated = true;
Response.Redirect("UserProfileWall.aspx");
// Event Authenticate is true forward to user profile
}
}
}
【问题讨论】:
-
不要以纯文本形式存储密码。
-
另外,不要选择 *,只返回你真正需要的。在这种情况下,看起来你应该只是做“选择用户ID ...”
-
您应该在
using语句中处理数据库内容。另外,你不需要初始化dr。 -
我只是想修复错误,我不确定如果有人可以发布一个 asnwer 来纠正我的错误,那将是一种善意的错误!