1 public string AddUserN = ""; //定义用户名字符串 2 public string paswrd1 = ""; //密码1 3 public string paswrd2 = ""; //确认密码 4 public string userKind = ""; //用户类型 5 public string myconnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ErLake.mdb"; 6 7 8 private void 添加用户frm_Load(object sender, EventArgs e) 9 { 10 11 loadtable(); 12 13 } 14 15 private void button1_Click(object sender, EventArgs e) 16 { 17 userKind = comboBox1.Text.ToString();//获取用户类型 18 //MessageBox.Show(userKind); 19 AddUserN = textBox1.Text.Trim(); 20 paswrd1 = textBox2.Text.Trim(); 21 paswrd2 = textBox3.Text.Trim(); 22 if (userKind == "") 23 { 24 MessageBox.Show("请选择添加的用户类型", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); 25 } 26 else 27 { 28 29 if (AddUserN == "") 30 { 31 MessageBox.Show("请填写用户名", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); 32 } 33 else 34 { 35 string sql = "Select count(*) from tb_admin where 用户名='" + AddUserN + "'";//查询表里用户名为输入用户名的条数(有几条数据) 36 OleDbConnection myconn = new OleDbConnection(myconnstr); 37 //打开数据库 38 myconn.Open(); 39 //执行SQL 40 OleDbCommand commd = new OleDbCommand(sql, myconn);// 41 42 int n = (int)commd.ExecuteScalar();//得到条数 43 myconn.Close();//关闭 44 45 if (n >= 1)//存在 46 { 47 MessageBox.Show("该用户已存在!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); 48 } 49 else 50 { 51 if (paswrd1 == "") 52 { 53 MessageBox.Show("请填写用户密码", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); 54 } 55 else 56 { 57 58 if (paswrd2 == "") 59 { 60 MessageBox.Show("请填写确认的用户密码", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); 61 } 62 else 63 { 64 if (paswrd1 == paswrd2) 65 { 66 //插入字符串 67 string mySQL = "insert into tb_admin (用户名,权限,密码) values ('" + AddUserN + "','" + userKind + "','" + paswrd2 + "')"; 68 //创建连接实例 69 OleDbConnection myconn1 = new OleDbConnection(myconnstr); 70 //打开数据库 71 myconn1.Open(); 72 //执行SQL 73 OleDbCommand commd1 = new OleDbCommand(mySQL, myconn1);// 74 //写入 75 commd1.ExecuteNonQuery(); 76 //命令关闭连接 77 commd1.Connection.Close(); 78 //数据库关闭连接 79 myconn1.Close(); 80 // 81 MessageBox.Show("增加用户成功", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); 82 83 loadtable();//加载用户信息表 84 85 } 86 else 87 { 88 MessageBox.Show("密码填写不一致,请重新填写", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); 89 } 90 } 91 } 92 } 93 } 94 } 95 } 96 97 public void loadtable() 98 { 99 DataTable mytableall = new DataTable(); 100 string mysqlall = "select * from tb_admin "; 101 OleDbDataAdapter da = new OleDbDataAdapter(mysqlall, myconnstr);//数据适配器 102 da.Fill(mytableall);//填表 103 dataGridView1.DataSource = mytableall; 104 }