要求:
- 编写一个窗体,往Users表中添加一条数据
- UserName 不能为空,不能重复
- PassWord 两次密码,一致才行
- NickName 如果没填,自动填入“这家伙很懒”
- Sex 用单选按钮来让用户选择
- Birthday 生日
- Nation 下拉列表,显示所有民族的名称,默认选中一个
- 通过用户选择的民族名称 ,填入相应的民族编号
- 注册按钮,只要上面有一项未按照规定填写,那么注册按钮就不会将数据保存至数据库
成品样式:
代码:
主要是数据访问类代码:
using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; namespace WindowsFormsApplication1.App_Code { public class UsersData { SqlConnection conn = null; SqlCommand cmd = null; public UsersData() { conn = new SqlConnection("server=.;database=ODA;user=sa;pwd=123;"); cmd = conn.CreateCommand(); } //检测用户名是否重复 public bool SeleNa(string a) { bool b = false; cmd.CommandText = "select * from Users where UserName=@a"; conn.Open(); cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@a", a); SqlDataReader dr = cmd.ExecuteReader(); if (!dr.HasRows) { b = true; } conn.Close(); return b; } //获取民族编号 public string Code(string a) { string b; cmd.CommandText = "select NationCode from Nation where NationName=@a"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@a", a); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); dr.Read(); b = dr[0].ToString(); conn.Close(); return b; } //提交数据 public bool Submit(string a, string b, string c, bool d, DateTime e, string f) { bool h = false; cmd.CommandText = "insert into Users values(@a,@b,@c,@d,@e,@f)"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@a", a); cmd.Parameters.AddWithValue("@b", b); cmd.Parameters.AddWithValue("@c", c); cmd.Parameters.AddWithValue("@d", d); cmd.Parameters.AddWithValue("@e", e); cmd.Parameters.AddWithValue("@f", f); conn.Open(); int z = cmd.ExecuteNonQuery(); conn.Close(); if (z == 1) { h = true; } return h; } } }
Form代码:
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 WindowsFormsApplication1.App_Code; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //总判断条件 //1.用户名判断: bool Na = false; //(1)如果用户名不为空才能 private void textBox1_Leave(object sender, EventArgs e) { //如果为空 if (textBox1.Text == "") { label8.Text = "用户名不能为空"; pictureBox1.BackgroundImage = Image.FromFile(@"F:/XXXX/WindowsFormsApplication1/jpg/wrong.png"); label8.Visible = true; pictureBox1.Visible = true; Na = false; } else { pictureBox1.Visible = false; } } //(2)查询有没有该用户 private void button1_Click(object sender, EventArgs e) { //如果不为空 if (textBox1.Text != "") { //如果用户不存在 if (new UsersData().SeleNa(textBox1.Text)) { //错号变对号 pictureBox1.BackgroundImage = Image.FromFile(@"F:/XXXX/WindowsFormsApplication1/jpg/wright.png"); pictureBox1.Visible = true; label8.Visible = false; // name条件成立 Na = true; } else { //错号出现 pictureBox1.BackgroundImage = Image.FromFile(@"F:/XXXX/WindowsFormsApplication1/jpg/wrong.png"); pictureBox1.Visible = true; //提示用户 label8.Text = "用户名重复"; label8.Visible = true; //用户名判断 Na = false; } } } //2.密码判断 bool Pwd = false; private void textBox3_Leave(object sender, EventArgs e) { //密码不为空 if (textBox3.Text != "" || textBox2.Text != "") { //密码相同 if (textBox2.Text == textBox3.Text) { //显示对号 pictureBox3.BackgroundImage = Image.FromFile(@"F:/XXXX/WindowsFormsApplication1/jpg/wright.png"); pictureBox3.Visible = true; //隐藏文字 label9.Visible = false; //Pwd条件成立 Pwd = true; } else { //显示错号 pictureBox3.BackgroundImage = Image.FromFile(@"F:/XXXX/WindowsFormsApplication1/jpg/wrong.png"); pictureBox3.Visible = true; //提示文字 label9.Text = "两次密码不一致"; label9.Visible = true; //密码判断 Pwd = false; } } else { //显示错号 pictureBox3.BackgroundImage = Image.FromFile(@"F:/XXXX/WindowsFormsApplication1/jpg/wrong.png"); pictureBox3.Visible = true; //提示文字 label9.Visible = true; label9.Text = "密码不能为空"; //密码判断 Pwd = false; } } //3.昵称判断 private void textBox4_Leave(object sender, EventArgs e) { //如果昵称为空 if (textBox4.Text == "") { //提示文字显示,感叹号显示 label10.Visible = true; pictureBox2.Visible = true; //出现默认昵称 textBox4.Text = "这家伙很懒"; //提交按钮可用 if (Na == true && Pwd == true) { button2.Enabled = true; } else { button2.Enabled = false; } } else { //提示文字隐藏,感叹号隐藏 label10.Visible = false; pictureBox2.Visible = false; //提交按钮可用 if (Na == true && Pwd == true) { button2.Enabled = true; } else { button2.Enabled = false; } } } //生日不能超过今天 private void dateTimePicker1_Enter(object sender, EventArgs e) { dateTimePicker1.MaxDate = DateTime.Now; } //鼠标移入提交按钮 bool Sex; string Nation; private void button2_MouseEnter(object sender, EventArgs e) { //性别转换bool值 if (radioButton1.Checked) { Sex = true; } else { Sex = false; } Nation = new UsersData().Code(comboBox1.Text); } //点击提交按钮 private void button2_Click(object sender, EventArgs e) { if (new UsersData().Submit(textBox1.Text, textBox3.Text, textBox4.Text, Sex, Convert.ToDateTime((dateTimePicker1.Text)), Nation)) { MessageBox.Show("注册成功"); } else { MessageBox.Show("注册失败"); } } } }