您的位置:首页 > 编程语言 > C#

C#_会员管理系统:开发二(会员资料管理界面的‘增删改查’)

2016-04-22 11:26 711 查看
会员资料管理界面:

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



窗体中的控件dgvManager更改FullRowSelect属性(点击选中效果)为:FullRowSelect



会员资料管理界面窗体的详细代码:

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 = "";
}
}
}
}
}

}
}


运行效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: