



下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、C#實現(xiàn)用戶登錄功能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 MrCy.BaseClass;/引入文件夾BaseClassusing System.Data.SqlClient;/引入連接數(shù)據(jù)庫所需的類namespace MrCy public partial class fr
2、mLogin : Form public frmLogin() InitializeComponent(); / <summary> / 用戶點擊“登錄”觸發(fā)的事件 / </summary> / <param name="sender"></param> / <param name="e"></param> private void btnLogin_Click(object sender, EventArgs e) try string UserName = txtUserName
3、.Text.Trim();/獲取用戶名 string PassWord = txtPwd.Text.Trim();/獲取密碼 if (UserName = "")/如果用戶名為空值 MessageBox.Show("請輸入用戶名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);/彈出消息對話框 else if (PassWord = "")/如果密碼為空值 MessageBox.Show("請輸入密碼", "提示&qu
4、ot;, MessageBoxButtons.OK, MessageBoxIcon.Information);/彈出消息對話框 else SqlConnection conn = DBconn.Connection();/創(chuàng)建數(shù)據(jù)庫連接對象 conn.Open();/打開數(shù)據(jù)庫連接 string str = "select count(*) from tb_User where UserName='" + UserName + "' and UserPwd='" + PassWord + "'"/建立查
5、詢數(shù)據(jù)庫中tb_User表的用戶名和對應(yīng)密碼的SQL字符串 SqlCommand cmd = new SqlCommand(str, conn);/創(chuàng)建命令對象; /SqlDataReader sdr = cmd.ExecuteReader();/創(chuàng)建數(shù)據(jù)讀取器對象 /sdr.Read();/讀取數(shù)據(jù) int n = (int)cmd.ExecuteScalar(); if (n >= 1)/判斷是否有匹配的用戶名和密碼 string str1 = "select * from tb_User where UserName='" + UserName + &q
6、uot;'"/根據(jù)用戶名查詢tb_User表中數(shù)據(jù) cmd = new SqlCommand(str1, conn);/創(chuàng)建命令對象; SqlDataReader sdr = cmd.ExecuteReader();/創(chuàng)建數(shù)據(jù)讀取器對象 sdr.Read();/讀取數(shù)據(jù) string UserPower = sdr"Power".ToString().Trim();/獲取數(shù)據(jù)庫中tb_User表的Power字段 sdr.Close();/關(guān)閉數(shù)據(jù)讀取器 conn.Close();/關(guān)閉數(shù)據(jù)庫連接 frmMain Main = new frmMain();/
7、建立主窗體對象 Main.Uname = UserName;/將用戶名賦值給frmMain窗體的Uname對象 Main.Utime = DateTime.Now.ToString();/獲取登錄時間并賦值給主窗體的Utime對象 Main.Upower = UserPower;/將用戶權(quán)限賦值給主窗體的對象Upower Main.Show();/顯示frmMain窗體 this.Hide();/隱藏當前登錄窗體 else MessageBox.Show("用戶名或密碼錯誤", "警告", MessageBoxButtons.OK, MessageBox
8、Icon.Warning);/彈出警告提示框 catch (Exception ex)/拋出異常 MessageBox.Show(ex.Message);/彈出消息對話框 / <summary> / 單擊“取消”按鈕,清空文本框內(nèi)容 / </summary> / <param name="sender"></param> / <param name="e"></param> private void btnCancel_Click(object sender, EventArgs e
9、) /txtUserName.Text = "" /txtPwd.Text = "" if (MessageBox.Show("確定退出本系統(tǒng)嗎?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) = DialogResult.OK)/如果點擊了消息對話框中的“確定” Application.Exit();/退出應(yīng)用程序 / <summary> / 當用戶輸入用戶名之后判斷是否按了“Enter”鍵,按了的話就激發(fā)“登錄”按鈕的
10、Click事件 / </summary> / <param name="sender"></param> / <param name="e"></param> private void txtUserName_KeyPress(object sender, KeyPressEventArgs e) if (e.KeyChar = 13) btnLogin_Click(sender, e); / <summary> / 當用戶輸完密碼,判斷用戶是否按了“Enter”鍵,按了就調(diào)用登錄事件 / </summary> / <param name="sender"></param>
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 破產(chǎn)管理人信息化管理計劃
- 2025小學數(shù)學學生評價體系心得體會
- 四年級數(shù)學作業(yè)輔導復習計劃
- 2025年度地鐵消防培訓計劃
- 小學生國慶詩歌朗誦配樂范文
- 2024年特殊教育第一學期教學工作計劃
- 高三第二學期課堂教學計劃
- 初三班主任校際交流計劃
- 兒科創(chuàng)傷危重癥搶救流程
- 以康??h幼兒園為例探究教學活動中隨機教育的實踐與優(yōu)化
- 全屋定制培訓
- 《提高團隊戰(zhàn)斗力》課件
- 神州數(shù)碼行測題
- 數(shù)字化賦能小學語文中段習作教學的有效策略探究
- 解讀-刑法修正案十一
- 《贊美技巧》課件
- 老年人炎癥性腸病發(fā)病機制的研究進展與干細胞治療
- 醫(yī)療責任組長競聘
- 流浪未成年人救助保護中心建設(shè)標準
- 2024年中國燈影牛肉市場調(diào)查研究報告
- 2024年高中生物學業(yè)水平合格考及答案
評論
0/150
提交評論