1 Star 0 Fork 0

wyb/SlSoftSecond

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
UserMessage.cs 5.98 KB
一键复制 编辑 原始数据 按行查看 历史
wyb 提交于 2024-04-08 09:07 . 打印和bug完成
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApp1.lei;
namespace WindowsFormsApp1
{
public partial class UserMessage : Form
{
private DBHelper dBHelper = new DBHelper();
private DataTable dataTable = new DataTable();
private string opertion;
private int uid1;
public UserMessage()
{
InitializeComponent();
}
public UserMessage(string opertion, int uid1)
{
InitializeComponent();
this.opertion = opertion;
this.uid1 = uid1;
}
//查询用户名是否重复
public int selectIdByAccount(string account)
{
dataTable.Clear();
int id = 0;
string sql = "select id from 用户信息 where 用户名 = '{0}'";
sql = string.Format(sql, account);
dataTable = dBHelper.ExecSqlForDataTable(sql);
if (dataTable.Rows.Count > 0)
{
id = (int)dataTable.Rows[0]["id"];
}
return id;
}
//通过id查询需要修改的用户信息
public void selectMessageByid(int id)
{
string sql = "select id,用户名,密码,角色 from 用户信息 where id = {0}";
sql = string.Format(sql, id);
dataTable = dBHelper.ExecSqlForDataTable(sql);
}
//添加
public void addMessage()
{
string account = this.textBox1.Text;
string password = this.textBox2.Text;
string role = this.comboBox1.Text;
if (string.IsNullOrEmpty(account))
{
MessageBox.Show("用户账号不能为空");
return;
}
if (string.IsNullOrEmpty(password))
{
MessageBox.Show("用户密码不能为空");
return;
}
if (string.IsNullOrEmpty(role))
{
MessageBox.Show("用户角色不能为空");
return;
}
int id = selectIdByAccount(account);
if (id > 0)
{
MessageBox.Show("用户账号已经存在");
return;
}
string sql = "insert into 用户信息(用户名,密码,角色) values ('{0}','{1}','{2}')";
sql = string.Format(sql, account, password, role);
int a = dBHelper.ExecSql(sql);
if (a > 0)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
//修改
public void ModityMessage(int id)
{
string account = this.textBox1.Text;
string oldpassword = this.textBox2.Text;
string newpassword = this.textBox3.Text;
string role = this.comboBox1.Text;
if (oldpassword.Equals(this.textBox3.Text))
{
MessageBox.Show("新密码不能和旧密码相同");
return;
}
if (!(this.textBox3.Text).Equals(this.textBox4.Text))
{
MessageBox.Show("两次密码必须相同");
return;
}
if (string.IsNullOrEmpty(account))
{
MessageBox.Show("用户账号不能为空");
return;
}
if (string.IsNullOrEmpty(newpassword))
{
MessageBox.Show("用户密码不能为空");
return;
}
if (string.IsNullOrEmpty(role))
{
MessageBox.Show("用户角色不能为空");
return;
}
int idss = selectIdByAccount(account);
if (idss > 0)
{
if(idss != uid1)
{
MessageBox.Show("用户账号已经存在");
return;
}
}
string sql = "update 用户信息 set 密码='{0}'where id = {1}";
sql = string.Format(sql, newpassword,uid1);
int a = dBHelper.ExecSql(sql);
if (a > 0)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
//页面加载判断是修改还是添加
private void UserMessage_Load(object sender, EventArgs e)
{
if (opertion.Equals("添加"))
{
this.label4.Text = "用户添加";
this.label2.Text = "密码";
this.label5.Visible = false;
this.textBox3.Visible = false;
}
if ((this.textBox2.Text) != (this.textBox4.Text))
{
MessageBox.Show("两次密码不相同");
return;
}
if (opertion.Equals("修改"))
{
this.textBox2.Enabled = false;
selectMessageByid(uid1);
if (dataTable.Rows.Count <= 0)
{
MessageBox.Show("信息不存在");
}
this.textBox1.Text = dataTable.Rows[0]["用户名"].ToString();
this.textBox2.Text = dataTable.Rows[0]["密码"].ToString();
this.comboBox1.Text = dataTable.Rows[0]["角色"].ToString();
this.label4.Text = "用户修改";
this.comboBox1.Enabled = false;
this.textBox1.Enabled = false;
}
}
//确认按钮
private void button1_Click(object sender, EventArgs e)
{
if (opertion.Equals("添加"))
{
addMessage();
}else if (opertion.Equals("修改"))
{
ModityMessage(uid1);
}
else
{
MessageBox.Show("有内鬼终止交易");
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wyhwd/sl-soft-second.git
git@gitee.com:wyhwd/sl-soft-second.git
wyhwd
sl-soft-second
SlSoftSecond
master

搜索帮助