3.0修改密码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Data.SqlClient;
  6 using System.Drawing;
  7 using System.Linq;
  8 using System.Text;
  9 using System.Threading.Tasks;
 10 using System.Windows.Forms;
 11 
 12 namespace 物业管理登录页面
 13 {
 14     public partial class checkpwd : Form
 15     {
 16         public checkpwd()
 17         {
 18             InitializeComponent();
 19         }
 20 
 21         private void textBox3_TextChanged(object sender, EventArgs e)
 22         {
 23             this.textBox3.PasswordChar = '*';
 24         }
 25 
 26         private void textBox4_TextChanged(object sender, EventArgs e)
 27         {
 28             this.textBox4.PasswordChar = '*';
 29         }
 30 
 31         private void button1_Click(object sender, EventArgs e)
 32         {
 33             //判断各个字段是否为空,如果为空提示用户
 34             if ((textBox1.Text==""|| textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" ||radioButton1.Checked!=true)&&radioButton2.Checked!=true)
 35             {
 36                 MessageBox.Show("每个字段都是必填字段!","提示");
 37             }
 38             else
 39             {
 40                 //判断用户输入密码是否相同
 41                 if (textBox3.Text!=textBox4.Text)
 42                 {
 43                     MessageBox.Show("两次输入的密码不相同!", "提示");
 44                 }
 45                 else
 46                 {
 47                     //连接数据库字符串
 48                     string str = "server=.;database=Community system;Integrated Security=true";
 49                     //创建连接对象
 50                     SqlConnection conn = new SqlConnection(str);
 51                     //打开数据库
 52                     conn.Open();
 53                     if (radioButton2.Checked==true)
 54                     {
 55                         //sql语句
 56                         string sql = "select usename from userphone where usename='"+textBox1.Text+"'";
 57                         SqlCommand comm = new SqlCommand(sql, conn);
 58                         SqlDataReader reader = comm.ExecuteReader();
 59                         //conn.Close();
 60                         if (reader.Read())
 61                         {
 62                             string name = reader["usename"].ToString();
 63                             if (name==textBox1.Text)
 64                             {
 65                                 if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
 66                                 {
 67                                     MessageBox.Show("每个字段不能为空!");
 68                                     return;
 69                                 }
 70                                 else if (textBox3.Text!=textBox4.Text)
 71                                 {
 72                                     MessageBox.Show("2次输入的新密码不一样,请重新输入!");
 73                                     return;
 74                                 }
 75                                 else
 76                                 {
 77                                     reader.Close();
 78                                     string sql1 = "update userphone set pwd='" + textBox3.Text + "' where usename='" + name + "'";
 79                                     SqlCommand cmd = new SqlCommand(sql1, conn);
 80                                     int x = cmd.ExecuteNonQuery();
 81                                     if (x > 0)
 82                                     {
 83                                         MessageBox.Show("修改成功!", "提示");
 84                                         this.Close();
 85                                     }
 86                                     else
 87                                     {
 88                                         MessageBox.Show("修改失败!", "提示");
 89                                         this.Close();
 90                                     }
 91                                 }  
 92                             }
 93                             else
 94                             {
 95                                 MessageBox.Show("并无此人信息", "提示");
 96                             }
 97                         }
 98                     }
 99                     if (radioButton1.Checked== true)
100                     {
101                         //sql语句
102                         string sql = "select Adname from Adphone";
103                         SqlCommand comm = new SqlCommand(sql, conn);
104                         SqlDataReader reader = comm.ExecuteReader();
105                         //conn.Close();
106                         if (reader.Read())
107                         {
108                             string name = reader["Adname"].ToString();
109                             if (name == textBox1.Text)
110                             {
111                                 if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
112                                 {
113                                     MessageBox.Show("新密码确认不能为空!");
114                                     return;
115                                 }
116                                 else if (textBox3.Text != textBox4.Text)
117                                 {
118                                     MessageBox.Show("2次输入的新密码不一样,请重新输入!");
119                                     return;
120                                 }
121                                 else
122                                 {
123                                     reader.Close();
124                                     string sql2 = "update Adphone set pwd='" + textBox3.Text + "' where Adname='" + name + "'";
125                                     SqlCommand cmd = new SqlCommand(sql2, conn);
126                                     int x = cmd.ExecuteNonQuery();
127                                     if (x > 0)
128                                     {
129                                         MessageBox.Show("修改成功!", "提示");
130                                         //this.Close();
131                                     }
132                                     else
133                                     {
134                                         MessageBox.Show("修改失败!", "提示");
135                                         //this.Close();
136                                     }
137                                 }
138                             }
139                         }
140                         else
141                         {
142                             MessageBox.Show("并无此人信息", "提示");
143                         }
144                     }
145                 }
146             }
147             
148         }
149     }
150 }

 

posted on 2021-06-04 20:55  钟硕cool  阅读(50)  评论(0编辑  收藏  举报

导航