修改密码
using System;
using System.Data.SqlClient;
using System.Configuration;
namespace SuperMarketMIS.DB {
public class DBHelper {
private SqlConnection connection = null;
public static string ConnectionString {
get {
return ConfigurationManager.AppSettings["cs"];
}
}
public SqlConnection Connection {
get {
if (connection == null) {
string cs = ConfigurationManager.AppSettings["cs"];
connection = new SqlConnection(cs);
}
return connection;
}
}
public void OpenConnection() {
if (connection.State == System.Data.ConnectionState.Closed) {
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken) {
connection.Close();
connection.Open();
}
}
public void CloseConnection() {
if (connection.State == System.Data.ConnectionState.Open) {
connection.Close();
}
}
}
}
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.Windows.Forms;
using SuperMarketMIS.DB;
namespace SuperMarketMIS.GUI {
public partial class ChangePasswordForm : Form {
private string username;
private DBHelper helper = null;
public ChangePasswordForm(string username) {
InitializeComponent();
this.username = username;
helper = new DBHelper();
}
private void btnReturn_Click(object sender, EventArgs e) {
this.Close();
}
private void btnModify_Click(object sender, EventArgs e) {
if (CheckInput()) {
try {
SqlConnection con = helper.Connection;
helper.OpenConnection();
SqlCommand command = con.CreateCommand();
command.CommandText = "update tb_sysuser set password = '" + txtNewPassword.Text + "' where username='" + username + "'";
int result = command.ExecuteNonQuery();
if (result == 1) {
MessageBox.Show("修T改?成??功|", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
else {
MessageBox.Show("修T改?失º?ì败ã??");
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
finally {
helper.CloseConnection();
}
}
else {
MessageBox.Show("原-密??码?错ä?ª误??", "错ä?ª误??", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private bool CheckInput() {
if (txtOrginalPassword.Text == "" || txtNewPassword.Text == "" ||
txtConfirmPassword.Text == "") {
MessageBox.Show("输º?入??不?能??为a空?", "错ä?ª误??", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
else if (txtNewPassword.Text != txtConfirmPassword.Text) {
MessageBox.Show("两??次ä?密??码?不?一??致?", "错ä?ª误??", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
else {
return CheckOriginalPassword();
}
}
private bool CheckOriginalPassword() {
try {
SqlConnection con = helper.Connection;
helper.OpenConnection();
SqlCommand command = con.CreateCommand();
command.CommandText = "select password from tb_sysuser where username='" + username + "'";
SqlDataReader reader = command.ExecuteReader();
if (reader.Read()) {
if (reader["password"].ToString() == txtOrginalPassword.Text) {
return true;
}
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
MessageBox.Show("系?Ì统ª3故?º障?", "错ä?ª误??", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally {
helper.CloseConnection();
}
return false;
}
}
}