C#_会员管理系统:开发七(用户分类)

登录界面(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 }

 

posted @ 2016-04-25 10:03  翻滚吧炒鸡蛋  阅读(2519)  评论(0编辑  收藏  举报