數據庫設計
這里由於增加了普通用戶權限值,我們需要對數據庫結構稍作修改.這里在MovieAccount表中增加4列內容 分別用於
RightFManager 判斷普通用戶管理界面權限 RightFRegistration 判斷普通用戶注冊權限
RightFPwdChange 判斷普通用戶密碼修改權限 RightFLog 判斷普通用戶日志查詢權限
詳細數據庫語句:
1 use Vip 2 select * from VipAccount 3 alter table VipAccount add RightFManager int null 4 alter table VipAccount add RightFRegistration int null 5 alter table VipAccount add RightFPwdChange int null 6 alter table VipAccount add RightFLog int null
登錄窗體(VIPLogin.cs)代碼修改,詳細代碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace 會員管理系統 { public partial class VIPLogin : Form { public VIPLogin() { InitializeComponent(); } //用於連接配置文件App.config string connStr = ConfigurationManager.ConnectionStrings["str"].ConnectionString; //定義一個全局變量 Uid; //用於獲取登錄成功后的用戶名 public static string uid; //定義一個全局變量 time; //用於獲取用戶登錄時的時間 public static DateTime time; //定義一個全局變量situation //用於獲取用戶的登錄狀態 public static string situation; //定義一個全局變量UserType //用於獲取用戶權限 public static string UserType; //定義一個全局變量 數組FRight 來獲取登錄成功的普通用戶功能權限 public static int[] FRight; //登錄按鈕 private void btnLogin_Click(object sender, EventArgs e) { //連接數據庫語句 using(SqlConnection con=new SqlConnection(connStr)) { //操作數據庫語句 string sql = "select vuserpwd,usertype,RightFManager,RightFRegistration,RightFPwdChange,RightFLog from vipaccount where vUserName='" + txtName.Text + "'"; using(SqlCommand cmd=new SqlCommand(sql,con)) { //打開數據庫 con.Open(); //使用 SqlDataReader 來 讀取數據庫 using (SqlDataReader sdr = cmd.ExecuteReader()) { //SqlDataReader 在數據庫中為 從第1條數據開始 一條一條往下讀 if (sdr.Read()) //如果讀取賬戶成功(文本框中的用戶名在數據庫中存在) { //則將第1條 密碼 賦給 字符串pwd ,並且依次往后讀取 所有的密碼 //Trim()方法為移除字符串前后的空白 string pwd = sdr.GetString(0).Trim(); //讀取器sdr獲取了2列數據 第1列為密碼 第2列 即索引為1的是用戶類型 string uType = sdr.GetString(1).Trim() ; //如果 文本框中輸入的密碼 ==數據庫中的密碼 if (pwd == txtPwd.Text) { uid = txtName.Text; time = DateTime.Now; situation = "登錄"; //將登錄成功的用戶類型 賦給全局變量UserType //用於獲取當前登錄 用戶的類型 UserType = uType; /************用戶登錄成功后 對用戶類型進行判斷********************/ //用戶功能權限索引i 對應的數據讀取器sdr里的索引是 是 i+2 //FRight[0] = sdr.GetInt32(2); //RightFManager //FRight[1] = sdr.GetInt32(3); //RightFRegistration //FRight[2] = sdr.GetInt32(4); //RightFPwdChange //FRight[3] = sdr.GetInt32(5); //RightFLog //如果用戶為普通用戶 則檢查 其功能權限 管理員不被檢查 if (UserType == "NormalUser") { FRight = new int[4]; for (int i = 0; i < FRight.Length; i++) { //如果數據讀取器中讀到數據庫中的權限值為0 if (sdr.GetInt32(i + 2) == 0) { //則賦給全局變量0 說明該功能被禁用 FRight[i] = 0; } //如果 為1 else if (sdr.GetInt32(i + 2) == 1) { //賦給全局變量1 該功能可用 FRight[i] = 1; } else { //否則默認為0 該功能不可用 FRight[i] = 0; } } } //說明在該賬戶下 密碼正確, 系統登錄成功 MessageBox.Show("登錄成功,正在進入主界面......"); //***************新增代碼*************** VIPLog vl = new VIPLog(); //添加當前的用戶信息到日志中 vl.AddMsg(); //退出程序 //創建新的會員資料管理界面窗體並顯示,同時把登錄界面隱藏 //VIPManager vm=new VIPManager(); VIPMain vmain = new VIPMain(); vmain.Show(); this.Hide(); //***************新增代碼*************** } else { //密碼錯誤 MessageBox.Show("密碼錯誤,請重新輸入"); txtPwd.Text = ""; } } else { //用戶名錯誤 MessageBox.Show("用戶名錯誤,請重新輸入!"); txtName.Text = ""; } } } } } //設置快捷鍵 private void VIPLogin_KeyDown(object sender, KeyEventArgs e) { //如果按下ESC鍵 if (e.KeyCode == Keys.Escape) { //關閉窗體 this.Close(); } //如果按下F5鍵 else if (e.KeyCode == Keys.F5) { //調用登錄按鈕單擊事件 this.btnLogin_Click(null,null); } } //關閉 private void btnClose_Click(object sender, EventArgs e) { //徹底的退出 System.Environment.Exit(0); } //當登錄窗體為活動窗體時 private void VIPLogin_Activated(object sender, EventArgs e) { //設置文本框txtName獲得焦點 txtName.Focus(); } //獲取文本框txtName的快捷鍵 private void txtName_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { txtPwd.Focus(); txtPwd.SelectAll(); } } //獲取文本框txtPwd的快捷鍵 private void txtPwd_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { btnLogin_Click(null, null); } } } }
主界面(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 //當用戶為普通用戶的時候 需要判斷下其功能權限 管理員不接受檢查 21 if (VIPLogin.UserType=="NormalUser") 22 { 23 //對應的功能權限為1的時候 本功能開放 24 if (VIPLogin.FRight[0] == 1) 25 { 26 btnVIPManager.Enabled = true; 27 } 28 //否則 禁用本功能 29 else 30 { 31 btnVIPManager.Enabled = false; 32 } 33 //對應的功能權限為1的時候 本功能開放 34 if (VIPLogin.FRight[1] == 1) 35 { 36 btnRegistration.Enabled = true; 37 } 38 //否則 禁用本功能 39 else 40 { 41 btnRegistration.Enabled = false; 42 } 43 //對應的功能權限為1的時候 本功能開放 44 if (VIPLogin.FRight[2] == 1) 45 { 46 btnPwdChange.Enabled = true; 47 } 48 //否則 禁用本功能 49 else 50 { 51 btnPwdChange.Enabled = false; 52 } 53 //對應的功能權限為1的時候 本功能開放 54 if (VIPLogin.FRight[3] == 1) 55 { 56 btnLog.Enabled = true; 57 } 58 //否則 禁用本功能 59 else 60 { 61 btnLog.Enabled = false; 62 } 63 } 64 } 65 66 private void btnVIPManager_Click(object sender, EventArgs e) 67 { 68 VIPManager vm = new VIPManager(); 69 vm.Show(); 70 this.Hide(); 71 } 72 73 private void btnVIPLogin_Click(object sender, EventArgs e) 74 { 75 VIPLog vipl = new VIPLog(); 76 vipl.GetExitTime(); 77 vipl.AddMsg(); 78 VIPLogin vp = new VIPLogin(); 79 vp.Show(); 80 this.Hide(); 81 } 82 83 private void btnClose_Click(object sender, EventArgs e) 84 { 85 VIPLog vipl = new VIPLog(); 86 vipl.GetExitTime(); 87 vipl.AddMsg(); 88 //徹底的退出 89 System.Environment.Exit(0); 90 } 91 92 private void btnPwdChange_Click(object sender, EventArgs e) 93 { 94 VIPPwdChange vpc = new VIPPwdChange(); 95 vpc.Show(); 96 this.Hide(); 97 } 98 99 private void VIPMain_Load(object sender, EventArgs e) 100 { 101 lblCurrentUser.Text = "當前登錄用戶為:"+VIPLogin.uid+ " 用戶類型:" + VIPLogin.UserType + " 登錄時間為:"+VIPLogin.time; 102 //給當前用戶打招呼 103 //這里通過獲取當前用戶電腦上的時間 以及判斷登錄用戶的類型 來給不同類型的用戶打招呼 104 // 定義整型變量 intTime 來獲取 用戶電腦上的具體小時數 然后在如下進行判斷 105 int inttime = VIPLogin.time.Hour; 106 //獲取VIPLogin窗體的全局變量 UserType 用戶類型, 用於給不同類型的用戶打招呼 107 string uType = VIPLogin.UserType; 108 //在凌晨0-6點的時候 109 if (inttime >= 0 && inttime < 6) 110 { 111 if (uType == "Administrator") 112 { 113 lblSayHi.Text = "尊敬的"+ VIPLogin.uid + "您好,現在已夜深,請注意休息!"; 114 } 115 else if (uType == "NormalUser") 116 { 117 lblSayHi.Text = "親愛的" + VIPLogin.uid + "您好,現在已夜深,請注意休息!"; 118 } 119 } 120 //早上6點-中午12點的時候 121 else if (inttime >= 6 && inttime < 12) 122 { 123 if (uType == "Administrator") 124 { 125 lblSayHi.Text = "尊敬的" + VIPLogin.uid + "早上好!"; 126 } 127 else if (uType == "NormalUser") 128 { 129 lblSayHi.Text = "親愛的" + VIPLogin.uid + "早上好!"; 130 } 131 } 132 //中午12點-下午6點的時候 133 else if (inttime >= 12 && inttime < 18) 134 { 135 if (uType == "Administrator") 136 { 137 lblSayHi.Text = "尊敬的" + VIPLogin.uid + "下午好!"; 138 } 139 else if (uType == "NormalUser") 140 { 141 lblSayHi.Text = "親愛的" + VIPLogin.uid + "下午好!"; 142 } 143 } 144 //晚上 145 else if (inttime >= 18 && inttime < 24) 146 { 147 if (uType == "Administrator") 148 { 149 lblSayHi.Text = "尊敬的" + VIPLogin.uid + "晚上好!"; 150 } 151 else if (uType == "NormalUser") 152 { 153 lblSayHi.Text = "親愛的" + VIPLogin.uid + "晚上好!"; 154 } 155 } 156 //否則 默認為 157 else 158 { 159 lblSayHi.Text = "歡迎使用會員管理系統!"; 160 } 161 ////判斷用戶類型 並給用戶設置功能權限 162 //if (uType == "NormalUser") 163 //{ 164 // btnRegistration.Enabled = false; 165 // btnLog.Enabled = false; 166 //} 167 } 168 169 private void btnLog_Click(object sender, EventArgs e) 170 { 171 VIPLog vl=new VIPLog(); 172 vl.Show(); 173 this.Hide(); 174 } 175 176 private void btnRegistration_Click(object sender, EventArgs e) 177 { 178 VIPRegistration vrn = new VIPRegistration(); 179 vrn.Show(); 180 this.Hide(); 181 } 182 183 private void btnVIPUserManger_Click(object sender, EventArgs e) 184 { 185 VIPUserManger vum = new VIPUserManger(); 186 vum.Show(); 187 this.Hide(); 188 } 189 } 190 }
用戶權限功能判斷成功之后,我們便要開始寫管理員給普通用戶分配權限的功能,我們新建一個普通用戶管理窗體VIPUserManger.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 VIPUserManger : Form 16 { 17 public VIPUserManger() 18 { 19 InitializeComponent(); 20 } 21 //定義私有變量-功能權限值 用於獲取 radiobutton 被check后的返回值 22 private int rightFManager = 0; 23 private int rightFRegistration = 0; 24 private int rightFPwdChange = 0; 25 private int rightFLog = 0; 26 27 //strType 用於獲取 當前DataGridView 被點中行的用戶類型 28 private string strUType = ""; 29 30 //經常會調用的數據庫語句,放出來方便使用 31 string connstr = ConfigurationManager.ConnectionStrings["str"].ConnectionString; 32 33 private void ViewAllUsers() 34 { 35 //string connstr = ConfigurationManager.ConnectionStrings["str"].ConnectionString; 36 SqlConnection conn = new SqlConnection(connstr); 37 string sql = "select * from VipAccount"; 38 SqlCommand cmd = new SqlCommand(sql,conn); 39 DataTable dt = new DataTable(); 40 SqlDataAdapter sda = new SqlDataAdapter(cmd); 41 sda.Fill(dt); 42 dgvUserManger.DataSource = dt; 43 } 44 45 private void VIPUserManger_Load(object sender, EventArgs e) 46 { 47 //為cboUserType下拉框添加可選值 48 cboUserType.Items.AddRange(new string[] { "NormalUser", "Administrator" }); 49 //刷新dgvUserManger數據界面 50 ViewAllUsers(); 51 } 52 53 private void btnBack_Click(object sender, EventArgs e) 54 { 55 VIPMain vm = new VIPMain(); 56 vm.Show(); 57 this.Hide(); 58 } 59 60 private void btnClose_Click(object sender, EventArgs e) 61 { 62 Application.Exit(); 63 } 64 65 private void dgvUserManger_CellContentClick(object sender, DataGridViewCellEventArgs e) 66 { 67 //點中DataGridView后,將當前行內容傳遞給文本框 便於在文本框中修改 68 txtUserName.Text = dgvUserManger.Rows[e.RowIndex].Cells["vusername"].Value.ToString(); 69 txtPwd.Text = dgvUserManger.Rows[e.RowIndex].Cells["vuserpwd"].Value.ToString(); 70 cboUserType.Text = dgvUserManger.Rows[e.RowIndex].Cells["usertype"].Value.ToString(); 71 72 //獲取用戶類型 73 strUType = cboUserType.Text.Trim(); 74 /************************************************************************************/ 75 //每次點擊 DataGridView新的行之前 都要把前一次點擊行的 CheckBox 狀態調整為系統默認狀態 全未選中 並且都可選 76 //比如 如果之前 點了NormalUser 再點Administer 會保留之前NormalUser的選擇項 這里我們需要清空 77 chkManager.Checked = false; 78 chkRegistration.Checked = false; 79 chkPwdChange.Checked = false; 80 chkLog.Checked = false; 81 82 txtUserName.Enabled = true; 83 txtPwd.Enabled = true; 84 cboUserType.Enabled = true; 85 chkManager.Enabled = true; 86 chkRegistration.Enabled = true; 87 chkPwdChange.Enabled = true; 88 chkLog.Enabled = true; 89 90 if (strUType == "NormalUser") 91 { 92 //獲取當前DataGridView被點中行內容中的用戶權限值 93 rightFManager = Convert.ToInt32(dgvUserManger.Rows[e.RowIndex].Cells["RightFManager"].Value); 94 rightFRegistration = Convert.ToInt32(dgvUserManger.Rows[e.RowIndex].Cells["RightFRegistration"].Value); 95 rightFPwdChange = Convert.ToInt32(dgvUserManger.Rows[e.RowIndex].Cells["RightFPwdChange"].Value); 96 rightFLog = Convert.ToInt32(dgvUserManger.Rows[e.RowIndex].Cells["RightFLog"].Value); 97 98 //同時判斷獲取的權限值 並相應的選中Checkbox 99 if (rightFManager == 1) 100 chkManager.Checked = true; 101 else 102 chkManager.Checked = false; 103 if (rightFRegistration == 1) 104 chkRegistration.Checked = true; 105 else 106 chkRegistration.Checked = false; 107 if (rightFPwdChange == 1) 108 chkPwdChange.Checked = true; 109 else 110 chkPwdChange.Checked = false; 111 if (rightFLog == 1) 112 chkLog.Checked = true; 113 else 114 chkLog.Checked = false; 115 } 116 //當用戶類型為管理員的時候,將所有的CheckBox禁用, 管理員功能權限不允許設置 117 else if (strUType == "Administrator") 118 { 119 chkManager.Enabled = false; 120 chkPwdChange.Enabled = false; 121 chkRegistration.Enabled = false; 122 chkLog.Enabled = false; 123 124 txtUserName.Enabled = false; 125 txtPwd.Enabled = false; 126 cboUserType.Enabled = false; 127 } 128 } 129 130 //查詢用戶信息 131 private void txtUserQuery_TextChanged(object sender, EventArgs e) 132 { 133 string sql = ""; 134 135 if (txtUserQuery.Text.Trim() == "") 136 { 137 //執行查詢語句 138 sql = "select * from VipAccount"; 139 } 140 else 141 { 142 //全局搜索 (通過用戶ID 用戶名和 用戶類型進行模糊查找) 143 sql = "select * from VipAccount where(vId like'%" + txtUserQuery.Text.Trim() + "%')or(vUserName like'%" + txtUserQuery.Text.Trim() + "%')or(UserType like'%" + txtUserQuery.Text.Trim() + "%')"; 144 } 145 SqlConnection conn = new SqlConnection(connstr); 146 SqlCommand cmd = new SqlCommand(sql, conn); 147 DataTable dt = new DataTable(); 148 SqlDataAdapter sda = new SqlDataAdapter(cmd); 149 sda.Fill(dt); 150 dgvUserManger.DataSource = dt; 151 152 } 153 154 //調用查看所有用戶的方法來刷新當前 DataGridView的內容 155 private void btnView_Click(object sender, EventArgs e) 156 { 157 //點擊 "查看所有" 按鈕 調用 ViewAllUsers 方法 刷新 並查當前看所有用戶 158 ViewAllUsers(); 159 } 160 161 //添加用戶 162 private void btnAdd_Click(object sender, EventArgs e) 163 { 164 //判斷插入的數據是否為空,如果為空,則提示重新插入! 165 if (txtUserName.Text.Trim() == "" || txtPwd.Text.Trim() == "" || cboUserType.Text == "") 166 { 167 MessageBox.Show("插入數據不能為空,請按要求插入數據!"); 168 return; 169 } 170 if (cboUserType.Text == "Administrator") 171 { 172 MessageBox.Show("暫不開放注冊管理員功能!"); 173 return; 174 } 175 176 //使用SQL插入數據語句 177 string sql = "insert into VipAccount(vUserName,vUserPwd,UserType,RightFManager,RightFRegistration,RightFPwdChange,RightFLog) values (@vUserName,@vUserPwd,@UserType,@RightFManager,@RightFRegistration,@RightFPwdChange,@RightFLog)"; 178 179 //判斷 哪些CheckBox 被選中 180 JudgeChecked(); 181 182 //向數據庫插入參數 183 SqlParameter[] param ={ 184 new SqlParameter("@vUserName",txtUserName.Text.Trim()), 185 new SqlParameter("@vUserPwd",txtPwd.Text.Trim()), 186 new SqlParameter("@UserType",cboUserType.Text.Trim()), 187 new SqlParameter("@RightFManager",rightFManager), 188 new SqlParameter("@RightFRegistration",rightFRegistration), 189 new SqlParameter("@RightFPwdChange",rightFPwdChange), 190 new SqlParameter("@RightFLog",rightFLog) 191 192 }; 193 194 SqlConnection conn = new SqlConnection(connstr); 195 SqlCommand cmd = new SqlCommand(sql,conn); 196 conn.Open(); 197 cmd.Parameters.AddRange(param); 198 int n = cmd.ExecuteNonQuery(); 199 conn.Close(); 200 201 if (n > 0) 202 { 203 MessageBox.Show("數據插入成功!"); 204 } 205 else 206 { 207 MessageBox.Show("插入失敗!"); 208 return; 209 } 210 211 //調用ViewAllUsers 方法 用於刷新 在添加完成數據后 自動刷新數據 212 ViewAllUsers(); 213 } 214 //判斷CheckBox是否被點重或是取消, 用於更新和修改用戶權限 215 private void JudgeChecked() 216 { 217 //當相應的功能權限 checkbox 被點中 或者取消 將分別賦值 並傳給相應的私有變量 用於數據庫添加,更新等操作 218 if (chkManager.Checked == true) 219 rightFManager = 1; 220 else 221 rightFManager = 0; 222 if (chkRegistration.Checked == true) 223 rightFRegistration = 1; 224 else 225 rightFRegistration = 0; 226 if (chkPwdChange.Checked == true) 227 rightFPwdChange = 1; 228 else 229 rightFPwdChange = 0; 230 if (chkLog.Checked == true) 231 rightFLog = 1; 232 else 233 rightFLog = 0; 234 } 235 236 //保存修改的信息 237 private void btnSave_Click(object sender, EventArgs e) 238 { 239 //在對數據進行修改之前 對文本框的內容做一下檢查 是否用戶已經輸入內容, 如果為空 則提示重新輸入 240 if (txtUserName.Text.Trim() == "" || txtPwd.Text.Trim() == "" || cboUserType.Text.Trim() == "") 241 { 242 MessageBox.Show("文本框的輸入不能為空!"); 243 return; 244 } 245 if (cboUserType.Text == "Administrator") 246 { 247 MessageBox.Show("暫不開放注冊管理員功能!"); 248 return; 249 } 250 251 //判斷 哪些CheckBox 被選中 252 JudgeChecked(); 253 254 //使用SQL update 更新語句 255 //獲取文本框 和ComboBox 輸入的內容, 通過用戶的ID(vId) 進行更新(ID為當前鼠標點擊行的vId) 256 string sqlUpdate = "update VipAccount set vUserName = @vUserName, vUserPwd = @vUserPwd,UserType=@UserType,RightFManager=@RightFManager,RightFRegistration=@RightFRegistration,RightFPwdChange=@RightFPwdChange,RightFLog=@RightFLog where vId=@vId"; 257 258 SqlParameter[] param ={ 259 new SqlParameter("@vUserName",txtUserName.Text.Trim()), 260 new SqlParameter("@vUserPwd",txtPwd.Text.Trim()), 261 new SqlParameter("@UserType",cboUserType.Text.Trim()), 262 new SqlParameter("@RightFManager",rightFManager), 263 new SqlParameter("@RightFRegistration",rightFRegistration), 264 new SqlParameter("@RightFPwdChange",rightFPwdChange), 265 new SqlParameter("@RightFLog",rightFLog), 266 new SqlParameter("@vId",dgvUserManger.CurrentRow.Cells[0].Value), 267 }; 268 SqlConnection conn = new SqlConnection(connstr); 269 SqlCommand cmd = new SqlCommand(sqlUpdate, conn); 270 conn.Open(); 271 cmd.Parameters.AddRange(param); 272 int n = cmd.ExecuteNonQuery(); 273 conn.Close(); 274 275 //判定 如果n=0,則說明沒有獲取到數據,ExecuteNonQuery執行不成功 276 if (n == 0) 277 { 278 //提示更新失敗 279 MessageBox.Show("更新失敗!"); 280 return;// 並且返回 281 } 282 else 283 { 284 //否則更新成功 285 MessageBox.Show("恭喜你!更新成功!"); 286 } 287 288 //保存完以后 調用刷新方法,將更新后的數據 顯示在datagridview上面 289 ViewAllUsers(); 290 } 291 292 //刪除DataGridView里 選中的用戶 293 private void btnDelete_Click(object sender, EventArgs e) 294 { 295 //使用sql刪除語句 296 string sqlDelete = null; 297 //如果datagridview的當前行被選中 298 if (dgvUserManger.CurrentRow.Selected) 299 { 300 //獲取當前點中行的vID 並且 將當前行的vID號 賦給SQL 語句 301 sqlDelete = "delete from VipAccount where vId=@vId"; 302 } 303 304 SqlParameter[] param = { 305 new SqlParameter("@vId",Convert.ToInt32(dgvUserManger.CurrentRow.Cells[0].Value)) 306 }; 307 SqlConnection conn = new SqlConnection(connstr); 308 SqlCommand cmd = new SqlCommand(sqlDelete, conn); 309 conn.Open(); 310 cmd.Parameters.AddRange(param); 311 int n = cmd.ExecuteNonQuery(); 312 conn.Close(); 313 //如果n>0 說明刪除數據成功 314 if (n > 0) 315 { 316 MessageBox.Show("刪除成功!"); 317 } 318 else //否則失敗 319 { 320 MessageBox.Show("不存在的ID!"); 321 } 322 //刪除完后 刷新一下當前數據 323 ViewAllUsers(); 324 } 325 } 326 }
最后,由於在數據庫中新增了幾列普通用戶功能權限的字段,我們的用戶注冊窗體也需要稍作修改(這里普通用戶注冊是默認禁用其所有功能權限,需要管理員在普通用戶管理窗體賦予其相應的功能權限).
詳細代碼如下:
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 37 int rightFManager = 0; 38 int rightFRegistration = 0; 39 int rightFPwdChange = 0; 40 int rightFLog = 0; 41 42 if (rdoAdministrator.Checked) 43 { 44 uType = "Administrator"; 45 rightFManager = 1; 46 rightFRegistration = 1; 47 rightFPwdChange = 1; 48 rightFLog = 1; 49 } 50 else if (rdoNormalUser.Checked) 51 { 52 uType = "NormalUser"; 53 } 54 else 55 { 56 uType = "NormalUser"; 57 } 58 59 //--------------------------- 60 if (txtName.Text.Trim() == "") 61 { 62 lblName.Text = "用戶名不能為空"; 63 return; 64 } 65 else if (txtPwd.Text.Trim() == "" || txtPwdConfirm.Text.Trim() == "") 66 { 67 lblPwd.Text = "密碼不能為空"; 68 return; 69 } 70 else if (txtPwdConfirm.Text.Trim() != txtPwd.Text.Trim()) 71 { 72 lblPwdConfirm.Text = "兩次密碼輸入不同,請確認后再輸"; 73 return; 74 } 75 else if (sda.Read()) 76 { 77 lblName.Text = "用戶名已存在,請重新輸入"; 78 return; 79 } 80 else 81 { 82 conn.Close(); 83 SqlConnection conninsert = new SqlConnection(connstr); 84 //string insertsql = string.Format("insert into VipAccount(vUserName,vUserPwd) values('{0}','{1}')",txtName.Text,txtPwd.Text); 85 string insertsql = "insert into VipAccount(vUserName,vUserPwd,UserType,RightFManager,RightFRegistration,RightFPwdChange,RightFLog) values(@vUserName,@vUserPwd,@UserType,@RightFManager,@RightFRegistration,@RightFPwdChange,@RightFLog) "; 86 //使用1個SQL參數數組 來裝載 需要插入的數據 87 SqlParameter[] param = { 88 new SqlParameter("@vUserName",txtName.Text), 89 new SqlParameter("@vUserPwd",txtPwd.Text), 90 new SqlParameter("@UserType",uType), 91 new SqlParameter("@RightFManager",rightFManager), 92 new SqlParameter("@RightFRegistration",rightFRegistration), 93 new SqlParameter("@RightFPwdChange",rightFPwdChange), 94 new SqlParameter("@RightFLog",rightFLog) 95 }; 96 97 SqlCommand cmdinsert = new SqlCommand(insertsql, conninsert); 98 conninsert.Open(); 99 cmdinsert.Parameters.AddRange(param); 100 int n = cmdinsert.ExecuteNonQuery(); 101 if (n == 0) 102 { 103 MessageBox.Show("注冊失敗,請重新輸入"); 104 } 105 else 106 { 107 MessageBox.Show("注冊成功"); 108 } 109 conninsert.Close(); 110 111 } 112 //conn.Close(); 113 } 114 115 //返回主菜單 116 private void btnBack_Click(object sender, EventArgs e) 117 { 118 VIPMain vm = new VIPMain(); 119 vm.Show(); 120 this.Hide(); 121 } 122 123 private void VIPRegistration_Load(object sender, EventArgs e) 124 { 125 lblName.Text = ""; 126 lblPwd.Text = ""; 127 lblPwdConfirm.Text = ""; 128 } 129 130 } 131 }
附上源代碼: