一.显示登陆窗口

  应用程序入口点为Main方法,因此在Main方法中创建登陆窗体。

    1)创建登陆窗体(登陆窗体UI已提前创建好);

    2)显示窗体,以模式对话框的形式显示,并赋值给result;

    3)判断窗体的返回值是否为OK,若是,则显示主窗体,(窗体的对话框结果在相应的窗体中设置,已达到逻辑处理,登陆验证的效果),否则退出程序;

  具体代码如下:

c#桌面应用程序开发--登陆窗口-LMLPHP


二.登陆窗体数据访问方法的编写

  1.准备:

    1)数据访问层DAL创建:解决方案→新建项目→类库;

    2)在DAL中创建管理员数据访问类SysAdminService: DAL→右键→类

    3)编写通用数据访问类:负责连接数据库(最基本的格式化SQL语句通用数据访问类),代码如下

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration; namespace DAL
{
/// <summary>
/// 通用数据访问类
/// </summary>
class SQLHelper
{
private static string connString = Common.StringSecurity.DESDecrypt(ConfigurationManager.ConnectionStrings["connString"].ToString());//于数据库连接的字符串(配置文件解密) /// <summary>
/// 执行增、删、改操作
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int Update(string sql)
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{ throw ex;
}
finally
{
conn.Close();
}
} /// <summary>
/// 执行单一结果查询
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static object GetSingleResult(string sql)
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
return cmd.ExecuteScalar();
}
catch (Exception ex)
{ throw ex;
}
finally
{
conn.Close();
}
} /// <summary>
/// 返回结果集的查询
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static SqlDataReader GetReader(string sql)
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
conn.Close();
throw ex;
}
} }
}

  2.登陆窗体数据访问方法编写:

 using System.Data.SqlClient;
using Models; namespace DAL
{
/// <summary>
/// 管理员数据访问类
/// </summary>
public class SysAdminService
{
/// <summary>
/// 根据账号和密码返回登陆结果的查询,
/// </summary>
/// <param name="objAdmin"></param>
/// <returns>返回管理员对象,若为空,则表示账号或密码错误</returns>
public SysAdmin AdminLogin(SysAdmin objAdmin)
{
string sql = "select AdminName from Admins where LoginId={0} and LoginPwd={1}";
sql = string.Format(sql, objAdmin.LoginId, objAdmin.LoginPwd); SqlDataReader objReader = SQLHelper.GetReader(sql);
if (objReader.Read())//从数据库查到结果,则表示登陆账号和密码正确,将管理员姓名封装到对象中,并返回对象,以便以后修改账号密码使用
{
objAdmin.AdminName = objReader["AdminName"].ToString();
}
else objAdmin = null;//没查到数据,表示登陆不成功,则清空对象
objReader.Close();
return objAdmin;
}
}
}

  3.前台UI逻辑编写(事件+控件)

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Models; namespace StudentManager
{
public partial class FrmUserLogin : Form
{
SysAdminService objAdminService = new SysAdminService(); public FrmUserLogin()
{
InitializeComponent();
} //登录
private void btnLogin_Click(object sender, EventArgs e)
{
//[1]数据验证
if (this.txtLoginId.Text.Trim().Length == )
{
this.lblMsg.Text = "请输入登陆账号!";
return;
}
if (this.txtLoginPwd.Text.Trim().Length == )
{
this.lblMsg.Text = "请输入登陆密码!";
return;
} //[2]封装对象
SysAdmin objAdmin = new SysAdmin()
{
LoginId = Convert.ToInt32(this.txtLoginId.Text.Trim()),
LoginPwd = this.txtLoginPwd.Text.Trim()
};
//[3]和后台交互,判断登陆信息是否正确
try
{
objAdmin = objAdminService.AdminLogin(objAdmin);
if (objAdmin != null)
{
//保存登陆信息
Program.objCurrentAdmin = objAdmin;
this.DialogResult = DialogResult.OK;//this代表当前窗体
this.Close();
}
else
{
this.lblMsg.Text = "账号或密码错误!";
}
}
catch (Exception ex)
{ MessageBox.Show("数据访问出现异常,登陆失败!具体原因:"+ex.Message);
} }
//关闭
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
} #region 改善用户体验
private void txtLoginId_KeyDown(object sender, KeyEventArgs e)
{
        //按回车健代替鼠标单击事件
if(e.KeyValue==)
{
if(this.txtLoginId.Text.Trim().Length != )
{
this.txtLoginPwd.Focus();
}
}
} private void txtLoginPwd_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyValue==)
{
btnLogin_Click(null,null);
}
} #endregion }
}
05-22 02:24