登錄界面(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 //定義一個全局變量 Uid; 24 //用於獲取登錄成功后的用戶名 25 public static string uid; 26 //定義一個全局變量 time; 27 //用於獲取用戶登錄時的時間 28 public static DateTime time; 29 //定義一個全局變量situation 30 //用於獲取用戶的登錄狀態 31 public static string situation; 32 //定義一個全局變量UserType 33 //用於獲取用戶權限 34 public static string UserType; 35 36 //登錄按鈕 37 private void btnLogin_Click(object sender, EventArgs e) 38 { 39 //連接數據庫語句 40 using(SqlConnection con=new SqlConnection(connStr)) 41 { 42 //操作數據庫語句 43 string sql = "select vuserpwd,usertype from vipaccount where vUserName='" + txtName.Text + "'"; 44 using(SqlCommand cmd=new SqlCommand(sql,con)) 45 { 46 //打開數據庫 47 con.Open(); 48 //使用 SqlDataReader 來 讀取數據庫 49 using (SqlDataReader sdr = cmd.ExecuteReader()) 50 { 51 //SqlDataReader 在數據庫中為 從第1條數據開始 一條一條往下讀 52 if (sdr.Read()) //如果讀取賬戶成功(文本框中的用戶名在數據庫中存在) 53 { 54 //則將第1條 密碼 賦給 字符串pwd ,並且依次往后讀取 所有的密碼 55 //Trim()方法為移除字符串前后的空白 56 string pwd = sdr.GetString(0).Trim(); 57 //讀取器sdr獲取了2列數據 第1列為密碼 第2列 即索引為1的是用戶類型 58 string uType = sdr.GetString(1).Trim() ; 59 //如果 文本框中輸入的密碼 ==數據庫中的密碼 60 if (pwd == txtPwd.Text) 61 { 62 uid = txtName.Text; 63 time = DateTime.Now; 64 situation = "登錄"; 65 //將登錄成功的用戶類型 賦給全局變量UserType 66 //用於獲取當前登錄 用戶的類型 67 UserType = uType; 68 //說明在該賬戶下 密碼正確, 系統登錄成功 69 MessageBox.Show("登錄成功,正在進入主界面......"); 70 //***************新增代碼*************** 71 VIPLog vl = new VIPLog(); 72 //添加當前的用戶信息到日志中 73 vl.AddMsg(); 74 //退出程序 75 //創建新的會員資料管理界面窗體並顯示,同時把登錄界面隱藏 76 //VIPManager vm=new VIPManager(); 77 VIPMain vmain = new VIPMain(); 78 vmain.Show(); 79 this.Hide(); 80 //***************新增代碼*************** 81 } 82 else 83 { 84 //密碼錯誤 85 MessageBox.Show("密碼錯誤,請重新輸入"); 86 txtPwd.Text = ""; 87 } 88 } 89 else 90 { 91 //用戶名錯誤 92 MessageBox.Show("用戶名錯誤,請重新輸入!"); 93 txtName.Text = ""; 94 } 95 } 96 } 97 } 98 } 99 100 //設置快捷鍵 101 private void VIPLogin_KeyDown(object sender, KeyEventArgs e) 102 { 103 //如果按下ESC鍵 104 if (e.KeyCode == Keys.Escape) 105 { 106 //關閉窗體 107 this.Close(); 108 } 109 //如果按下F5鍵 110 else if (e.KeyCode == Keys.F5) 111 { 112 //調用登錄按鈕單擊事件 113 this.btnLogin_Click(null,null); 114 } 115 } 116 117 //關閉 118 private void btnClose_Click(object sender, EventArgs e) 119 { 120 //徹底的退出 121 System.Environment.Exit(0); 122 } 123 124 //當登錄窗體為活動窗體時 125 private void VIPLogin_Activated(object sender, EventArgs e) 126 { 127 //設置文本框txtName獲得焦點 128 txtName.Focus(); 129 } 130 131 //獲取文本框txtName的快捷鍵 132 private void txtName_KeyUp(object sender, KeyEventArgs e) 133 { 134 if (e.KeyCode == Keys.Enter) 135 { 136 txtPwd.Focus(); 137 txtPwd.SelectAll(); 138 } 139 } 140 141 //獲取文本框txtPwd的快捷鍵 142 private void txtPwd_KeyUp(object sender, KeyEventArgs e) 143 { 144 if (e.KeyCode == Keys.Enter) 145 { 146 btnLogin_Click(null, null); 147 } 148 } 149 } 150 }
主界面(VIPMain.cs)新增一個Label控件:
詳細代碼如下:
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 VIPLog vipl = new VIPLog(); 30 vipl.GetExitTime(); 31 vipl.AddMsg(); 32 VIPLogin vp = new VIPLogin(); 33 vp.Show(); 34 this.Hide(); 35 } 36 37 private void btnClose_Click(object sender, EventArgs e) 38 { 39 VIPLog vipl = new VIPLog(); 40 vipl.GetExitTime(); 41 vipl.AddMsg(); 42 //徹底的退出 43 System.Environment.Exit(0); 44 } 45 46 private void btnPwdChange_Click(object sender, EventArgs e) 47 { 48 VIPPwdChange vpc = new VIPPwdChange(); 49 vpc.Show(); 50 this.Hide(); 51 } 52 53 private void VIPMain_Load(object sender, EventArgs e) 54 { 55 lblCurrentUser.Text = "當前登錄用戶為:"+VIPLogin.uid+ " 用戶類型:" + VIPLogin.UserType + " 登錄時間為:"+VIPLogin.time; 56 //給當前用戶打招呼 57 //這里通過獲取當前用戶電腦上的時間 以及判斷登錄用戶的類型 來給不同類型的用戶打招呼 58 // 定義整型變量 intTime 來獲取 用戶電腦上的具體小時數 然后在如下進行判斷 59 int inttime = VIPLogin.time.Hour; 60 //獲取VIPLogin窗體的全局變量 UserType 用戶類型, 用於給不同類型的用戶打招呼 61 string uType = VIPLogin.UserType; 62 //在凌晨0-6點的時候 63 if (inttime >= 0 && inttime < 6) 64 { 65 if (uType == "Administrator") 66 { 67 lblSayHi.Text = "尊敬的"+ VIPLogin.uid + "您好,現在已夜深,請注意休息!"; 68 } 69 else if (uType == "NormalUser") 70 { 71 lblSayHi.Text = "親愛的" + VIPLogin.uid + "您好,現在已夜深,請注意休息!"; 72 } 73 } 74 //早上6點-中午12點的時候 75 else if (inttime >= 6 && inttime < 12) 76 { 77 if (uType == "Administrator") 78 { 79 lblSayHi.Text = "尊敬的" + VIPLogin.uid + "早上好!"; 80 } 81 else if (uType == "NormalUser") 82 { 83 lblSayHi.Text = "親愛的" + VIPLogin.uid + "早上好!"; 84 } 85 } 86 //中午12點-下午6點的時候 87 else if (inttime >= 12 && inttime < 18) 88 { 89 if (uType == "Administrator") 90 { 91 lblSayHi.Text = "尊敬的" + VIPLogin.uid + "下午好!"; 92 } 93 else if (uType == "NormalUser") 94 { 95 lblSayHi.Text = "親愛的" + VIPLogin.uid + "下午好!"; 96 } 97 } 98 //晚上 99 else if (inttime >= 18 && inttime < 24) 100 { 101 if (uType == "Administrator") 102 { 103 lblSayHi.Text = "尊敬的" + VIPLogin.uid + "晚上好!"; 104 } 105 else if (uType == "NormalUser") 106 { 107 lblSayHi.Text = "親愛的" + VIPLogin.uid + "晚上好!"; 108 } 109 } 110 //否則 默認為 111 else 112 { 113 lblSayHi.Text = "歡迎使用會員管理系統!"; 114 } 115 //判斷用戶類型 並給用戶設置功能權限 116 if (uType == "NormalUser") 117 { 118 btnRegistration.Enabled = false; 119 btnLog.Enabled = false; 120 } 121 } 122 123 private void btnLog_Click(object sender, EventArgs e) 124 { 125 VIPLog vl=new VIPLog(); 126 vl.Show(); 127 this.Hide(); 128 } 129 130 private void btnRegistration_Click(object sender, EventArgs e) 131 { 132 VIPRegistration vrn = new VIPRegistration(); 133 vrn.Show(); 134 this.Hide(); 135 } 136 } 137 }
會員資料管理(VIPManager.cs)詳細代碼:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Configuration; 5 using System.Data; 6 using System.Data.SqlClient; 7 using System.Drawing; 8 using System.Linq; 9 using System.Text; 10 using System.Threading.Tasks; 11 using System.Windows.Forms; 12 13 namespace 會員管理系統 14 { 15 public partial class VIPManager : Form 16 { 17 public VIPManager() 18 { 19 InitializeComponent(); 20 } 21 22 //連接字符串 獲取配置文件里的連接路徑,多次需要調用,放在外面方便 23 static string connStr = ConfigurationManager.ConnectionStrings["str"].ConnectionString; 24 //窗體運行自動加載 25 private void VipManager_Load(object sender, EventArgs e) 26 { 27 //刷新數據 28 Refresh(); 29 cmbforfieldSelecting.Text = "全局搜索"; 30 cmbforfieldSelecting.Items.Add("全局搜索"); 31 cmbforfieldSelecting.Items.Add("編號"); 32 cmbforfieldSelecting.Items.Add("名字"); 33 cmbforfieldSelecting.Items.Add("性別"); 34 cmbforfieldSelecting.Items.Add("年齡"); 35 cmbforfieldSelecting.Items.Add("地址"); 36 cmbforfieldSelecting.Items.Add("電話"); 37 //添加對用戶 類型的判斷 用來設置功能按鈕的使用權限 38 if (VIPLogin.UserType == "NormalUser") 39 { 40 btnAdd.Enabled = false; 41 btnDelete.Enabled = false; 42 btnSave.Enabled = false; 43 } 44 } 45 46 //寫一個刷新數據的方法(跟查看數據一樣) 47 public void Refresh(bool isAdded = false) 48 { 49 //查詢數據庫字符串 50 string sql = String.Format("select vId '{0}',vName '{1}',vGender '{2}',vAge '{3}',vAddress '{4}',vPhone '{5}' from VipInformation", "編號", "名字", "性別", "年齡", "地址", "電話"); 51 //連接數據庫對象 52 SqlConnection conn = new SqlConnection(connStr); 53 //操作數據庫對象 54 SqlCommand cmd = new SqlCommand(sql, conn); 55 //創建表對象 56 System.Data.DataTable dt = new System.Data.DataTable(); 57 //創建數據庫填充操作對象(語句) 58 SqlDataAdapter sda = new SqlDataAdapter(cmd); 59 //把數據填充進dt表中 60 sda.Fill(dt); 61 //指定dgvManager控件的數據源:dt 62 dgvManager.DataSource = dt; 63 64 //if (isAdded) 65 //{ 66 // if (dt.Rows.Count > 0) 67 // dgvManager.Rows[0].Selected = false; 68 // dgvManager.Rows[dt.Rows.Count - 1].Selected = true; 69 //} 70 } 71 72 //刷新數據界面 73 private void btnView_Click(object sender, EventArgs e) 74 { 75 //刷新數據 76 Refresh(); 77 } 78 79 //添加數據 80 private void btnAdd_Click(object sender, EventArgs e) 81 { 82 //判斷文本框是否為空,提示數據完整性 83 if (txtName.Text == "" || txtGender.Text == "" || txtAge.Text == "" || txtAddress.Text == "" || txtPhone.Text == "") 84 { 85 MessageBox.Show("數據不能為空,請填寫齊全"); 86 return; 87 } 88 //插入數據庫字符串 89 string sql = string.Format("insert into VipInformation values('{0}','{1}',{2},'{3}','{4}')",txtName.Text.Trim(),txtGender.Text.Trim(),txtAge.Text.Trim(),txtAddress.Text.Trim(),txtPhone.Text.Trim()); 90 //連接數據庫對象 91 SqlConnection conn = new SqlConnection(connStr); 92 //操作數據庫對象 93 SqlCommand cmd = new SqlCommand(sql, conn); 94 //創建表對象 95 System.Data.DataTable dt = new DataTable(); 96 //創建數據庫填充操作對象(語句) 97 SqlDataAdapter sda = new SqlDataAdapter(cmd); 98 //把數據填充進dt表中 99 sda.Fill(dt); 100 //指定dgvManager控件的數據源:dt 101 dgvManager.DataSource = dt; 102 //刷新數據 103 Refresh(); 104 } 105 106 //刪除數據 107 private void btnDelete_Click(object sender, EventArgs e) 108 { 109 //使用sql刪除語句,where 1=1 就是沒有條件,等於全部數據刪除 110 string sql = "delete from VipInformation where 1=1"; 111 //如果選中某行則執行 112 if (dgvManager.CurrentRow.Selected) 113 { 114 sql = sql + " and vid=" + Convert.ToInt32(dgvManager.CurrentRow.Cells[0].Value.ToString()); 115 } 116 int n = 0; 117 //創建連接數據庫對象 118 SqlConnection conn = new SqlConnection(connStr); 119 //創建操作數據庫對象 120 SqlCommand cmd = new SqlCommand(sql, conn); 121 //打開數據庫 122 conn.Open(); 123 //取得ExecuteNonQuery返回的受影響行數,無影響則為0 124 n = cmd.ExecuteNonQuery(); 125 if (n == 0) 126 { 127 MessageBox.Show("刪除操作失敗!不存在的ID"); 128 conn.Close(); 129 return; 130 } 131 else if (n > 0) 132 { 133 MessageBox.Show("刪除操作成功!"); 134 } 135 //關閉數據庫連接 136 conn.Close(); 137 //刷新數據界面 138 Refresh(); 139 } 140 141 //修改數據 142 private void btnSave_Click(object sender, EventArgs e) 143 { 144 if (txtName.Text == "" || txtGender.Text == "" || txtAge.Text == "" || txtAddress.Text == "" || txtPhone.Text == "") 145 { 146 MessageBox.Show("所提供的數據不完整,請填寫完整數據"); 147 return; 148 } 149 int n = 0; 150 //更新SQL語句 151 string sqlupdate = "update VipInformation set vName='" + txtName.Text + "',vgender='" + txtGender.Text + "',vage=" + txtAge.Text + ",vaddress='" + txtAddress.Text + "',vphone='" + txtPhone.Text + "' where vid='" + dgvManager.CurrentRow.Cells[0].Value.ToString() + "'"; 152 SqlConnection conn = new SqlConnection(connStr); 153 SqlCommand cmd = new SqlCommand(sqlupdate, conn); 154 conn.Open(); 155 n = cmd.ExecuteNonQuery(); 156 if (n == 0) 157 { 158 MessageBox.Show("修改操作失敗!"); 159 conn.Close(); 160 return; 161 } 162 else if (n > 0) 163 { 164 MessageBox.Show("修改操作成功!"); 165 } 166 conn.Close(); 167 Refresh(); 168 } 169 170 //點擊dgvManager在文本框上顯示 171 private void dgvManager_CellContentClick(object sender, DataGridViewCellEventArgs e) 172 { 173 txtName.Text = dgvManager.CurrentRow.Cells[1].Value.ToString(); 174 txtGender.Text = dgvManager.CurrentRow.Cells[2].Value.ToString(); 175 txtAge.Text = dgvManager.CurrentRow.Cells[3].Value.ToString(); 176 txtAddress.Text = dgvManager.CurrentRow.Cells[4].Value.ToString(); 177 txtPhone.Text = dgvManager.CurrentRow.Cells[5].Value.ToString(); 178 } 179 180 string selectedValue; 181 //事件索引改變時觸發 182 private void cmbforfieldSelecting_SelectedIndexChanged(object sender, EventArgs e) 183 { 184 string strSelected = cmbforfieldSelecting.Text; 185 switch (strSelected) 186 { 187 case "全局搜索": 188 selectedValue = "全局搜索"; 189 break; 190 case "編號": 191 selectedValue="vid"; 192 break; 193 case "名字": 194 selectedValue = "vname"; 195 break; 196 case "性別": 197 selectedValue = "vgender"; 198 break; 199 case "年齡": 200 selectedValue = "vage"; 201 break; 202 case "地址": 203 selectedValue = "vaddress"; 204 break; 205 case "電話": 206 selectedValue = "vphone"; 207 break; 208 default: 209 selectedValue = "全局搜索"; 210 break; 211 } 212 } 213 214 private void txtDataforQuery_TextChanged(object sender, EventArgs e) 215 { 216 string sql = ""; 217 if (txtDataforQuery.Text.Trim() == "") 218 { 219 //執行查詢語句 220 sql = "select * from VipInformation"; 221 } 222 else if (cmbforfieldSelecting.Text.Trim() == "全局搜索" || selectedValue == "全局搜索") 223 { 224 //全字段搜索 225 sql = "select * from VipInformation where vName like '%" + txtDataforQuery.Text.Trim() + "%' or vgender like '%" + txtDataforQuery.Text.Trim() + "%' or vage like '%" + txtDataforQuery.Text.Trim() + "%' or vaddress like '%" + txtDataforQuery.Text.Trim() + "%' or vphone like '%" + txtDataforQuery.Text.Trim() + "%'"; 226 } 227 else if (selectedValue == "vid" || selectedValue == "vname" || selectedValue == "vgender" || selectedValue == "vage" || selectedValue == "vaddress" || selectedValue == "vphone") 228 { 229 //通過相應的字段進行搜索 230 sql = "select * from VipInformation where " + selectedValue + " like '%" + txtDataforQuery.Text.Trim() + "%'"; 231 } 232 233 SqlConnection conn = new SqlConnection(connStr); 234 SqlCommand cmd = new SqlCommand(sql, conn); 235 conn.Open(); 236 DataTable dt = new DataTable(); 237 SqlDataAdapter sda = new SqlDataAdapter(cmd); 238 sda.Fill(dt); 239 dgvManager.DataSource = dt; 240 conn.Close(); 241 } 242 243 private void btnBack_Click(object sender, EventArgs e) 244 { 245 VIPMain vmain = new VIPMain(); 246 vmain.Show(); 247 this.Hide(); 248 } 249 250 private void btnClose_Click(object sender, EventArgs e) 251 { 252 VIPLog vpl = new VIPLog(); 253 vpl.GetExitTime(); 254 vpl.AddMsg(); 255 //徹底的退出 256 System.Environment.Exit(0); 257 } 258 259 260 } 261 }
用戶注冊界面(VIPRegistration.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.Data.SqlClient; 11 using System.Configuration; 12 13 namespace 會員管理系統 14 { 15 public partial class VIPRegistration : Form 16 { 17 public VIPRegistration() 18 { 19 InitializeComponent(); 20 } 21 22 23 //提交按鈕 24 private void btnOK_Click(object sender, EventArgs e) 25 { 26 27 string connstr = ConfigurationManager.ConnectionStrings["str"].ConnectionString; 28 SqlConnection conn = new SqlConnection(connstr); 29 string sql = string.Format("select vusername from VipAccount where vUserName='{0}' ",txtName.Text); 30 SqlCommand cmd = new SqlCommand(sql, conn); 31 conn.Open(); 32 SqlDataReader sda=cmd.ExecuteReader(); 33 34 //new一個 uType 來獲取 radiobutton 點擊事件下 觸發的用戶類型賦值 35 string uType = ""; 36 if (rdoAdministrator.Checked) 37 { 38 uType = "Administrator"; 39 } 40 else if (rdoNormalUser.Checked) 41 { 42 uType = "NormalUser"; 43 } 44 else 45 { 46 uType = "NormalUser"; 47 } 48 49 //--------------------------- 50 if (txtName.Text.Trim() == "") 51 { 52 lblName.Text="用戶名不能為空"; 53 return; 54 } 55 else if (txtPwd.Text.Trim() == ""|| txtPwdConfirm.Text.Trim()=="") 56 { 57 lblPwd.Text = "密碼不能為空"; 58 return; 59 } 60 else if (txtPwdConfirm.Text.Trim()!= txtPwd.Text.Trim()) 61 { 62 lblPwdConfirm.Text = "兩次密碼輸入不同,請確認后再輸"; 63 return; 64 } 65 else if (sda.Read()) 66 { 67 lblName.Text = "用戶名已存在,請重新輸入"; 68 return; 69 } 70 else 71 { 72 conn.Close(); 73 SqlConnection conninsert = new SqlConnection(connstr); 74 //string insertsql = string.Format("insert into VipAccount(vUserName,vUserPwd) values('{0}','{1}')",txtName.Text,txtPwd.Text); 75 string insertsql = "insert into VipAccount(vUserName,vUserPwd,UserType) values(@vUserName,@vUserPwd,@UserType) "; 76 //使用1個SQL參數數組 來裝載 需要插入的數據 77 SqlParameter[] param = { 78 new SqlParameter("@vUserName",txtName.Text), 79 new SqlParameter("@vUserPwd",txtPwd.Text), 80 new SqlParameter("@UserType",uType) 81 }; 82 83 SqlCommand cmdinsert = new SqlCommand(insertsql, conninsert); 84 conninsert.Open(); 85 cmdinsert.Parameters.AddRange(param); 86 int n = cmdinsert.ExecuteNonQuery(); 87 if (n == 0) 88 { 89 MessageBox.Show("注冊失敗,請重新輸入"); 90 } 91 else 92 { 93 MessageBox.Show("注冊成功"); 94 } 95 conninsert.Close(); 96 97 } 98 //conn.Close(); 99 } 100 101 //返回主菜單 102 private void btnBack_Click(object sender, EventArgs e) 103 { 104 VIPMain vm = new VIPMain(); 105 vm.Show(); 106 this.Hide(); 107 } 108 109 private void VIPRegistration_Load(object sender, EventArgs e) 110 { 111 lblName.Text = ""; 112 lblPwd.Text = ""; 113 lblPwdConfirm.Text = ""; 114 } 115 116 } 117 }