C#_會員管理系統:開發三(修改密碼)


為以后多個功能界面考慮,新增一個主界面:

主界面如下:

主界面(VIPMain.cs)詳細代碼如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace 會員管理系統
12 {
13     public partial class VIPMain : Form
14     {
15         public VIPMain()
16         {
17             InitializeComponent();
18         }
19 
20         private void btnVIPManager_Click(object sender, EventArgs e)
21         {
22             VIPManager vm = new VIPManager();
23             vm.Show();
24             this.Hide();
25         }
26 
27         private void btnVIPLogin_Click(object sender, EventArgs e)
28         {
29             VIPLogin vp = new VIPLogin();
30             vp.Show();
31             this.Hide();
32         }
33 
34         private void btnClose_Click(object sender, EventArgs e)
35         {
36             //徹底的退出
37             System.Environment.Exit(0);
38         }
39 
40         private void btnPwdChange_Click(object sender, EventArgs e)
41         {
42             VIPPwdChange vpc = new VIPPwdChange();
43             vpc.Show();
44             this.Hide();
45         }
46     }
47 }

創建個修改密碼的界面:

修改密碼界面如下:

為了方便操作,設置啟用了該窗體的快捷鍵:

修改密碼界面(VIPPwdChange.cs)詳細代碼如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using System.Configuration;
 11 using System.Data.SqlClient;
 12 
 13 namespace 會員管理系統
 14 {
 15     public partial class VIPPwdChange : Form
 16     {
 17         public VIPPwdChange()
 18         {
 19             InitializeComponent();
 20         }
 21 
 22         private void btnBack_Click(object sender, EventArgs e)
 23         {
 24             VIPMain vmain = new VIPMain();
 25             vmain.Show();
 26             this.Hide();
 27         }
 28 
 29         private void btnOk_Click(object sender, EventArgs e)
 30         {
 31             //假如文本框有空的則提示
 32             if (txtName.Text == "" || txtOldPwd.Text == "" || txtNewPwd.Text == "" || txtNewPwdConfirm.Text == "")
 33             {
 34                 MessageBox.Show("信息不完整,請確認完整后再提交!");
 35                 return;
 36             }
 37             //連接數據庫字符串
 38             string connstr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;
 39             //創建連接數據庫對象
 40             SqlConnection conn = new SqlConnection(connstr);
 41             //查詢VipAccount表語句(即查詢密碼)
 42             string sql = string.Format("select vUserPwd from VipAccount where vUserName='" + txtName.Text + "'");
 43             //創建操作數據庫對象
 44             SqlCommand cmd = new SqlCommand(sql,conn);
 45             //打開數據庫
 46             conn.Open();
 47             //創建SqlDataReader對象並執行cmd.ExecuteReader()方法(該方法執行查詢VipAccount表語句)
 48             SqlDataReader sdr = cmd.ExecuteReader();
 49             //判斷是否有下一條數據並把游標定位在下一條數據
 50             if (sdr.Read())
 51             {
 52                 //txtOldPwd文本框的值是否等於sdr的第一列數據
 53                 if (txtOldPwd.Text == sdr.GetString(0).Trim())
 54                 {
 55                     //判斷兩次輸入的新密碼是否一致
 56                     if (txtNewPwd.Text != txtNewPwdConfirm.Text)
 57                     {
 58                         MessageBox.Show("兩次輸入的新密碼不一樣,請重新輸入");
 59                         //清空新密碼文本框
 60                         txtNewPwd.Text = "";
 61                         txtNewPwdConfirm.Text = "";
 62                         return;
 63                     }
 64                     else
 65                     {
 66                         //關閉sdr
 67                         sdr.Close();
 68                         //更新VipAccount表語句
 69                         string sqlupdate =string.Format( "update VipAccount set vUserPwd='{0}' where vUserName='{1}'",txtNewPwd.Text,txtName.Text);
 70                         //創建操作數據庫對象
 71                         SqlCommand cmdup = new SqlCommand(sqlupdate,conn);
 72                         //執行cmdup.ExecuteNonQuery()方法(該方法執行更新VipAccount表語句),並判斷返回的值是否為0
 73                         if (cmdup.ExecuteNonQuery() == 0)
 74                         {
 75                             MessageBox.Show("未知異常");
 76                             return;
 77                         }
 78                         else
 79                         {
 80                             MessageBox.Show("密碼修改成功");
 81                         }
 82                     }
 83                 }
 84                 else
 85                 {
 86                     MessageBox.Show("舊密碼錯誤,或者不能為空");
 87                     txtOldPwd.Text = "";
 88                     txtNewPwd.Text = "";
 89                     txtNewPwdConfirm.Text = "";
 90                     return;
 91                 }
 92             }
 93             //關閉數據庫連接
 94             conn.Close();
 95         }
 96 
 97         //設置快捷鍵
 98         private void VIPPwdChange_KeyDown(object sender, KeyEventArgs e)
 99         {
100             //F5確定
101             if(e.KeyCode==Keys.F5)
102             {
103                 btnOk_Click(null,null);
104             }
105             //ESC返回
106             else if (e.KeyCode == Keys.Escape)
107             {
108                 btnBack_Click(null,null);
109             }
110         }
111 
112         //當登錄窗體為活動窗體時
113         private void VIPPwdChange_Activated(object sender, EventArgs e)
114         {
115             //設置文本框txtName獲得焦點
116             txtName.Focus();
117         }
118 
119         //獲取文本框txtName的快捷鍵
120         private void txtName_KeyUp(object sender, KeyEventArgs e)
121         {
122             if (e.KeyCode == Keys.Enter)
123             {
124                 txtOldPwd.Focus();
125                 txtOldPwd.SelectAll();
126             }
127         }
128         //獲取文本框txtOldPwd的快捷鍵
129         private void txtOldPwd_KeyUp(object sender, KeyEventArgs e)
130         {
131             if (e.KeyCode == Keys.Enter)
132             {
133                 txtNewPwd.Focus();
134                 txtNewPwd.SelectAll();
135             }
136         }
137         //獲取文本框txtNewPwd的快捷鍵
138         private void txtNewPwd_KeyUp(object sender, KeyEventArgs e)
139         {
140             if (e.KeyCode == Keys.Enter)
141             {
142                 txtNewPwdConfirm.Focus();
143                 txtNewPwdConfirm.SelectAll();
144             }
145         }
146         //獲取文本框txtNewPwdConfirm的快捷鍵
147         private void txtNewPwdConfirm_KeyUp(object sender, KeyEventArgs e)
148         {
149             if (e.KeyCode == Keys.Enter)
150             {
151                 btnOk_Click(null, null);
152             }
153         }
154 
155 
156     }
157 }

登錄界面也增加了快捷鍵操作:

登錄界面(VIPLogin.cs)詳細代碼如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Configuration;
  5 using System.Data;
  6 using System.Drawing;
  7 using System.Linq;
  8 using System.Text;
  9 using System.Threading.Tasks;
 10 using System.Windows.Forms;
 11 using System.Data.SqlClient;
 12 
 13 namespace 會員管理系統
 14 {
 15     public partial class VIPLogin : Form
 16     {
 17         public VIPLogin()
 18         {
 19             InitializeComponent();
 20         }
 21         //用於連接配置文件App.config
 22         string connStr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;
 23         //登錄按鈕
 24         private void btnLogin_Click(object sender, EventArgs e)
 25         {
 26             //連接數據庫語句
 27             using(SqlConnection con=new SqlConnection(connStr))
 28             {
 29                 //操作數據庫語句
 30                 string sql = "select vuserpwd from vipaccount where vUserName='" + txtName.Text + "'";
 31                 using(SqlCommand cmd=new SqlCommand(sql,con))
 32                 {
 33                     //打開數據庫
 34                     con.Open();
 35                     //使用 SqlDataReader 來 讀取數據庫
 36                     using (SqlDataReader sdr = cmd.ExecuteReader())
 37                     {
 38                         //SqlDataReader 在數據庫中為 從第1條數據開始 一條一條往下讀
 39                         if (sdr.Read()) //如果讀取賬戶成功(文本框中的用戶名在數據庫中存在)
 40                         {
 41                             //則將第1條 密碼 賦給 字符串pwd  ,並且依次往后讀取 所有的密碼
 42                             //Trim()方法為移除字符串前后的空白
 43                             string pwd = sdr.GetString(0).Trim();
 44                             //如果 文本框中輸入的密碼 ==數據庫中的密碼
 45                             if (pwd == txtPwd.Text)
 46                             {
 47                                 //說明在該賬戶下 密碼正確, 系統登錄成功
 48                                 MessageBox.Show("登錄成功,正在進入主界面......");
 49                                 //***************新增代碼***************
 50                                 //創建新的會員資料管理界面窗體並顯示,同時把登錄界面隱藏
 51                                 //VIPManager vm=new VIPManager();
 52                                 VIPMain vmain = new VIPMain();
 53                                 vmain.Show();
 54                                 this.Hide();
 55                                 //***************新增代碼***************
 56                             }
 57                             else
 58                             {
 59                                 //密碼錯誤
 60                                 MessageBox.Show("密碼錯誤,請重新輸入");
 61                                 txtPwd.Text = "";
 62                             }
 63                         }
 64                         else
 65                         {
 66                             //用戶名錯誤
 67                             MessageBox.Show("用戶名錯誤,請重新輸入!");
 68                             txtName.Text = "";
 69                         }
 70                     }
 71                 }
 72             }
 73         }
 74         
 75         //設置快捷鍵
 76         private void VIPLogin_KeyDown(object sender, KeyEventArgs e)
 77         {
 78             //如果按下ESC鍵
 79             if (e.KeyCode == Keys.Escape)
 80             {
 81                 //關閉窗體
 82                 this.Close();
 83             }
 84             //如果按下F5鍵
 85             else if (e.KeyCode == Keys.F5)
 86             {
 87                 //調用登錄按鈕單擊事件
 88                 this.btnLogin_Click(null,null);
 89             }
 90         }
 91 
 92         //關閉
 93         private void btnClose_Click(object sender, EventArgs e)
 94         {
 95             //徹底的退出
 96             System.Environment.Exit(0);
 97         }
 98 
 99         //當登錄窗體為活動窗體時
100         private void VIPLogin_Activated(object sender, EventArgs e)
101         {
102             //設置文本框txtName獲得焦點
103             txtName.Focus();
104         }
105 
106         //獲取文本框txtName的快捷鍵
107         private void txtName_KeyUp(object sender, KeyEventArgs e)
108         {
109             if (e.KeyCode == Keys.Enter)
110             {
111                 txtPwd.Focus();
112                 txtPwd.SelectAll();
113             }
114         }
115 
116         //獲取文本框txtPwd的快捷鍵
117         private void txtPwd_KeyUp(object sender, KeyEventArgs e)
118         {
119             if (e.KeyCode == Keys.Enter)
120             {
121                 btnLogin_Click(null, null);
122             }
123         }
124     }
125 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM