不管用户名、密码对不对,都提示”用户名或密码不对“。access数据库为user.mdb
里边的表为user,登录页的代码部分如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.Txtuid.Text = "";
this.Txtpwd.Text = "";
}
}
protected void Btny_Click(object sender, EventArgs e)
{
string uid=this.Txtuid.Text;
string pwd=this.Txtpwd.Text;
if (Validate(uid, pwd))
this.Lab1.Text = "成功登录";
else
this.Lab1.Text = "用户名或密码不对";
}
private bool Validate(string id, string password)
{
string strconnection = "provider=microsoft.jet.oledb.4.0;";
strconnection += @"data source=D:\newlog\App_Data\user.mdb";
OleDbConnection con = null;
try
{
DataSet ds = new DataSet();
con = new OleDbConnection(strconnection);
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select count(*) as number from user where id=? and password=?";
cmd.Parameters.Add("@id", OleDbType.VarChar, 50).Value = id;
cmd.Parameters.Add("@password", OleDbType.VarChar, 50).Value = password;
cmd.Connection = con;
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(ds);
DataRow dr = ds.Tables[0].Rows[0];
int number = Convert.ToInt32(dr["number"].ToString());
if (number == 0)
return false;
else
return true;
}
catch (OleDbException ex)
{
this.Lab1.Text = "错误" + ex.Message;
return false;
}
finally
{
try
{
if (con != null)
con.Close();
}
catch (SqlException ex)
{
this.Lab1.Text = "错误" + ex.Message;
}
}
}
}