C#_会员管理系统:开发二(会员资料管理界面的‘增删改查’)
2016-04-22 11:26
711 查看
会员资料管理界面:
新建一个窗体,窗体界面和控件如下:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/12/1f75a425a88ba75f213390ab0d329769.png)
窗体中的控件dgvManager更改FullRowSelect属性(点击选中效果)为:FullRowSelect
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/12/39a864dfd1a34fa759283d561be2fd5f.png)
会员资料管理界面窗体的详细代码:
之前登录窗体的代码增加代码:
登录窗体的详细代码:
运行效果:
新建一个窗体,窗体界面和控件如下:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/12/1f75a425a88ba75f213390ab0d329769.png)
窗体中的控件dgvManager更改FullRowSelect属性(点击选中效果)为:FullRowSelect
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/12/39a864dfd1a34fa759283d561be2fd5f.png)
会员资料管理界面窗体的详细代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 会员管理系统 { public partial class VipManager : Form { public VipManager() { InitializeComponent(); } //连接字符串 获取配置文件里的连接路径,多次需要调用,放在外面方便 static string connStr = ConfigurationManager.ConnectionStrings["str"].ConnectionString; //窗体运行自动加载 private void VipManager_Load(object sender, EventArgs e) { //刷新数据 Refresh(); } //写一个刷新数据的方法(跟查看数据一样) public void Refresh(bool isAdded = false) { //查询数据库字符串 string sql = String.Format("select vId '{0}',vName '{1}',vGender '{2}',vAge '{3}',vAddress '{4}',vPhone '{5}' from VipInformation", "编号", "名字", "性别", "年龄", "地址", "电话"); //连接数据库对象 SqlConnection conn = new SqlConnection(connStr); //操作数据库对象 SqlCommand cmd = new SqlCommand(sql, conn); //创建表对象 System.Data.DataTable dt = new System.Data.DataTable(); //创建数据库填充操作对象(语句) SqlDataAdapter sda = new SqlDataAdapter(cmd); //把数据填充进dt表中 sda.Fill(dt); //指定dgvManager控件的数据源:dt dgvManager.DataSource = dt; //if (isAdded) //{ // if (dt.Rows.Count > 0) // dgvManager.Rows[0].Selected = false; // dgvManager.Rows[dt.Rows.Count - 1].Selected = true; //} } //刷新数据界面 private void btnView_Click(object sender, EventArgs e) { //刷新数据 Refresh(); } //添加数据 private void btnAdd_Click(object sender, EventArgs e) { //判断文本框是否为空,提示数据完整性 if (txtName.Text == "" || txtGender.Text == "" || txtAge.Text == "" || txtAddress.Text == "" || txtPhone.Text == "") { MessageBox.Show("数据不能为空,请填写齐全"); return; } //插入数据库字符串 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()); //连接数据库对象 SqlConnection conn = new SqlConnection(connStr); //操作数据库对象 SqlCommand cmd = new SqlCommand(sql, conn); //创建表对象 System.Data.DataTable dt = new DataTable(); //创建数据库填充操作对象(语句) SqlDataAdapter sda = new SqlDataAdapter(cmd); //把数据填充进dt表中 sda.Fill(dt); //指定dgvManager控件的数据源:dt dgvManager.DataSource = dt; //刷新数据 Refresh(); } //删除数据 private void btnDelete_Click(object sender, EventArgs e) { //使用sql删除语句,where 1=1 就是没有条件,等于全部数据删除 string sql = "delete from VipInformation where 1=1"; //如果选中某行则执行 if (dgvManager.CurrentRow.Selected) { sql = sql + " and vid=" + Convert.ToInt32(dgvManager.CurrentRow.Cells[0].Value.ToString()); } int n = 0; //创建连接数据库对象 SqlConnection conn = new SqlConnection(connStr); //创建操作数据库对象 SqlCommand cmd = new SqlCommand(sql, conn); //打开数据库 conn.Open(); //取得ExecuteNonQuery返回的受影响行数,无影响则为0 n = cmd.ExecuteNonQuery(); if (n == 0) { MessageBox.Show("删除操作失败!不存在的ID"); conn.Close(); return; } else if (n > 0) { MessageBox.Show("删除操作成功!"); } //关闭数据库连接 conn.Close(); //刷新数据界面 Refresh(); } //修改数据 private void btnSave_Click(object sender, EventArgs e) { if (txtName.Text == "" || txtGender.Text == "" || txtAge.Text == "" || txtAddress.Text == "" || txtPhone.Text == "") { MessageBox.Show("所提供的数据不完整,请填写完整数据"); return; } int n = 0; //更新SQL语句 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() + "'"; SqlConnection conn = new SqlConnection(connStr); SqlCommand cmd = new SqlCommand(sqlupdate, conn); conn.Open(); n = cmd.ExecuteNonQuery(); if (n == 0) { MessageBox.Show("修改操作失败!"); conn.Close(); return; } else if (n > 0) { MessageBox.Show("修改操作成功!"); } conn.Close(); Refresh(); } //点击dgvManager在文本框上显示 private void dgvManager_CellContentClick(object sender, DataGridViewCellEventArgs e) { txtName.Text = dgvManager.CurrentRow.Cells[1].Value.ToString(); txtGender.Text = dgvManager.CurrentRow.Cells[2].Value.ToString(); txtAge.Text = dgvManager.CurrentRow.Cells[3].Value.ToString(); txtAddress.Text = dgvManager.CurrentRow.Cells[4].Value.ToString(); txtPhone.Text = dgvManager.CurrentRow.Cells[5].Value.ToString(); } } }
之前登录窗体的代码增加代码:
if (pwd == txtPwd.Text) { //说明在该账户下 密码正确, 系统登录成功 MessageBox.Show("登录成功,正在进入主界面......"); //***************新增代码*************** //创建新的会员资料管理界面窗体并显示,同时把登录界面隐藏 VipManager vm=new VipManager(); vm.Show(); this.Hide(); //***************新增代码*************** } else { //密码错误 MessageBox.Show("密码错误,请重新输入"); txtPwd.Text = ""; }
登录窗体的详细代码:
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; //登录按钮 private void btnLogin_Click(object sender, EventArgs e) { //连接数据库语句 using(SqlConnection con=new SqlConnection(connStr)) { //操作数据库语句 string sql = "select vuserpwd 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(); //如果 文本框中输入的密码 ==数据库中的密码 if (pwd == txtPwd.Text) { //说明在该账户下 密码正确, 系统登录成功 MessageBox.Show("登录成功,正在进入主界面......"); //***************新增代码*************** //创建新的会员资料管理界面窗体并显示,同时把登录界面隐藏 VipManager vm=new VipManager(); vm.Show(); this.Hide(); //***************新增代码*************** } else { //密码错误 MessageBox.Show("密码错误,请重新输入"); txtPwd.Text = ""; } } else { //用户名错误 MessageBox.Show("用户名错误,请重新输入!"); txtName.Text = ""; } } } } } } }
运行效果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/12/63f635eca11beb55c39b3701300b9941.png)
相关文章推荐
- VS C#命令行编译工具CSC使用入门
- VS2010远程调试C#程序
- c#中winform窗体之间的传值
- C# Math.Round中国式的四舍五入法
- C#委托
- C# 非常有用的方法和短句
- C#多线程编程
- C#,数据类型转换,装箱与拆箱概念,数据类型转换方法
- C#界面设计之树目录TreeView的使用
- C#读取 文本文件 写入textbox
- C#实现文本文件读写方法汇总
- C#关于输入输出流的知识点
- C#遍历指定文件夹中的所有文件
- C# 图片旋转360度程序
- C# 中反射获取某类的子类和根据类型名动态创建对象(转载)
- C# 排列组合
- C#字符串转日期格式
- 细说C#多线程那些事 - 线程同步和多线程优先级
- C#中的线程(一)入门
- C# class示例 0012