using System;
using System.Collections.Generic;
using System.ComponentModel;
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 SuperMarkSystem
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
// 数据库连接字符串
private readonly string ConnectionString = "server=.;database=SuperMark;uid=sa;pwd=sa";
#region 窗体加载
// 窗体加载
private void FrmMain_Load(object sender, EventArgs e)
{
SelectStatus(); // 查询状态
SelectUserInfo(); // 查询用户信息
}
#endregion
#region 查询状态
// 查询状态
private void SelectStatus()
{
string sql = "select * from States";
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(sql, ConnectionString);
sda.Fill(dt);
this.cboStatus.DisplayMember = "StatesName";
this.cboStatus.ValueMember = "Id";
this.cboStatus.DataSource = dt;
}
#endregion
#region 查询用户信息
// 查询用户信息
private void SelectUserInfo()
{
string sql = "select u.Id,u.CustomerId,u.CustomerType,u.Score,s.StatesName from UsersInfo as u inner join States as s on u.statusId=s.Id";
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(sql, ConnectionString);
sda.Fill(dt);
this.dgv.DataSource = dt;
}
#endregion
#region 右键菜单打开的时候
// 右键菜单打开的时候
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
if (this.dgv.SelectedRows.Count <= 0)
{
e.Cancel = true;
}
}
#endregion
#region 右键 - DataGridView - 删除
// 右键 - DataGridView - 删除
private void tsmiDelete_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("确定删除吗?", "操作提示:", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result != DialogResult.Yes)
{
return;
}
// 获取当前选中行的“用户编号”列中的值
int id = Convert.ToInt32(this.dgv.SelectedRows[0].Cells["cId"].Value);
#region 删除操作
string sql = "delete from UsersInfo where Id=" + id;
SqlConnection conn = null;
try
{
conn = new SqlConnection(ConnectionString);
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
int count = comm.ExecuteNonQuery();
if (count > 0)
{
MessageBox.Show("删除成功!");
}
else
{
MessageBox.Show("删除失败!");
}
}
catch (Exception)
{
throw;
}
finally
{
if (conn != null)
{
conn.Close();
}
}
#endregion
SelectUserInfo(); // 重新调用查询用户信息
}
#endregion
#region 当更改选中卡别的时候
// 当更改选中卡别的时候
private void CardType_CheckedChanged(object sender, EventArgs e)
{
if (this.rbtnJinKa.Checked)
{
this.txtScore.Text = "500";
}
else if (this.rbtnBoJinKa.Checked)
{
this.txtScore.Text = "2000";
}
else
{
this.txtScore.Text = "5000";
}
}
#endregion
#region 添加
// 点击“添加”
private void btnAdd_Click(object sender, EventArgs e)
{
string customerId = this.txtCustomerId.Text.Trim();
string customerPassword = this.txtCustomerPassword.Text.Trim();
int statusId = Convert.ToInt32(this.cboStatus.SelectedValue);
int score = Convert.ToInt32(this.txtScore.Text.Trim());
string a = "";
if (this.rbtnJinKa.Checked)
{
a = "金卡";
}
else if (this.rbtnBoJinKa.Checked)
{
a = "铂金卡";
}
else
{
a = "钻石卡";
}
#region 添加操作
string sql = string.Format("insert into UsersInfo(CustomerId,CustomerPassword,CustomerType,Score,StatusId) values('{0}','{1}','{2}',{3},{4})", customerId, customerPassword, a, score, statusId);
SqlConnection conn = null;
try
{
conn = new SqlConnection(ConnectionString);
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
int count = comm.ExecuteNonQuery();
if (count > 0)
{
MessageBox.Show("添加成功!");
}
else
{
MessageBox.Show("添加失败!");
}
}
catch (Exception)
{
throw;
}
finally
{
conn.Close();
}
#endregion
ClearInput(); // 清空输入
SelectUserInfo(); // 重新查询用户信息,刷新
}
#endregion
#region 清空输入
// 清空输入
private void ClearInput()
{
this.txtCustomerId.Text = string.Empty;
this.txtCustomerPassword.Text = string.Empty;
this.cboStatus.SelectedIndex = 0;
}
#endregion
#region 退出
// 退出
private void btnExit_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("确定退出吗?","操作提示:",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Application.Exit();
}
}
#endregion
private void cboStatus_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void txtScore_TextChanged(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
}
}