标题:关于.net+access用户登录问题[求助]
只看楼主
jcrystal
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-6-1
 问题点数:0 回复次数:3 
关于.net+access用户登录问题[求助]

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

}
}


}

}

搜索更多相关主题的帖子: using System access 数据库 Web 
2007-06-01 23:53
从小到大
Rank: 1
等 级:新手上路
威 望:2
帖 子:200
专家分:0
注 册:2006-6-24
得分:0 

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;


不知道这样可以不


2007-06-02 10:16
GrimFish
Rank: 1
等 级:新手上路
威 望:1
帖 子:167
专家分:0
注 册:2007-5-28
得分:0 
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 = "用户名或密码不对";
}
2007-06-02 12:36
jcrystal
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-6-1
得分:0 
好像还是不行

2007-06-04 23:31



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-144189-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.695478 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved