• 做一个 有限制的登陆窗口


    要求:

    • 编写一个窗体,往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
  • 相关阅读:
    poj3032
    poj2603
    poj2019
    poj2369
    AVI 录像功能压缩算法设置
    陆其明的新书《脚本驱动的应用软件开发方法与实践》
    c# 动态编译
    !!!分享:把bmp格式的图片转化为AVI格式的视频操作的封装类其中对于AVI API的函数的使用较为完整
    视频文件格式和视频编码方式
    activex 控件的id 定义位置+使用ocx控件的客户端程序中对控件定义的文件中控件id定义的位置
  • 原文地址:https://www.cnblogs.com/big-lll/p/6792986.html
Copyright © 2020-2023  润新知