当前位置:编程学习 > C#/ASP.NET >>

C#修改密码 修改

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text.Trim().Length == 0 || textBox3.Text.Trim().Length == 0 || textBox4.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入完整!");
                return;
            }

            if (textBox3.Text.Trim() != textBox4.Text.Trim())
            {
                MessageBox.Show("两次密码输入不一致!");
                return;
            }

            String ConnString = "Data Source=.;Initial Catalog=WageManage;User ID=sa;password=147258369;";
            SqlConnection conn = new SqlConnection(ConnString);
            DataTable dt = new DataTable();

            String sql = "SELECT * FROM stuff where StuffID= StuffID and Password = " + textBox2.Text.Trim();

            try
            {
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(dt);
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message);
            }

            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("旧密码输入错误!");

                dt.Dispose();
            }
            else
            {
                sql = "UPDATE Stuff set Password ='" + textBox3.Text.Trim() + "' where StuffID= '" + "'";

                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = sql;
                cmd.Connection = conn;
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (Exception Err)
                {
                    MessageBox.Show("更新出现异常!");
                }
                finally
                {
                    conn.Close();
                }

                MessageBox.Show("密码更新成功!");

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            this.Hide();
            if (frm2.ShowDialog() == DialogResult.OK)
            {
                this.Show();
            }
        }

        private void Form5_Load(object sender, EventArgs e)
        {

        }
    }
}


点了修改之后提示成功但是数据库数据却没有变化  求大神指点 --------------------编程问答-------------------- sql = "UPDATE Stuff set Password ='" + textBox3.Text.Trim() + "' where StuffID= '" + "'";
StuffID的值呢?! --------------------编程问答-------------------- 用单步调试一点点看,你用了try cath finally虽然出错,但是后面代码任然会执行。要一步步看 --------------------编程问答-------------------- 懂了  谢谢 --------------------编程问答-------------------- 学会调试代码就能自己找到答案了。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,