zoukankan      html  css  js  c++  java
  • 做一个 有限制的登陆窗口

    要求:

    • 编写一个窗体,往Users表中添加一条数据
    • UserName 不能为空,不能重复
    • PassWord 两次密码,一致才行
    • NickName 如果没填,自动填入“这家伙很懒”
    • Sex 用单选按钮来让用户选择
    • Birthday 生日
    • Nation 下拉列表,显示所有民族的名称,默认选中一个
    • 通过用户选择的民族名称 ,填入相应的民族编号
    • 注册按钮,只要上面有一项未按照规定填写,那么注册按钮就不会将数据保存至数据库  

    布局大概是这个样子。性别默认属性为true  生日用的datatimepiker  民族是comboBox  属性dropdownstyle修改为dropdownlist

     这是窗体代码,  每个控件的限制,和  事件 。

    public partial class Form1 : Form
        {
            bool usernameisok = false; //判断用户名
            bool pwdisok = false;//判断密码
    
            public Form1()
            {
                InitializeComponent();
    
                //绑定民族信息
                List<UserNation> nlist = new UserNationData().SelectAll();
    
                comboBox1.DataSource = nlist;
                comboBox1.DisplayMember = "NationName";
                comboBox1.ValueMember = "NationCode";
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (!usernameisok)
                {
                    textBox1.BackColor = Color.Red;
                }
                else if (!pwdisok)
                {
                    textBox2.BackColor = Color.Red;
                    textBox3.BackColor = Color.Red;
                }
                else
                {
                    Users u = new Users();
                    u.UserName = textBox1.Text.Trim();
                    u.PassWord = textBox2.Text;
                    if (textBox4.Text.Trim().Length > 0)
                        u.NickName = textBox4.Text.Trim();
                    else
                        u.NickName = "这家伙很懒";
    
                    u.Sex = radioButton1.Checked;
    
                    u.Birthday = dateTimePicker1.Value;
    
                    u.Nation = comboBox1.SelectedValue.ToString();
    
                    bool insertOK = new UsersData().InsertUser(u);
                    if (insertOK)
                    {
                        MessageBox.Show("注册成功!");
                    }
                    else
                    {
                        MessageBox.Show("注册失败!服务器连接失败,请联系110!");
                    }
    
                }
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                textBox1.BackColor = Color.White;
                //判断用户名
                string uname = textBox1.Text;
                if (uname.Trim().Length <= 0)
                {
                    label_usernameMsg.Text = "用户名不能为空!";
                    label_usernameMsg.ForeColor = Color.Red;
                    usernameisok = false;
                }
                else
                {
                    bool has = new UsersData().HasUserName(uname);
    
                    if (has)
                    {
                        label_usernameMsg.Text = "用户名已存在!";
                        label_usernameMsg.ForeColor = Color.Red;
                        usernameisok = false;
                    }
                    else
                    {
                        label_usernameMsg.Text = "恭喜!用户名可以使用!";
                        label_usernameMsg.ForeColor = Color.Green;
                        usernameisok = true;
                    }
                }
            }
    
            private void textBox3_TextChanged(object sender, EventArgs e)
            {
                textBox2.BackColor = Color.White;
                textBox3.BackColor = Color.White;
    
                if (textBox2.Text == textBox3.Text)
                {
                    label_pwdError.Text = "正确!";
                    label_pwdError.ForeColor = Color.Green;
                    pwdisok = true;
                }
                else
                {
                    label_pwdError.Text = "两次密码不一致";
                    label_pwdError.ForeColor = Color.Red;
                    pwdisok = false;
                }
            }
    
        }
    }

    这是用了两个数据访问类   

    代码使用了里面的方法

     public class UserNationData
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;
    
            public UserNationData()
            {
                conn = new SqlConnection("server=.;database=wowowo;user=sa;pwd=123");
                cmd = conn.CreateCommand();
            }
    
            public List<UserNation> SelectAll()
            {
                List<UserNation> list = new List<UserNation>();
    
                cmd.CommandText = "select *from UserNation";
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    UserNation un = new UserNation();
                    un.NationCode = dr[0].ToString();
                    un.NationName = dr[1].ToString();
                    list.Add(un);
                }
                conn.Close();
                return list;
            }
    
    
    
        }
    View Code
     public class UsersData
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;
    
            public UsersData()
            {
                conn = new SqlConnection("server=.;database=wowowo;user=sa;pwd=123");
                cmd = conn.CreateCommand();
            }
    
    
            public List<Users> SelectAll()
            {
                List<Users> list = new List<Users>();
                cmd.CommandText = "select *from Users";
    
                try
                {
                    conn.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        Users u = new Users();
                        u.Ids = Convert.ToInt32(dr["ids"]);
                        u.UserName = dr["UserName"].ToString();
                        u.PassWord = dr["PassWord"].ToString();
                        u.NickName = dr["NickName"].ToString();
                        u.Sex = Convert.ToBoolean(dr["Sex"]);
                        u.Birthday = Convert.ToDateTime(dr["Birthday"]);
                        u.Nation = dr["Nation"].ToString();
    
                        list.Add(u);
                    }
                }
                catch
                {
                    Users u = new Users();
                    u.UserName = "服务器连接失败!";
                    u.PassWord = "UserError500";
                    list.Add(u);
                }
                finally
                {
                    conn.Close();
                }
                return list;
            }
    
            public bool HasUserName(string uname)
            {
                bool b = false;
    
                cmd.CommandText = "select *from Users where UserName = @a";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@a", uname);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    b = true;
                }
                conn.Close();
                return b;
            }
    
            public bool InsertUser(Users u)
            {
                bool ok = false;
    
                cmd.CommandText = "insert into Users values(@a,@b,@c,@d,@e,@f);";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@a", u.UserName);
                cmd.Parameters.Add("@b", u.PassWord);
                cmd.Parameters.Add("@c", u.NickName);
                cmd.Parameters.Add("@d", u.Sex);
                cmd.Parameters.Add("@e", u.Birthday);
                cmd.Parameters.Add("@f", u.Nation);
                try
                {
                    conn.Open();
                    int a = cmd.ExecuteNonQuery();
                    if (a > 0)
                    {
                        ok = true;
                    }
                }
                catch { ok = false; }
                conn.Close();
                return ok;
            }
    
    
        }
    View Code
  • 相关阅读:
    索引
    varnish它是一款高性能的http缓存服务器
    前端性能优化浅谈
    锁机制-SQL Server 数据库
    NET Core中NuGet包
    “干掉” if...else
    混搭.NET技术
    MONO x64 amd_x64
    跨平台移动开发UI语言 -XAML
    使用LinqToExcel读取Excel
  • 原文地址:https://www.cnblogs.com/big-lll/p/6792986.html
Copyright © 2011-2022 走看看