用户登陆界面与数据库表里的数据是否匹配 我写了一个用户登陆界面,然后怎么判断一个用户登录的时候跟数据库表里的用户匹配。走过路过,请各位高手指点指点,不胜感激 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 会用SQL不?首先根据username 去查询数据库,看看是否有记录,如果有再匹配密码等之类的 string str = "server=数据库地址;database=数据库名称;uid=数据库用户名;pwd=数据库密码"; SqlConnection conn = new SqlConnection(str); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "查询语句"; DataSet ds = new DataSet(); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(ds); if (ds.Tables[0].Rows.Count > 0) {登陆成功}elas{登陆失败} string sql = select count(*) from tb where username='"+name+"' and userpwd = '"+pwd+"' ; //然后接受 返回int型、bool型都可以 using(SqlConnection conn= new SqlConnection("")){ conn.Open();SqlCommand command= new SqlCommand("select * from Tb where username='"+name+"'", conn);SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);if(reader.HasRows && reader.Read()){}else{}reader.Dispose();}或用SqlParameter 首先取到你从页面传过的用户名和密码,如String username=request.getparameter("页面传过来的用户名");String password=request.getparameter("页面传过来的用户名密码");你肯定有一个连接数据库的类如你的类为 DBConnection();connection conn=DBConnection().getConnection;String sql="select * from 你的表名 where username="+username+"and password="+password;prepareStatement pas=conn.prepareStatement();result i=pas.execuQuery();if(i.next()){登录成功!}else{登录失败!}当然如果你用的是ssh框架的话!上面代码可简化为:你需要在这个类中传一个vo对象过来,也就是你封装数据库的对像如uservo userconnection conn=DBConnection().getConnection;String sql="select * from 你的表名 where username=?and password=?";prepareStatement pas=conn.prepareStatement();pas.setString(1,user.getUsername);pas.setString(2,user.getPassword);result i=pas.execuQuery();if(i.next()){登录成功!}else{登录失败!}还有其他方法!在这就不说了! 关于textbox控件 向数据库中更新信息问题 下载代码 VS2005的运行环境的问题 (50分)关于StringBuilder.ToString()的怪问题 数码相机编程难题 wince下C#实现音频文件缓冲的问题 这个程序怎么修改???调了很多还是出错,求指教 关于ini如何修改与读取 .net win form 中有能显示windows 默认的文件复制、删除等动画的控件吗? 谁能帮我这段代码简单的注释下,谢谢了。 大家看看我这种情况用什么控件比较容易
首先根据username 去查询数据库,看看是否有记录,如果有再匹配密码等之类的
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "查询语句";
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
登陆成功
}
elas
{
登陆失败
}
string sql = select count(*) from tb where username='"+name+"' and userpwd = '"+pwd+"' ;
//然后接受 返回int型、bool型都可以
{
conn.Open();
SqlCommand command= new SqlCommand("select * from Tb where username='"+name+"'", conn);
SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
if(reader.HasRows && reader.Read())
{}
else
{}
reader.Dispose();
}
或用SqlParameter
String username=request.getparameter("页面传过来的用户名");
String password=request.getparameter("页面传过来的用户名密码");
你肯定有一个连接数据库的类
如你的类为 DBConnection();
connection conn=DBConnection().getConnection;
String sql="select * from 你的表名 where username="+username+"and password="+password;
prepareStatement pas=conn.prepareStatement();
result i=pas.execuQuery();
if(i.next()){
登录成功!
}
else{
登录失败!
}当然如果你用的是ssh框架的话!上面代码可简化为:
你需要在这个类中传一个vo对象过来,也就是你封装数据库的对像
如uservo user
connection conn=DBConnection().getConnection;
String sql="select * from 你的表名 where username=?and password=?";
prepareStatement pas=conn.prepareStatement();
pas.setString(1,user.getUsername);
pas.setString(2,user.getPassword);
result i=pas.execuQuery();
if(i.next()){
登录成功!
}
else{
登录失败!
}还有其他方法!在这就不说了!