C#實現(xiàn)用戶登錄功能_第1頁
C#實現(xiàn)用戶登錄功能_第2頁
C#實現(xiàn)用戶登錄功能_第3頁
全文預覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、.C#實現(xiàn)用戶登錄功能usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingMrCy.BaseClass; / 引入文件夾 BaseClassusingSystem.Data.SqlClient;/ 引入連接數(shù)據(jù)庫所需的類namespace MrCypublicpartialclassfrmLogin:Fo

2、rmpublicfrmLogin()InitializeComponent();/ <summary>/ 用戶點擊 “登錄 ”觸發(fā)的事件/ </summary>/ <param name="sender"></param>/ <param name="e"></param>privatevoid btnLogin_Click(objectsender,EventArgs e)trystringUserName = txtUserName.Text.Trim();/ 獲取用戶名stri

3、ngPassWord = txtPwd.Text.Trim();/ 獲取密碼if(UserName ="" ) / 如果用戶名為空值MessageBox.Show( " 請輸入用戶名" ," 提示 " ,MessageBoxButtons .OK,MessageBoxIcon.Information);/ 彈出消息對話框elseif(PassWord ="" ) / 如果密碼為空值MessageBox.Show( " 請輸入密碼 " ," 提示 " ,MessageBoxBu

4、ttons .OK,MessageBoxIcon.Information);/ 彈出消息對話框1 / 4.elseSqlConnectionconn =DBconn.Connection();/ 創(chuàng)建數(shù)據(jù)庫連接對象conn.Open();/ 打開數(shù)據(jù)庫連接stringstr ="select count(*) from tb_User where UserName='"+ UserName +"' and UserPwd='"+ PassWord +"'" ; / 建立查詢數(shù)據(jù)庫中tb_User 表的用

5、戶名和對應密碼的SQL字符串SqlCommandcmd =new SqlCommand(str, conn);/ 創(chuàng)建命令對象 ;/SqlDataReader sdr = cmd.ExecuteReader();/創(chuàng)建數(shù)據(jù)讀取器對象/sdr.Read();/讀取數(shù)據(jù)intn = (int )cmd.ExecuteScalar();if(n >= 1)/ 判斷是否有匹配的用戶名和密碼stringstr1= "select* from tb_User where UserName='" + UserName+ "'" ; /根據(jù)用戶名查

6、詢tb_User 表中數(shù)據(jù)cmd =new SqlCommand(str1, conn);/ 創(chuàng)建命令對象 ;SqlDataReader sdr = cmd.ExecuteReader();/ 創(chuàng)建數(shù)據(jù)讀取器對象sdr.Read();/ 讀取數(shù)據(jù)stringUserPower = 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 (); / 建立主窗體對象Main.Uname

7、= UserName;/ 將用戶名賦值給frmMain 窗體的 Uname對象Main.Utime =DateTime.Now.ToString();/ 獲取登錄時間并賦值給主窗體的Utime 對象Main.Upower = UserPower;/ 將用戶權(quán)限賦值給主窗體的對象UpowerMain.Show();/ 顯示 frmMain 窗體this .Hide();/ 隱藏當前登錄窗體elseMessageBox.Show( " 用戶名或密碼錯誤" ," 警告 " ,MessageBoxButtons .OK,MessageBoxIcon.Warnin

8、g);/ 彈出警告提示框catch ( Exceptionex) / 拋出異常MessageBox.Show(ex.Message);/ 彈出消息對話框2 / 4./ <summary>/ 單擊 “取消 ”按鈕,清空文本框內(nèi)容/ </summary>/ <param name="sender"></param>/ <param name="e"></param>privatevoid btnCancel_Click(objectsender,EventArgs e)/txtUserN

9、ame.Text = ""/txtPwd.Text = ""if( MessageBox.Show( " 確定退出本系統(tǒng)嗎?" ," 提示 " ,MessageBoxButtons .OKCancel,MessageBoxIcon.Exclamation) =DialogResult.OK) / 如果點擊了消息對話框中的“確定 ”Application.Exit();/ 退出應用程序/ <summary>/當用戶輸入用戶名之后判斷是否按了“Enter ”鍵,按了的話就激發(fā)“登錄 ”按鈕的 Click 事

10、件/ </summary>/ <param name="sender"></param>/ <param name="e"></param>privatevoid txtUserName_KeyPress(objectsender,KeyPressEventArgse)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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論