📄 usersetting.cs
字号:
}
}
else
{
MessageBox.Show("用户名非法!可能是含有非法字符串。","错误");
textBox1.SelectAll();
return;
}
}
/// <summary>
/// 添加管理员
/// </summary>
private void insertData()
{
if(textBox1.Text == "")
{
MessageBox.Show("管理员名称不能为空!","警告");
textBox1.Focus();
}
else if(textBox2.Text == "" || textBox3.Text == "")
{
MessageBox.Show("密码不能为空!","警告");
}
else if(textBox2.Text == textBox3.Text)
{
CheckString(textBox1.Text,textBox2.Text);
string str = "select * from 管理员 where 名称='" + textBox1.Text + "'";
cmd.CommandText = str;
try
{
dr = cmd.ExecuteReader();
dr.Read();
if(dr.HasRows)
{
if(dr!=null)dr.Close();
MessageBox.Show("用户名已经存在!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
initializeScreen();
return;
}
if(dr!=null)dr.Close();
}
catch(Exception ex)
{
if(dr!=null)dr.Close();
MessageBox.Show("发生如下错误:\n\t" + ex.ToString(),"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
initializeScreen();
return;
}
str = "insert into 管理员 values('" + textBox1.Text + "',";
if(checkBox1.Checked)
{
str += "1,";
}
else
{
str += "0,";
}
if(checkBox2.Checked)
{
str += "1,";
}
else
{
str += "0,";
}
if(checkBox3.Checked)
{
str += "1,";
}
else
{
str += "0,";
}
if(checkBox4.Checked)
{
str += "1,";
}
else
{
str += "0,";
}
if(checkBox5.Checked)
{
str += "1,";
}
else
{
str += "0,";
}
if(checkBox6.Checked)
{
str += "1,";
}
else
{
str += "0,";
}
str += "'" + hm.Encrypto(textBox2.Text) + "')";
cmd.CommandText = str;
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("成功添加记录!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
initializeScreen();
}
catch(Exception ex)
{
MessageBox.Show("发生如下错误:\n\t" + ex.ToString(),"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
initializeScreen();
return;
}
}
else
{
MessageBox.Show("添加记录出错!请确保管理员信息正确!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
initializeScreen();
return;
}
}
/// <summary>
/// 保存更改
/// </summary>
private void updateData()
{
if(textBox1.Text == "")
{
MessageBox.Show("管理员名称不能为空!","警告");
textBox1.Focus();
}
else if(textBox2.Text == "" || textBox3.Text == "")
{
MessageBox.Show("密码不能为空!","警告");
}
else if(textBox2.Text == textBox3.Text)
{
CheckString(textBox1.Text,textBox2.Text);
string str = "update 管理员 set 名称=@名称,系统设置=@系统设置,图书设置=@图书设置,图书管理=@图书管理,读者管理=@读者管理,系统查询=@系统查询,统计排名=@统计排名,密码=@密码 where 名称='"
+ listView1.SelectedItems[0].SubItems[1].Text + "'";
cmd.CommandText = str;
cmd.Parameters.Add(new SqlParameter("@名称",SqlDbType.VarChar,20)).Value = textBox1.Text;
if(checkBox1.Checked)
{
cmd.Parameters.Add(new SqlParameter("@系统设置",SqlDbType.Int,20)).Value = 1;
}
else
{
cmd.Parameters.Add(new SqlParameter("@系统设置",SqlDbType.Int,20)).Value = 0;
}
if(checkBox2.Checked)
{
cmd.Parameters.Add(new SqlParameter("@图书设置",SqlDbType.Int,20)).Value = 1;
}
else
{
cmd.Parameters.Add(new SqlParameter("@图书设置",SqlDbType.Int,20)).Value = 0;
}
if(checkBox3.Checked)
{
cmd.Parameters.Add(new SqlParameter("@图书管理",SqlDbType.Int,20)).Value = 1;
}
else
{
cmd.Parameters.Add(new SqlParameter("@图书管理",SqlDbType.Int,20)).Value = 0;
}
if(checkBox4.Checked)
{
cmd.Parameters.Add(new SqlParameter("@读者管理",SqlDbType.Int,20)).Value = 1;
}
else
{
cmd.Parameters.Add(new SqlParameter("@读者管理",SqlDbType.Int,20)).Value = 0;
}
if(checkBox5.Checked)
{
cmd.Parameters.Add(new SqlParameter("@系统查询",SqlDbType.Int,20)).Value = 1;
}
else
{
cmd.Parameters.Add(new SqlParameter("@系统查询",SqlDbType.Int,20)).Value = 0;
}
if(checkBox6.Checked)
{
cmd.Parameters.Add(new SqlParameter("@统计排名",SqlDbType.Int,20)).Value = 1;
}
else
{
cmd.Parameters.Add(new SqlParameter("@统计排名",SqlDbType.Int,20)).Value = 0;
}
cmd.Parameters.Add(new SqlParameter("@密码",SqlDbType.VarChar,20)).Value = hm.Encrypto(textBox2.Text);
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("保存成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
}
else
{
MessageBox.Show("密码确认错误!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
textBox3.SelectAll();
return;
}
}
/// <summary>
/// 关闭按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, System.EventArgs e)
{
this.Close();
}
/// <summary>
/// 窗体加载代码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void UserSetting_Load(object sender, System.EventArgs e)
{
cmd = Global.conn.CreateCommand();
initializeScreen();
}
/// <summary>
/// 新增管理员
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, System.EventArgs e)
{
isUpdate = false;
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
checkBox1.Enabled = true;
checkBox2.Enabled = true;
checkBox3.Enabled = true;
checkBox4.Enabled = true;
checkBox5.Enabled = true;
checkBox6.Enabled = true;
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
checkBox4.Checked = false;
checkBox5.Checked = false;
checkBox6.Checked = false;
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
button4.Enabled = true;
button1.Enabled = false;
textBox1.Focus();
}
/// <summary>
/// 显示用户信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView1_Click(object sender, System.EventArgs e)
{
if(listView1.SelectedItems[0].SubItems[0].Text != "")
{
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
checkBox1.Enabled = false;
checkBox2.Enabled = false;
checkBox3.Enabled = false;
checkBox4.Enabled = false;
checkBox5.Enabled = false;
checkBox6.Enabled = false;
button1.Enabled = true;
button2.Enabled = true;
button4.Enabled = false;
button5.Enabled = true;
textBox1.Text = listView1.SelectedItems[0].SubItems[1].Text;
if(listView1.SelectedItems[0].SubItems[2].Text=="是")
{
checkBox1.Checked = true;
}
else
{
checkBox1.Checked = false;
}
if(listView1.SelectedItems[0].SubItems[3].Text=="是")
{
checkBox2.Checked = true;
}
else
{
checkBox2.Checked = false;
}
if(listView1.SelectedItems[0].SubItems[4].Text=="是")
{
checkBox3.Checked = true;
}
else
{
checkBox3.Checked = false;
}
if(listView1.SelectedItems[0].SubItems[5].Text=="是")
{
checkBox4.Checked = true;
}
else
{
checkBox4.Checked = false;
}
if(listView1.SelectedItems[0].SubItems[6].Text=="是")
{
checkBox5.Checked = true;
}
else
{
checkBox5.Checked = false;
}
if(listView1.SelectedItems[0].SubItems[7].Text=="是")
{
checkBox6.Checked = true;
}
else
{
checkBox6.Checked = false;
}
/*
string str = "select 密码 from 管理员 where 名称='" + listView1.SelectedItems[0].SubItems[1].Text + "'";
cmd.CommandText = str;
try
{
dr = cmd.ExecuteReader();
dr.Read();
if(dr.HasRows)
{
textBox2.Text = dr.GetString(0);
textBox3.Text = dr.GetString(0);
}
dr.Close();
}
catch(Exception ex)
{
if(dr!=null)dr.Close();
MessageBox.Show("发生如下错误:\n\t" + ex.ToString(),"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
*/
}
else
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
checkBox4.Checked = false;
checkBox5.Checked = false;
checkBox6.Checked = false;
button2.Enabled = false;
button5.Enabled = false;
}
}
/// <summary>
/// 保存记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, System.EventArgs e)
{
if(isUpdate)
{
updateData();
initializeScreen();
}
else
{
insertData();
}
}
/// <summary>
/// 准备更改数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, System.EventArgs e)
{
isUpdate = true;
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
checkBox1.Enabled = true;
checkBox2.Enabled = true;
checkBox3.Enabled = true;
checkBox4.Enabled = true;
checkBox5.Enabled = true;
checkBox6.Enabled = true;
button4.Enabled = true;
button1.Enabled = true;
textBox1.Focus();
}
/// <summary>
/// 删除用户记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, System.EventArgs e)
{
if(listView1.SelectedItems[0].SubItems[1].Text!="")
{
string str = "delete from 管理员 where 名称='" + listView1.SelectedItems[0].SubItems[1].Text + "'";
cmd.CommandText = str;
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("成功删除用户!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
initializeScreen();
}
catch(Exception ex)
{
MessageBox.Show("发生如下错误:\n\t" + ex.ToString (),"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
}
else
{
MessageBox.Show("未选中任何记录!请重新选择!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -