c#桌面应用程序开发--登陆窗口
生活随笔
收集整理的這篇文章主要介紹了
c#桌面应用程序开发--登陆窗口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一.顯示登陸窗口
應用程序入口點為Main方法,因此在Main方法中創建登陸窗體。
1)創建登陸窗體(登陸窗體UI已提前創建好);
2)顯示窗體,以模式對話框的形式顯示,并賦值給result;
3)判斷窗體的返回值是否為OK,若是,則顯示主窗體,(窗體的對話框結果在相應的窗體中設置,已達到邏輯處理,登陸驗證的效果),否則退出程序;
具體代碼如下:
二.登陸窗體數據訪問方法的編寫
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 }
}
總結
以上是生活随笔為你收集整理的c#桌面应用程序开发--登陆窗口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 农村丧古的打法
- 下一篇: 有持枪证可以随身带枪吗