我们来设计注册界面:
插入:
groupBox,button,pictureBox,label,ComBox,TextBox,openFileDialog
设置全局变量
1 public static byte[] imgBytesIn; 2 string connstr = ConfigurationManager.ConnectionStrings["SQL"].ConnectionString; 3 Stream ms; 4 byte[] picbyte;
注册按钮:
1 private void button1_Click(object sender, EventArgs e) 2 { 3 string sql = "select Name from StaffAccount where Name='" + txtUserName + "'"; 4 string connstr = ConfigurationManager.ConnectionStrings["SQL"].ConnectionString; 5 SqlConnection conn = new SqlConnection(connstr); 6 SqlCommand cmd = new SqlCommand(sql, conn); 7 conn.Open(); 8 SqlDataReader sdr = cmd.ExecuteReader(); 9 if (sdr.Read()) 10 { 11 lblUserMsg.Text = "用户名已存在,请重新输入!"; 12 } 13 else if (txtUserName.Text.Trim() == "") 14 { 15 lblUserMsg.Text = "用户名不能为空!"; 16 } 17 18 else if (txtPassword.Text.Trim() == "") 19 { 20 lblPwd.Text = "密码不能为空!"; 21 lblPwd.Text = ""; 22 } 23 else if (txtPwdConfirm.Text.Trim() == "") 24 { 25 lblPswConfirm.Text = "验证密码不能为空!"; 26 lblUserMsg.Text = ""; 27 lblPwd.Text = ""; 28 } 29 else if (txtPassword.Text.Trim() != txtPwdConfirm.Text.Trim()) 30 { 31 lblPwd.Text = "2次密码必须一样!"; 32 lblPswConfirm.Text = "请重新输入!"; 33 } 34 else if (txtName.Text.Trim() == "" | txtAge.Text.Trim() == "" | cmboxSex.Text == "" | cmboxOffice.Text == ""| picbyte == null) 35 { 36 lblBaseInfo.Text = "基本信息不完整!"; 37 } 38 else 39 { 40 lblUserMsg.Text = ""; 41 lblPwd.Text = ""; 42 lblPswConfirm.Text = ""; 43 lblBaseInfo.Text = ""; 44 string uType = ""; 45 if (rbtAdmin.Checked) 46 uType = "Administrator"; 47 else if (rbtNormalUser.Checked) 48 uType = "NormalUser"; 49 else 50 uType = "NormalUser"; 51 conn.Close(); 52 string sqlInsert = "insert into StaffAccount(Name,Password,UserType,Image) values(@UserName,@UserPwd,@UserType,@Image)"; 53 string sqlInsertInfo = "insert into StaffInfo(Name,Sex,Age,Office) values(@Name,@Sex,@Age,@Office)"; 54 SqlParameter[] param = { 55 new SqlParameter("@UserName",txtUserName.Text), 56 new SqlParameter("@UserPwd",txtPassword.Text), 57 new SqlParameter("@UserType",uType), 58 new SqlParameter("@Image",picbyte) 59 }; 60 SqlParameter[] paramInfo = { 61 new SqlParameter("@Name",txtName.Text), 62 new SqlParameter("@Age",txtAge.Text), 63 new SqlParameter("@Sex",cmboxSex.Text), 64 new SqlParameter("@Office",cmboxOffice.Text) 65 }; 66 SqlConnection connInsert = new SqlConnection(connstr); 67 SqlCommand cmdInsertInfo = new SqlCommand(sqlInsertInfo, connInsert); 68 SqlCommand cmdInsert = new SqlCommand(sqlInsert, connInsert); 69 connInsert.Open(); 70 cmdInsertInfo.Parameters.AddRange(paramInfo); 71 cmdInsert.Parameters.AddRange(param); 72 int m = cmdInsertInfo.ExecuteNonQuery(); 73 int n = cmdInsert.ExecuteNonQuery(); 74 75 if (n == 0||m==0) 76 { 77 MessageBox.Show("注册失败!,请重新输入"); 78 return; 79 } 80 else 81 { 82 MainForm mainForm = new MainForm(); 83 mainForm.Show(); 84 this.Close(); 85 MessageBox.Show("注册成功!"); 86 } 87 connInsert.Close(); 88 } 89 }
返回按钮:
1 private void button2_Click(object sender, EventArgs e) 2 { 3 this.Close(); 4 }
选择图片按钮:
1 private void button3_Click(object sender, EventArgs e) 2 { 3 openFileDialog1.Filter = "*.jpg|*.jpg"; 4 if (openFileDialog1.ShowDialog(this) == DialogResult.OK) 5 { 6 if ((ms = openFileDialog1.OpenFile()) != null) 7 { 8 picbyte = new byte[ms.Length]; 9 ms.Position = 0; 10 ms.Read(picbyte, 0, Convert.ToInt32(ms.Length)); 11 pictureBox1.Image = System.Drawing.Image.FromFile(openFileDialog1.FileName); 12 } 13 } 14 }
运行情况: