wayne9984
wayne9984
2020-05-28 22:16
采纳率: 100%
浏览 151

用c#连接access数据库写登录页面,报错OleDbDataReader dr = com.ExecuteReader(); 标准表达式中数据类型不匹配。

这学期刚学C#,这是作业,技艺不精,恳请格外大佬指点,感激不尽!

access数据库里有uname,upwd,ulevel三个字段,数据类型分别是文本、文本、数字

protected void btnOK_Click(object sender, EventArgs e)
{
if (txtUserName.Text.Trim() == "" | txtPassword.Text.Trim() == "")
{
lblMsg.Text = "用户名及密码不能为空";
return;
}
string ConnStr = ConfigurationManager.ConnectionStrings["Connstring"].ToString();
OleDbConnection conn = new OleDbConnection(ConnStr);
{
conn.Open();
string StrSQL = "select ulevel from Admin where uname='" + txtUserName.Text+ "'and upwd='" + txtPassword.Text + "'";
OleDbCommand com = new OleDbCommand(StrSQL, conn);
OleDbDataReader dr = com.ExecuteReader();//就是这一句报错
dr.Read();
string level;
if (dr.HasRows)
{
level = dr["ulevel"].ToString();
}
else
{
lblMsg.Text = "用户名或密码错";
return;
}
if (level == "0")
{
Session["pass"] = "admin";
Response.Redirect("Admin.aspx");
}
else
{
Session["pass"] = "guest";
Response.Redirect("Guest.aspx");
}
}
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • caozhy
    已采纳

    if (txtUserName.Text.Trim() == "" | txtPassword.Text.Trim() == "")
    这里 ||
    string StrSQL = "select ulevel from Admin where uname='" + txtUserName.Text+ "'and upwd='" + txtPassword.Text + "'";
    这里类型不匹配,你再检查下

    点赞 评论

相关推荐