public string AddUserN = ""; //定义用户名字符串
public string paswrd1 = ""; //密码1
public string paswrd2 = ""; //确认密码
public string userKind = ""; //用户类型
public string myconnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ErLake.mdb"; private void 添加用户frm_Load(object sender, EventArgs e)
{ loadtable(); } private void button1_Click(object sender, EventArgs e)
{
userKind = comboBox1.Text.ToString();//获取用户类型
//MessageBox.Show(userKind);
AddUserN = textBox1.Text.Trim();
paswrd1 = textBox2.Text.Trim();
paswrd2 = textBox3.Text.Trim();
if (userKind == "")
{
MessageBox.Show("请选择添加的用户类型", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
else
{ if (AddUserN == "")
{
MessageBox.Show("请填写用户名", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
else
{
string sql = "Select count(*) from tb_admin where 用户名='" + AddUserN + "'";//查询表里用户名为输入用户名的条数(有几条数据)
OleDbConnection myconn = new OleDbConnection(myconnstr);
//打开数据库
myconn.Open();
//执行SQL
OleDbCommand commd = new OleDbCommand(sql, myconn);// int n = (int)commd.ExecuteScalar();//得到条数
myconn.Close();//关闭 if (n >= )//存在
{
MessageBox.Show("该用户已存在!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
else
{
if (paswrd1 == "")
{
MessageBox.Show("请填写用户密码", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
else
{ if (paswrd2 == "")
{
MessageBox.Show("请填写确认的用户密码", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
else
{
if (paswrd1 == paswrd2)
{
//插入字符串
string mySQL = "insert into tb_admin (用户名,权限,密码) values ('" + AddUserN + "','" + userKind + "','" + paswrd2 + "')";
//创建连接实例
OleDbConnection myconn1 = new OleDbConnection(myconnstr);
//打开数据库
myconn1.Open();
//执行SQL
OleDbCommand commd1 = new OleDbCommand(mySQL, myconn1);//
//写入
commd1.ExecuteNonQuery();
//命令关闭连接
commd1.Connection.Close();
//数据库关闭连接
myconn1.Close();
//
MessageBox.Show("增加用户成功", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); loadtable();//加载用户信息表 }
else
{
MessageBox.Show("密码填写不一致,请重新填写", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
}
}
}
}
}
} public void loadtable()
{
DataTable mytableall = new DataTable();
string mysqlall = "select * from tb_admin ";
OleDbDataAdapter da = new OleDbDataAdapter(mysqlall, myconnstr);//数据适配器
da.Fill(mytableall);//填表
dataGridView1.DataSource = mytableall;
}
05-11 22:34