會員資料管理界面:
新建一個窗體,窗體界面和控件如下:
窗體中的控件dgvManager更改FullRowSelect屬性(點擊選中效果)為:FullRowSelect
會員資料管理界面窗體的詳細代碼:
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 } 30 31 //寫一個刷新數據的方法(跟查看數據一樣) 32 public void Refresh(bool isAdded = false) 33 { 34 //查詢數據庫字符串 35 string sql = String.Format("select vId '{0}',vName '{1}',vGender '{2}',vAge '{3}',vAddress '{4}',vPhone '{5}' from VipInformation", "編號", "名字", "性別", "年齡", "地址", "電話"); 36 //連接數據庫對象 37 SqlConnection conn = new SqlConnection(connStr); 38 //操作數據庫對象 39 SqlCommand cmd = new SqlCommand(sql, conn); 40 //創建表對象 41 System.Data.DataTable dt = new System.Data.DataTable(); 42 //創建數據庫填充操作對象(語句) 43 SqlDataAdapter sda = new SqlDataAdapter(cmd); 44 //把數據填充進dt表中 45 sda.Fill(dt); 46 //指定dgvManager控件的數據源:dt 47 dgvManager.DataSource = dt; 48 49 //if (isAdded) 50 //{ 51 // if (dt.Rows.Count > 0) 52 // dgvManager.Rows[0].Selected = false; 53 // dgvManager.Rows[dt.Rows.Count - 1].Selected = true; 54 //} 55 } 56 57 //刷新數據界面 58 private void btnView_Click(object sender, EventArgs e) 59 { 60 //刷新數據 61 Refresh(); 62 } 63 64 //添加數據 65 private void btnAdd_Click(object sender, EventArgs e) 66 { 67 //判斷文本框是否為空,提示數據完整性 68 if (txtName.Text == "" || txtGender.Text == "" || txtAge.Text == "" || txtAddress.Text == "" || txtPhone.Text == "") 69 { 70 MessageBox.Show("數據不能為空,請填寫齊全"); 71 return; 72 } 73 //插入數據庫字符串 74 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()); 75 //連接數據庫對象 76 SqlConnection conn = new SqlConnection(connStr); 77 //操作數據庫對象 78 SqlCommand cmd = new SqlCommand(sql, conn); 79 //創建表對象 80 System.Data.DataTable dt = new DataTable(); 81 //創建數據庫填充操作對象(語句) 82 SqlDataAdapter sda = new SqlDataAdapter(cmd); 83 //把數據填充進dt表中 84 sda.Fill(dt); 85 //指定dgvManager控件的數據源:dt 86 dgvManager.DataSource = dt; 87 //刷新數據 88 Refresh(); 89 } 90 91 //刪除數據 92 private void btnDelete_Click(object sender, EventArgs e) 93 { 94 //使用sql刪除語句,where 1=1 就是沒有條件,等於全部數據刪除 95 string sql = "delete from VipInformation where 1=1"; 96 //如果選中某行則執行 97 if (dgvManager.CurrentRow.Selected) 98 { 99 sql = sql + " and vid=" + Convert.ToInt32(dgvManager.CurrentRow.Cells[0].Value.ToString()); 100 } 101 int n = 0; 102 //創建連接數據庫對象 103 SqlConnection conn = new SqlConnection(connStr); 104 //創建操作數據庫對象 105 SqlCommand cmd = new SqlCommand(sql, conn); 106 //打開數據庫 107 conn.Open(); 108 //取得ExecuteNonQuery返回的受影響行數,無影響則為0 109 n = cmd.ExecuteNonQuery(); 110 if (n == 0) 111 { 112 MessageBox.Show("刪除操作失敗!不存在的ID"); 113 conn.Close(); 114 return; 115 } 116 else if (n > 0) 117 { 118 MessageBox.Show("刪除操作成功!"); 119 } 120 //關閉數據庫連接 121 conn.Close(); 122 //刷新數據界面 123 Refresh(); 124 } 125 126 //修改數據 127 private void btnSave_Click(object sender, EventArgs e) 128 { 129 if (txtName.Text == "" || txtGender.Text == "" || txtAge.Text == "" || txtAddress.Text == "" || txtPhone.Text == "") 130 { 131 MessageBox.Show("所提供的數據不完整,請填寫完整數據"); 132 return; 133 } 134 int n = 0; 135 //更新SQL語句 136 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() + "'"; 137 SqlConnection conn = new SqlConnection(connStr); 138 SqlCommand cmd = new SqlCommand(sqlupdate, conn); 139 conn.Open(); 140 n = cmd.ExecuteNonQuery(); 141 if (n == 0) 142 { 143 MessageBox.Show("修改操作失敗!"); 144 conn.Close(); 145 return; 146 } 147 else if (n > 0) 148 { 149 MessageBox.Show("修改操作成功!"); 150 } 151 conn.Close(); 152 Refresh(); 153 } 154 155 //點擊dgvManager在文本框上顯示 156 private void dgvManager_CellContentClick(object sender, DataGridViewCellEventArgs e) 157 { 158 txtName.Text = dgvManager.CurrentRow.Cells[1].Value.ToString(); 159 txtGender.Text = dgvManager.CurrentRow.Cells[2].Value.ToString(); 160 txtAge.Text = dgvManager.CurrentRow.Cells[3].Value.ToString(); 161 txtAddress.Text = dgvManager.CurrentRow.Cells[4].Value.ToString(); 162 txtPhone.Text = dgvManager.CurrentRow.Cells[5].Value.ToString(); 163 } 164 165 } 166 }
之前登錄窗體的代碼增加代碼:
1 if (pwd == txtPwd.Text) 2 { 3 //說明在該賬戶下 密碼正確, 系統登錄成功 4 MessageBox.Show("登錄成功,正在進入主界面......"); 5 //***************新增代碼*************** 6 //創建新的會員資料管理界面窗體並顯示,同時把登錄界面隱藏 7 VipManager vm=new VipManager(); 8 vm.Show(); 9 this.Hide(); 10 //***************新增代碼*************** 11 } 12 else 13 { 14 //密碼錯誤 15 MessageBox.Show("密碼錯誤,請重新輸入"); 16 txtPwd.Text = ""; 17 }
登錄窗體的詳細代碼:
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 vm.Show(); 53 this.Hide(); 54 //***************新增代碼*************** 55 } 56 else 57 { 58 //密碼錯誤 59 MessageBox.Show("密碼錯誤,請重新輸入"); 60 txtPwd.Text = ""; 61 } 62 } 63 else 64 { 65 //用戶名錯誤 66 MessageBox.Show("用戶名錯誤,請重新輸入!"); 67 txtName.Text = ""; 68 } 69 } 70 } 71 } 72 } 73 74 } 75 }
運行效果: