コード例 #1
0
        private void button_submit_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(forgotpw.connectionstr);

            conn.Open();
            String         sql = "select * from [dbo].[user] where uname='" + text_un.Text + "'and upw='" + text_pw.Text + "'and uphone='" + text_phone.Text + "'and uemail='" + text_email.Text + "'and uredate='" + dateTimePicker1.Value + "'";
            SqlDataAdapter adp = new SqlDataAdapter(sql, conn);
            DataSet        ds  = new DataSet();

            adp.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DialogResult result = MessageBox.Show("Sure to delete?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    string        sqluid  = "select * from [dbo].[user] where uname='" + text_un.Text + "'";
                    SqlCommand    finduid = new SqlCommand(sqluid, conn);
                    SqlDataReader uidread = finduid.ExecuteReader();
                    uidread.Read();
                    string uid = uidread["uid"].ToString().Trim();
                    uidread.Close();
                    SqlCommand cmd1 = conn.CreateCommand();
                    cmd1.CommandText = "delete from [dbo].[active] where uid='" + uid + "'";
                    cmd1.ExecuteNonQuery();
                    SqlCommand cmd2 = conn.CreateCommand();
                    cmd2.CommandText = "delete from [dbo].[user] where uname='" + text_un.Text + "'";
                    cmd2.ExecuteNonQuery();
                    conn.Close();
                    MessageBox.Show("Succeeded!");
                    string un = text_un.Text;
                    this.Close();
                    fm1.Close();
                    login form = new login();
                    form.Show();
                }
            }
            else
            {
                warn.Visible = true;
            }
        }
コード例 #2
0
        protected void button_submit_Click(object sender, EventArgs e)//以插入表现注册新账号
        {
            if (text_un.Text == "")
            {
                username.ForeColor = Color.FromArgb(255, 0, 0);
            }
            else
            {
                username.ForeColor = Color.FromArgb(255, 255, 255);
            }
            if (text_pw.Text == "")
            {
                password.ForeColor = Color.FromArgb(255, 0, 0);
            }
            else
            {
                password.ForeColor = Color.FromArgb(255, 255, 255);
            }
            if (text_confirmpw.Text == "")
            {
                confirmpw.ForeColor = Color.FromArgb(255, 0, 0);
            }
            else
            {
                confirmpw.ForeColor = Color.FromArgb(255, 255, 255);
            }
            if (text_phone.Text == "")
            {
                phone.ForeColor = Color.FromArgb(255, 0, 0);
            }
            else
            {
                phone.ForeColor = Color.FromArgb(255, 255, 255);
            }
            if (text_confirmpw.Text != text_pw.Text)
            {
                confirmpw.ForeColor = Color.FromArgb(255, 0, 0);
            }

            int lenun, lenpw;

            lenun = System.Text.Encoding.Default.GetByteCount(text_un.Text);
            lenpw = System.Text.Encoding.Default.GetByteCount(text_pw.Text);
            Regex number = new Regex(@"^[0-9]+$");    //仅数字的正则表达式
            Regex letter = new Regex(@"^[A-Za-z]+$"); //仅字母的正则表达式

            if ((lenun < 8 || lenun > 16) && (!number.IsMatch(text_un.Text) || !letter.IsMatch(text_un.Text) || !text_un.Text.Contains("_")) && text_un.Text != "")
            {
                warn2.Visible = true;
            }
            else
            {
                warn2.Visible = false;
            }
            if ((lenpw < 8 || lenpw > 16) && (!number.IsMatch(text_pw.Text) || !letter.IsMatch(text_pw.Text)) && text_pw.Text != "")
            {
                warn2.Visible = true;
            }
            else
            {
                warn2.Visible = false;
            }
            if ((!number.IsMatch(text_phone.Text)) && text_phone.Text != "")
            {
                warn3.Visible = true;
            }
            else
            {
                warn3.Visible = false;
            }
            if (!text_email.Text.Contains("@") && text_email.Text != "")
            {
                warn3.Visible = true;
            }
            else
            {
                warn3.Visible = false;
            }

            if (username.ForeColor != Color.FromArgb(255, 0, 0) && password.ForeColor != Color.FromArgb(255, 0, 0) && confirmpw.ForeColor != Color.FromArgb(255, 0, 0) && phone.ForeColor != Color.FromArgb(255, 0, 0) && warn2.Visible == false && warn3.Visible == false)
            {
                SqlConnection conn = new SqlConnection(register.connectionstr);
                conn.Open();
                string        sql        = String.Format("select uname from [dbo].[user] where uname = '{0}'", text_un.Text);
                SqlCommand    command    = new SqlCommand(sql, conn);
                SqlDataReader datareader = command.ExecuteReader();
                int           i;
                for (i = 0; datareader.Read(); i++)
                {
                }
                if (i > 0)
                {
                    warn1.Visible = true;
                }
                else//未注册,检查填写邮箱,向数据表中插入相应的用户名和密码
                {
                    datareader.Close();
                    string     newuser = String.Format("insert into [dbo].[user](uname,upw,uban,uphone,uemail,uredate) values('{0}','{1}','{2}','{3}','{4}','{5}')", text_un.Text, text_pw.Text, 0, text_phone.Text, text_email.Text, DateTime.Now.ToShortDateString().ToString());
                    SqlCommand cmd     = new SqlCommand(newuser, conn);
                    int        j       = cmd.ExecuteNonQuery();
                    if (j > 0)
                    {
                        MessageBox.Show("Welcome");
                        this.DialogResult = DialogResult.Retry;
                        conn.Close();
                        login form = new login();
                        form.Show();
                        this.Hide();
                    }
                }
            }
            label_code.Text       = randomcode.code();
            text_code.Text        = "";
            button_submit.Enabled = false;
        }