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

时间:2023-03-10 06:14:00
C#_会员管理系统:开发七(用户分类)

登录界面(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; //登录按钮
private void btnLogin_Click(object sender, EventArgs e)
{
//连接数据库语句
using(SqlConnection con=new SqlConnection(connStr))
{
//操作数据库语句
string sql = "select vuserpwd,usertype 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().Trim();
//读取器sdr获取了2列数据 第1列为密码 第2列 即索引为1的是用户类型
string uType = sdr.GetString().Trim() ;
//如果 文本框中输入的密码 ==数据库中的密码
if (pwd == txtPwd.Text)
{
uid = txtName.Text;
time = DateTime.Now;
situation = "登录";
//将登录成功的用户类型 赋给全局变量UserType
//用于获取当前登录 用户的类型
UserType = uType;
//说明在该账户下 密码正确, 系统登录成功
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();
} //当登录窗体为活动窗体时
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)新增一个Label控件:

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

详细代码如下:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace 会员管理系统
{
public partial class VIPMain : Form
{
public VIPMain()
{
InitializeComponent();
} private void btnVIPManager_Click(object sender, EventArgs e)
{
VIPManager vm = new VIPManager();
vm.Show();
this.Hide();
} private void btnVIPLogin_Click(object sender, EventArgs e)
{
VIPLog vipl = new VIPLog();
vipl.GetExitTime();
vipl.AddMsg();
VIPLogin vp = new VIPLogin();
vp.Show();
this.Hide();
} private void btnClose_Click(object sender, EventArgs e)
{
VIPLog vipl = new VIPLog();
vipl.GetExitTime();
vipl.AddMsg();
//彻底的退出
System.Environment.Exit();
} private void btnPwdChange_Click(object sender, EventArgs e)
{
VIPPwdChange vpc = new VIPPwdChange();
vpc.Show();
this.Hide();
} private void VIPMain_Load(object sender, EventArgs e)
{
lblCurrentUser.Text = "当前登录用户为:"+VIPLogin.uid+ " 用户类型:" + VIPLogin.UserType + " 登录时间为:"+VIPLogin.time;
//给当前用户打招呼
//这里通过获取当前用户电脑上的时间 以及判断登录用户的类型 来给不同类型的用户打招呼
// 定义整型变量 intTime 来获取 用户电脑上的具体小时数 然后在如下进行判断
int inttime = VIPLogin.time.Hour;
//获取VIPLogin窗体的全局变量 UserType 用户类型, 用于给不同类型的用户打招呼
string uType = VIPLogin.UserType;
//在凌晨0-6点的时候
if (inttime >= && inttime < )
{
if (uType == "Administrator")
{
lblSayHi.Text = "尊敬的"+ VIPLogin.uid + "您好,现在已夜深,请注意休息!";
}
else if (uType == "NormalUser")
{
lblSayHi.Text = "亲爱的" + VIPLogin.uid + "您好,现在已夜深,请注意休息!";
}
}
//早上6点-中午12点的时候
else if (inttime >= && inttime < )
{
if (uType == "Administrator")
{
lblSayHi.Text = "尊敬的" + VIPLogin.uid + "早上好!";
}
else if (uType == "NormalUser")
{
lblSayHi.Text = "亲爱的" + VIPLogin.uid + "早上好!";
}
}
//中午12点-下午6点的时候
else if (inttime >= && inttime < )
{
if (uType == "Administrator")
{
lblSayHi.Text = "尊敬的" + VIPLogin.uid + "下午好!";
}
else if (uType == "NormalUser")
{
lblSayHi.Text = "亲爱的" + VIPLogin.uid + "下午好!";
}
}
//晚上
else if (inttime >= && inttime < )
{
if (uType == "Administrator")
{
lblSayHi.Text = "尊敬的" + VIPLogin.uid + "晚上好!";
}
else if (uType == "NormalUser")
{
lblSayHi.Text = "亲爱的" + VIPLogin.uid + "晚上好!";
}
}
//否则 默认为
else
{
lblSayHi.Text = "欢迎使用会员管理系统!";
}
//判断用户类型 并给用户设置功能权限
if (uType == "NormalUser")
{
btnRegistration.Enabled = false;
btnLog.Enabled = false;
}
} private void btnLog_Click(object sender, EventArgs e)
{
VIPLog vl=new VIPLog();
vl.Show();
this.Hide();
} private void btnRegistration_Click(object sender, EventArgs e)
{
VIPRegistration vrn = new VIPRegistration();
vrn.Show();
this.Hide();
}
}
}

会员资料管理(VIPManager.cs)详细代码:

 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();
cmbforfieldSelecting.Text = "全局搜索";
cmbforfieldSelecting.Items.Add("全局搜索");
cmbforfieldSelecting.Items.Add("编号");
cmbforfieldSelecting.Items.Add("名字");
cmbforfieldSelecting.Items.Add("性别");
cmbforfieldSelecting.Items.Add("年龄");
cmbforfieldSelecting.Items.Add("地址");
cmbforfieldSelecting.Items.Add("电话");
//添加对用户 类型的判断 用来设置功能按钮的使用权限
if (VIPLogin.UserType == "NormalUser")
{
btnAdd.Enabled = false;
btnDelete.Enabled = false;
btnSave.Enabled = false;
}
} //写一个刷新数据的方法(跟查看数据一样)
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[].Value.ToString());
}
int n = ;
//创建连接数据库对象
SqlConnection conn = new SqlConnection(connStr);
//创建操作数据库对象
SqlCommand cmd = new SqlCommand(sql, conn);
//打开数据库
conn.Open();
//取得ExecuteNonQuery返回的受影响行数,无影响则为0
n = cmd.ExecuteNonQuery();
if (n == )
{
MessageBox.Show("删除操作失败!不存在的ID");
conn.Close();
return;
}
else if (n > )
{
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 = ;
//更新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[].Value.ToString() + "'";
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(sqlupdate, conn);
conn.Open();
n = cmd.ExecuteNonQuery();
if (n == )
{
MessageBox.Show("修改操作失败!");
conn.Close();
return;
}
else if (n > )
{
MessageBox.Show("修改操作成功!");
}
conn.Close();
Refresh();
} //点击dgvManager在文本框上显示
private void dgvManager_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
txtName.Text = dgvManager.CurrentRow.Cells[].Value.ToString();
txtGender.Text = dgvManager.CurrentRow.Cells[].Value.ToString();
txtAge.Text = dgvManager.CurrentRow.Cells[].Value.ToString();
txtAddress.Text = dgvManager.CurrentRow.Cells[].Value.ToString();
txtPhone.Text = dgvManager.CurrentRow.Cells[].Value.ToString();
} string selectedValue;
//事件索引改变时触发
private void cmbforfieldSelecting_SelectedIndexChanged(object sender, EventArgs e)
{
string strSelected = cmbforfieldSelecting.Text;
switch (strSelected)
{
case "全局搜索":
selectedValue = "全局搜索";
break;
case "编号":
selectedValue="vid";
break;
case "名字":
selectedValue = "vname";
break;
case "性别":
selectedValue = "vgender";
break;
case "年龄":
selectedValue = "vage";
break;
case "地址":
selectedValue = "vaddress";
break;
case "电话":
selectedValue = "vphone";
break;
default:
selectedValue = "全局搜索";
break;
}
} private void txtDataforQuery_TextChanged(object sender, EventArgs e)
{
string sql = "";
if (txtDataforQuery.Text.Trim() == "")
{
//执行查询语句
sql = "select * from VipInformation";
}
else if (cmbforfieldSelecting.Text.Trim() == "全局搜索" || selectedValue == "全局搜索")
{
//全字段搜索
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() + "%'";
}
else if (selectedValue == "vid" || selectedValue == "vname" || selectedValue == "vgender" || selectedValue == "vage" || selectedValue == "vaddress" || selectedValue == "vphone")
{
//通过相应的字段进行搜索
sql = "select * from VipInformation where " + selectedValue + " like '%" + txtDataforQuery.Text.Trim() + "%'";
} SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
dgvManager.DataSource = dt;
conn.Close();
} private void btnBack_Click(object sender, EventArgs e)
{
VIPMain vmain = new VIPMain();
vmain.Show();
this.Hide();
} private void btnClose_Click(object sender, EventArgs e)
{
VIPLog vpl = new VIPLog();
vpl.GetExitTime();
vpl.AddMsg();
//彻底的退出
System.Environment.Exit();
} }
}

用户注册界面(VIPRegistration.cs)新增控件,如图:

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

详细代码:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration; namespace 会员管理系统
{
public partial class VIPRegistration : Form
{
public VIPRegistration()
{
InitializeComponent();
} //提交按钮
private void btnOK_Click(object sender, EventArgs e)
{ string connstr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;
SqlConnection conn = new SqlConnection(connstr);
string sql = string.Format("select vusername from VipAccount where vUserName='{0}' ",txtName.Text);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader sda=cmd.ExecuteReader(); //new一个 uType 来获取 radiobutton 点击事件下 触发的用户类型赋值
string uType = "";
if (rdoAdministrator.Checked)
{
uType = "Administrator";
}
else if (rdoNormalUser.Checked)
{
uType = "NormalUser";
}
else
{
uType = "NormalUser";
} //---------------------------
if (txtName.Text.Trim() == "")
{
lblName.Text="用户名不能为空";
return;
}
else if (txtPwd.Text.Trim() == ""|| txtPwdConfirm.Text.Trim()=="")
{
lblPwd.Text = "密码不能为空";
return;
}
else if (txtPwdConfirm.Text.Trim()!= txtPwd.Text.Trim())
{
lblPwdConfirm.Text = "两次密码输入不同,请确认后再输";
return;
}
else if (sda.Read())
{
lblName.Text = "用户名已存在,请重新输入";
return;
}
else
{
conn.Close();
SqlConnection conninsert = new SqlConnection(connstr);
//string insertsql = string.Format("insert into VipAccount(vUserName,vUserPwd) values('{0}','{1}')",txtName.Text,txtPwd.Text);
string insertsql = "insert into VipAccount(vUserName,vUserPwd,UserType) values(@vUserName,@vUserPwd,@UserType) ";
//使用1个SQL参数数组 来装载 需要插入的数据
SqlParameter[] param = {
new SqlParameter("@vUserName",txtName.Text),
new SqlParameter("@vUserPwd",txtPwd.Text),
new SqlParameter("@UserType",uType)
}; SqlCommand cmdinsert = new SqlCommand(insertsql, conninsert);
conninsert.Open();
cmdinsert.Parameters.AddRange(param);
int n = cmdinsert.ExecuteNonQuery();
if (n == )
{
MessageBox.Show("注册失败,请重新输入");
}
else
{
MessageBox.Show("注册成功");
}
conninsert.Close(); }
//conn.Close();
} //返回主菜单
private void btnBack_Click(object sender, EventArgs e)
{
VIPMain vm = new VIPMain();
vm.Show();
this.Hide();
} private void VIPRegistration_Load(object sender, EventArgs e)
{
lblName.Text = "";
lblPwd.Text = "";
lblPwdConfirm.Text = "";
} }
}