注册 登录
编程论坛 ASP.NET技术论坛

关于.net+access用户登录问题[求助]

jcrystal 发布于 2007-06-01 23:53, 553 次点击

不管用户名、密码对不对,都提示”用户名或密码不对“。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;

}
}


}

}

3 回复
#2
从小到大2007-06-02 10:16

DataSet ds = new DataSet();
con = new OleDbConnection(strconnection);
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select * from user where id='"+id+"' and [password]='"+password+"'";
cmd.Connection = con;
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(ds);
if(ds.tables[0].rows.count>0)
return true;
else
return false;


不知道这样可以不

#3
GrimFish2007-06-02 12:36
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select count(*) as number from user where id=@id and password=@password";
cmd.Parameters.Add(new OleDbParameter("@id", OleDbType.VarChar, 50));
cmd.Parameters["@id"].Value=Textbox1.Text;
cmd.Parameters.Add(new OleDbParameter("@password", OleDbType.VarChar, 50));
cmd.Parameters["@password"].Value=Textbox2.Text;
OleDbDataReader dr = cmd.ExecuteReader();
if(dr.Read(())
{
Session["AdName"]=dr["id"].ToString();
this.Lab1.Text = "成功登录"; //登陆成功之后,从数据库去相关信息保存在SESSION里,方便其他页面验证。
}
else
{
this.Lab1.Text = "用户名或密码不对";
}
#4
jcrystal2007-06-04 23:31
好像还是不行
1