zoukankan      html  css  js  c++  java
  • C#_会员管理系统:开发二(会员资料管理界面的‘增删改查’)

    会员资料管理界面:

    新建一个窗体,窗体界面和控件如下:

    窗体中的控件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 }

    运行效果:

  • 相关阅读:
    [LeetCode]Sort List
    [LeetCode]Single Number II
    合并两个排序的列表
    翻转链表
    链表中倒数第k个结点
    调整数组顺序使奇数位于偶数前面
    数值的整数次方
    二进制中1的个数
    矩形覆盖
    变态跳台阶
  • 原文地址:https://www.cnblogs.com/start-from-scratch/p/5420588.html
Copyright © 2011-2022 走看看