Zoe

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication1.App_Code;



namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string uname = textBox1.Text;
            string upwd = textBox2.Text;
            usersdata ud=new usersdata();
            if (ud.hasuser(uname, upwd))
            {
           
             //  
                Form2 f2 = new Form2();
                f2.Show();
                this.Visible = false;
               // Close();
            }
            else { MessageBox.Show("登录失败!!"); }
           

           
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication1.App_Code;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
       
        
        public Form2()

        {
            InitializeComponent();
           
            F5();
            List<users> ulist = new usersdata().selectall();
            foreach (users u in ulist)
            {
                ListViewItem li = new ListViewItem();
                li.Text = u.Ids.ToString();
                li.SubItems.Add(u.Username);
                li.SubItems.Add(u.Password);
                li.SubItems.Add(u.Nickname);
                li.SubItems.Add(u.Sexstr);
                li.SubItems.Add(u.Birthday.ToString("yyyy年MM月dd日"));
                li.SubItems.Add(u.age.ToString());
                li.SubItems.Add(u.nationname);

                listView1.Items.Add(li);
               
            }
           
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3(this);
            f3.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count <= 0)
            {
                MessageBox.Show("请先选择要修改的人员");
                return;
            }
            if (listView1.SelectedItems.Count > 1)
            {
                MessageBox.Show("每次只能修改一个人员的信息");
                return;
            }
            
            Form4 f4 = new Form4(listView1.SelectedItems[0].SubItems[1].Text, this);
            f4.ShowDialog();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form5 f5 = new Form5();
            f5.ShowDialog();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }
       // 刷新方法
        public void F5()
        {
            listView1.Items.Clear();
            usersdata da = new usersdata();
            List<users> ulist = da.selectall();
            foreach (users u in ulist)
            {
                ListViewItem li = new ListViewItem();
                li.Text = u.Ids.ToString();
                li.SubItems.Add(u.Username);
                li.SubItems.Add(u.Password);
                li.SubItems.Add(u.Nickname);
                li.SubItems.Add(u.Sex ? "" : "");
                li.SubItems.Add(u.Birthday.ToString("yyyy年MM月dd日"));
                li.SubItems.Add(u.Nation);
                listView1.Items.Add(li);
            }
        }

    }



     

    
    
}
   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication1.App_Code;

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        Form2 F2 = null;
        public Form3(Form2 f2)
        {
            InitializeComponent();
            F2 = f2;
            //绑定民族信息
            List<nation> list = new nationdata().selectall();
            comboBox1.DataSource = list;
            comboBox1.DisplayMember = "nationname";
            comboBox1.ValueMember = "nationcode";


        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            string uname = textBox1.Text;
            if (uname.Trim().Length <= 0)
            {
                label_uname.Text = "用户名不能为空!!";
                label_uname.ForeColor = Color.Red;
            }
            else 
            {
                bool has = new usersdata().hasusern(uname);
           
                if (has)
                {
                    label_uname.Text = "用户名已存在!!";
                    label_uname.ForeColor = Color.Red;
                }
                else 
                {
                    label_uname.Text = "恭喜,用户名可以使用!!";
                    label_uname.ForeColor = Color.Blue;

                }
            }

            if (textBox2.Text.Equals("")||!labe_pwderror.Text.Equals( "密码正确!"))
            {
                MessageBox.Show("请确保两次密码输入一致");
                return;
            }
            


            users usermodel = new users() { 

 
     
        Username=uname,
                Password=textBox2.Text ,
        Nickname=textBox4.Text,
         Sex=radioButton1.Checked,
   
         Birthday=dateTimePicker1.Value,
      
        Nation=comboBox1.Text,
        
            };


            bool insertok = new usersdata().insertuser(usermodel);




            if (insertok) { MessageBox.Show("注册成功!!"); }
            else { MessageBox.Show("注册失败!!"); }
            F2.F5();
            this.Close();

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.Text == textBox3.Text)
            {
                labe_pwderror.Text = "密码正确!";
                labe_pwderror.ForeColor = Color.Blue;
            }
            else 
            {
                labe_pwderror.Text = "两次密码不一致!";
                labe_pwderror.ForeColor = Color.Red;

            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            string unickname = textBox4.Text;
            if (unickname.Trim().Length <= 0)
            {
                label_nickname.Text = "这家伙很懒!!";
                label_nickname.ForeColor = Color.Red;
            }
            else
            {
                label_nickname.Text = "成功!!";
                label_nickname.ForeColor = Color.Blue;
            }
        }
       
      

    
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication1.App_Code;

namespace WindowsFormsApplication1
{
    public partial class Form4 : Form
    {
        Form2 F2= null;
        public Form4(string uname,Form2 f2)
        {
            InitializeComponent();
            F2 = f2;
            //绑定民族信息
            List<nation> list = new nationdata().selectall();
            comboBox1.DataSource = list;
            comboBox1.DisplayMember = "nationname";
            comboBox1.ValueMember = "nationcode";
            users u = new usersdata().selectuserfromuname(uname);

            if(u!=null)
            {
                textBox1.Text = u.Username;
                textBox4.Text = u.Nickname;
                if(u.Sex==false)
                {
                    radioButton2.Checked = true;
                }
                dateTimePicker1.Value = u.Birthday;
                comboBox1.SelectedValue = u.Nation;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
           DialogResult dr= MessageBox.Show("确定要修改吗?","警告!!",MessageBoxButtons.OKCancel);
            if(dr==DialogResult.OK){
            users u = new users();
            u.Username = textBox1.Text;
            u.Password = textBox2.Text;
            u.Nickname = textBox4.Text;
            u.Sex = radioButton1.Checked;
            u.Birthday = dateTimePicker1.Value;
            u.Nation = comboBox1.SelectedValue.ToString();
            bool pwd = true;
            if(textBox2.Text.Length==0&&textBox3.Text.Length==0)
            {
                pwd = false;
            }

           bool isok= new usersdata().updateuser(u,pwd);
            if(isok)
            {
                MessageBox.Show("修改成功");
                F2.F5();
                this.Close();
            }
            }
            //string uname = textBox1.Text;
        //    if (uname.Trim().Length <= 0)
        //    {
        //        label_uname.Text = "用户名不能为空!!";
        //        label_uname.ForeColor = Color.Red;
        //    }
        //    else
        //    {
        //        bool has = new usersdata().hasusern(uname);
        //        if (has)
        //        {
        //            label_uname.Text = "用户名已存在!!";
        //            label_uname.ForeColor = Color.Red;
        //        }
        //        else
        //        {
        //            label_uname.Text = "恭喜,用户名可以使用!!";
        //            label_uname.ForeColor = Color.Blue;

        //        }
        //    }

        //    users usermodel = new users()
        //    {



        //        Username = uname,
        //        Password = textBox2.Text,
        //        Nickname = textBox4.Text,
        //        Sex = radioButton1.Checked,

        //        Birthday = dateTimePicker1.Value,

        //        Nation = comboBox1.Text,

        //    };
        //    bool insertok = new usersdata().insertuser(usermodel);
        //    if (insertok) { MessageBox.Show("提交成功!!"); }
        //    else { MessageBox.Show("提交失败!!"); }

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.Text == textBox3.Text)
            {
                labe_pwderror.Text = "密码正确!";
                labe_pwderror.ForeColor = Color.Blue;
            }
            else
            {
                labe_pwderror.Text = "两次密码不一致!";
                labe_pwderror.ForeColor = Color.Red;

            }

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.Text == textBox3.Text)
            {
                labe_pwderror.Text = "密码正确!";
                labe_pwderror.ForeColor = Color.Blue;
            }
            else
            {
                labe_pwderror.Text = "两次密码不一致!";
                labe_pwderror.ForeColor = Color.Red;

            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            string unickname = textBox4.Text;
            if (unickname.Trim().Length <= 0)
            {
                label_nickname.Text = "这家伙很懒!!";
                label_nickname.ForeColor = Color.Red;
            }
            else
            {
                label_nickname.Text = "成功!!";
                label_nickname.ForeColor = Color.Blue;
            }
        }

        private void Form4_Load(object sender, EventArgs e)
        {

        }


       
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {bool has=false;
            string ss = textBox1.Text;
            string sql = "server=.;database=master;user=sa;pwd=123;";

          
            SqlConnection conn = new SqlConnection(sql);

            SqlCommand cmd = conn.CreateCommand();
               cmd.CommandText = "select * from users where username=@a ";
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@a", ss);
         
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
                has = true;
            conn.Close();


            if (has)
            {
                cmd.CommandText = "delete from users where username=@a";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@a", ss);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("删除成功!");
            }
            else
            {
                MessageBox.Show("没有此用户!");
            }
        }

         
           
        }
    }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1.App_Code
{
     public class nation
    {
        private string _nationcode;

        public string Nationcode
        {
            get { return _nationcode; }
            set { _nationcode = value; }
        }
        private string _nationname;

        public string Nationname
        {
            get { return _nationname; }
            set { _nationname = value; }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1.App_Code
{
   public class nationdata
    {
       SqlConnection conn;
       SqlCommand cmd;
       public nationdata() 
       {
           conn = new SqlConnection("server=.;database=master;user=sa;pwd=123;");
           cmd = conn.CreateCommand();
       }
       public string selectnationname(string ncode) 
       {
           string end = "暂无";
           cmd.CommandText = "select * from nation where nationcode=@a";
           cmd.Parameters.Clear();
           cmd.Parameters.AddWithValue("@a",ncode);
           conn.Open();
           SqlDataReader dr = cmd.ExecuteReader();
           if(dr.HasRows)
           {
               dr.Read();
               end = dr["nationname"].ToString();
           }
           conn.Close();
           return end;
       }

       public List<nation> selectall() 
       {
           List<nation> list = new List<nation>();
           cmd.CommandText = "select * from nation";
           conn.Open();
           SqlDataReader dr = cmd.ExecuteReader();
           while(dr.Read())
           {
               nation n = new nation();
               n.Nationcode = dr[0].ToString();
               n.Nationname = dr[1].ToString();
               list.Add(n);
           }
           conn.Close();



           return list;
       }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1.App_Code
{
     public class users
    {
        private int _ids;

        public int Ids
        {
            get { return _ids; }
            set { _ids = value; }
        }
        private string _username;

        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }
        private string _password;

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }
        private string _nickname;

        public string Nickname
        {
            get { return _nickname; }
            set { _nickname = value; }
        }
        private bool _sex;

        public bool Sex
        {
            get { return _sex; }
            set { _sex = value; }
        }
       

        public string Sexstr
        {
            get { return _sex?"":""; }
           
        }

        private DateTime _birthday;

        public DateTime Birthday
        {
            get { return _birthday; }
            set { _birthday = value; }
        }
       

        public int age
        {
            get { return DateTime.Now.Year-Birthday.Year; }
          
        }
        private string _nation;

        public string Nation
        {
            get { return _nation; }
            set { _nation = value; }
        }
        public string nationname 
        {
            get { return new nationdata().selectnationname(_nation); }
        }

    }
}
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1.App_Code
{
    public class usersdata
    {
        SqlConnection conn=null;
        SqlCommand cmd=null;
        
        public usersdata()
        {
            conn = new SqlConnection("server=.;database=master;user=sa;pwd=123;");
            cmd = conn.CreateCommand();
        }


        public bool hasuser(string uname,string pwd) 
        {
            bool has = false;
            cmd.CommandText = "select * from users where username=@a and password=@b";
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@a",uname);
            cmd.Parameters.AddWithValue("@b",pwd);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows) { has = true; }
            conn.Close();
            return has;
        }


        public bool hasusern(string uname)
        {
            bool has = 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) { has = true; }
            conn.Close();
            return has;
        }


        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.AddWithValue("@a",u.Username);
            cmd.Parameters.AddWithValue("@b", u.Password);
            cmd.Parameters.AddWithValue("@c", u.Nickname);
            cmd.Parameters.AddWithValue("@d", u.Sex);
            cmd.Parameters.AddWithValue("@e",u.Birthday);
            cmd.Parameters.AddWithValue("@f", u.Nation);
            try
            {
                conn.Open();
                int a = cmd.ExecuteNonQuery();
                if (a > 0) { ok = true; }
            }
            catch
            { ok = false; }
                conn.Close();
            
            return ok;
        }



        public users selectuserfromuname(string uname)
        {
            users u = null;
            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)
            {
                u=new users();
                dr.Read();
                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();
               

            }
            conn.Close();
            return u;
        }


        public bool updateuser(users u,bool changepwd)
        {
            cmd.Parameters.Clear();
            bool isok = false;
            if (changepwd)
            {
                cmd.CommandText = "update users set password=@a,nickname=@b,sex=@c,birthday=@d,nation=@e where username=@f;";
                cmd.Parameters.AddWithValue("@f", u.Username);
            }
            else 
            {
                cmd.CommandText = "update users set nickname=@b,sex=@c,birthday=@d,nation=@e where username=@f;";
            }
          
           
            cmd.Parameters.AddWithValue("@a", u.Password);
            cmd.Parameters.AddWithValue("@b", u.Nickname);
            cmd.Parameters.AddWithValue("@c", u.Sex);
            cmd.Parameters.AddWithValue("@d", u.Birthday);
            cmd.Parameters.AddWithValue("@e", u.Nation);
           
            
                conn.Open();
                int a = cmd.ExecuteNonQuery();
                if (a > 0) { isok = true; }
          
            
            conn.Close();

            return isok;
        }

    }
}

 

posted on 2017-05-10 22:19  口袋里的SKY  阅读(232)  评论(0编辑  收藏  举报