📄 usermanage.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsApplication1.Systemweihu
{
public partial class UserManage : Form
{
baseclass.BaseOperate boperate = new baseclass.BaseOperate();
SqlConnection cn = new SqlConnection("SERVER=(local);UID=sa;PWD=sa;Trusted_Connection=True;DATABASE=test1");
protected string M_str_sql = "select Username as 用户名,Password as 密码,UserType as 用户类型 from userinfo ";
protected string M_str_table = "userinfo";
public UserManage()
{
InitializeComponent();
}
private void UserManage_Load(object sender, EventArgs e)
{
DataSet myds = boperate.getds(M_str_sql, M_str_table);
dataGridView1.DataSource = myds.Tables[0];
if (myds.Tables[0].Rows.Count > 0)
btnDelete.Enabled = true;
else
btnDelete.Enabled = false;
}
/// <summary>
/// 添加用户信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAdd_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
if (txtUserName.Text.Trim() == "" || txtPassword.Text.Trim() == "" || combUserType.Text.Trim() == "")
MessageBox.Show("请填写完整的用户信息", "提示");
else
{
if (cn.State == ConnectionState.Closed)
cn.Open();
string sql = "select * from userinfo where username ='" + txtUserName.Text.ToString().Trim() + "' and usertype = '"+ combUserType.Text.Trim() +"'";
cmd = new SqlCommand(sql,cn);
if (null != cmd.ExecuteScalar())
{
MessageBox.Show("已经存在相同的用户名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (cn.State == ConnectionState.Open)
cn.Close();
}
else
{
string txtUserNamecur = txtUserName.Text.Trim().ToString().Replace("'", "''");
string txtNewPWDcur = txtPassword.Text.Trim().ToString().Replace("'", "''");
boperate.getcom("insert into userinfo(Username,Password,UserType) values('" + txtUserNamecur + "','" + txtNewPWDcur + "','" + combUserType.Text.Trim() + "')");
UserManage_Load(sender, e);
MessageBox.Show("用户信息添加成功", "提示");
clear();
if (cn.State == ConnectionState.Open)
cn.Close();
}
}
}
/// <summary>
/// 清空用户名和密码输入文本框
/// </summary>
private void clear()
{
txtUserName.Clear();
txtPassword.Clear();
}
/// <summary>
/// 删除用户信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("确定要删除吗?", "删除确定", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
boperate.getcom("delete from userinfo where Username='" + Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim() + "'");
UserManage_Load(sender, e);
MessageBox.Show("删除数据成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
}
/// <summary>
/// 关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnQuit_Click(object sender, EventArgs e)
{
this.Close();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.White;
dataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.LightSlateGray;
dataGridView1.CurrentRow.Selected = true;
txtUserName.Text = Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim();
txtPassword.Text = Convert.ToString(dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value).Trim();
combUserType.Text = Convert.ToString(dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value).Trim();
}
/// <summary>
/// 更新用户信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnUpdata_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("确定要修改用户信息吗?", "确定修改", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
string name = Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim();
string txtUserNamecur = txtUserName.Text.Trim().ToString().Replace("'", "''");
string txtNewPWDcur = txtPassword.Text.Trim().ToString().Replace("'", "''");
boperate.getcom("update userinfo set Username = '" + txtUserNamecur + "',Password='" + txtNewPWDcur + "',UserType='" + combUserType.Text.Trim() + "' where Username ='" + name + "'");
UserManage_Load(sender, e);
MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
clear();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
}
#region 在datagridview中实现鼠标右键单击删除的功能
private void tSMItemDelete_Click(object sender, EventArgs e)
{
//删除选中的多条数据
boperate.delseldata(dataGridView1, "delete from userinfo where username =");
UserManage_Load(sender, e);
MessageBox.Show("删除数据成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -