登录界面:
后台代码:
//登录按钮单击事件
private void button1_Click(object sender, EventArgs e)
{
string loginname = _LoginName.Text;
string password = _PassWord.Text;
UserInfo model = new UserInfo();
model = UserInfoManager.GetUserInfoByuserName(loginname); //调用BLL里面的
if (model != null)
{
if (model.PassWord == password)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
this.lb_massge.Text = ";您输入的密码错误!";
}
}
else
{
this.lb_massge.Text = ";您输入的用户名不存在!";
}
}
Bll:
namespace HIS.BLL
{
public class UserInfoManager
{
UserInfoService dal = new UserInfoService();
//根据用户名查询单条用户信息
public static UserInfo GetUserInfoByuserName(string userName)
{
return UserInfoService.GetUserInfoByuserName(userName); //调用DAL里面的
}
}
}
DAL:
///
/// 根据用户名查询
///
///
public static UserInfo GetUserInfoByuserName(string userName)
{
UserInfo userInfo = null;
string sql = "select * from userInfo where userName=@UserName";
SqlParameter[] spr = new SqlParameter[]{
new SqlParameter("@UserName",userName)};
SqlDataReader reader = DBHelper.DBHelper.Reader(sql, spr); //调用DBHelper里面的Reader方法
if (reader.Read())
{
userInfo = new UserInfo();
userInfo.UserId = Convert.ToInt32(reader["UserId"]);
userInfo.UserName = Convert.ToString(reader["UserName"]);
userInfo.PassWord = Convert.ToString(reader["PassWord"]);
userInfo.Email = Convert.ToString(reader["Email"]);
userInfo.ProtectPass = Convert.ToInt32(reader["ProtectPass"]);
}
reader.Close();
HIS.DBHelper.DBHelper.Connection.Close(); //调用DBHelper里面的Connection
return userInfo;
}
DBHelper:
///
/
// 获取全部的信息,带参数
///
///
///
///
public static SqlDataReader Reader(string sql, SqlParameter[] spr)
{
SqlConnection conn = new SqlConnection(connString); //connString链接字符串
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddRange(spr);
SqlDataReader
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
//链接字符串
private static string connString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString(); //获取fig里面的
private static SqlConnection connection = null;
//连接
public static SqlConnection Connection
{
get
{
connection = new SqlConnection(connString);
if (connection.State == ConnectionState.Closed)
connection.Open();
if (connection.State == ConnectionState.Broken)
{
c
onnection.Close();
connection.Open();
}
return connection;
}
}
自己建一个配置文件:
代码2
就是一个简单的跟数据库表中值对照的一个登陆界面。数据库中的用户名是admin,密码是你adm;问题是当均输入正确的时候始终提示密码错误,也就是if (Pass == Password.Text.Trim())这句的内容始终不执行;调试时发现
Pass的值是"adm ",Password.Text的值是"adm".估计是Pass = G_DB.SqlResult["PASSWORD"].ToString()这句的问题,可是代码怎么改。求解!!!!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 输变电设施安全隐患预控管理平台;
namespace 登录
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Username.Focus();
}
}
protected void IBdenglu_Click(object sender, EventArgs e)
{ //用户名
string Name = "";
//密码
string Pass = "";
/
/设置一个中间变量存放用户名和密码
string td = "";
if (Username.Text.Trim() != "" && Password.Text.Trim() != "")
{
//先关闭数据库连接,防止报错
G_DB.SqlConn.Close();
//打开数据库连接
G_DB.SqlConn.Open();
//根据用户输入的用户查询数据库中的用户名和密码
//区分大小写
td = "select USERNAME,PASSWORD from 登录 where username collate Chinese_PRC_CS_AS='" + Username.Text.Trim() + "'";
//执行查询
G_DB.SqlQuery(td);
//当有值时,则返回数据
while (G_DB.SqlResultEof() == false)
{手机登陆界面设计
Name = G_DB.SqlResult["USERNAME"].ToString();
Pass = G_DB.SqlResult["PASSWORD"].ToString();
}
//关闭数据库
G_DB.SqlConn.Close();
//用户名不为空
if (Name != "")
{ //比对数据库中的密码和用户输入的密码
if (Pass == Password.Text.Trim())
{
//Session["username"] = Username.Text.Trim();
lbError.Text=";登录成功";
lbError.Visible = true;
}
else
{
lbError.Text = ";密码错误";
lbError.Visible =true;
}
}
//用户名为空
else
{
lbError.Text =";不存在此用户名";
lbError.Visible =true;
}
}
else
{
lbError.Text =";用户名或密码不能为空";
lbError.Visible =true;
}
}
protected void IBchognzhi_Click(object sender, EventArgs e)
{
Username.Text = "";
Password.Text = "";
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论