要求:将数据库里的旧密码更新为新密码,就密码从登录窗口获得
代码如下:
SqlConnection mycon = new SqlConnection("data source=20100320-1432\\SQLEXPRESS; database=xitong;integrated security=true");
mycon.Open();
SqlCommand cmd = new SqlCommand("updata guanliyuan set mima=TextBox2.text where id=From1.str2");
mycon.Close();
if(textBox2.Text==textBox3.Text)
//if(xitongDataSet.haschage())
MessageBox.Show("数据以保存至数据库!", "系统提示");
:::str2是登录窗体的公共静态变量
SqlConnection mycon = new SqlConnection("data source=20100320-1432\\SQLEXPRESS; database=xitong;integrated security=true");
mycon.Open();
SqlCommand cmd = new SqlCommand("updata guanliyuan set mima=TextBox2.text where id=From1.str2"); mycon.Close();
if(textBox2.Text==textBox3.Text)
//if(xitongDataSet.haschage())
MessageBox.Show("数据以保存至数据库!", "系统提示");
红色部分需要把变量放出来,不然认为是字串,那就错了,执行不了!~
最好text.trim();
private void bt确认_Click(object sender, EventArgs e)
{
sqlconnection = Connection.GetDbConnection();//从配置文件中获取连接字符串。
if (tbNewPassword.Text == queRen.Text)
{
string sql = string.Format("update 用户管理 set userPassWord='{0}' where userID='{1}'", tbNewPassword.Text.Trim(), Login.name);
SqlCommand cmd = new SqlCommand(sql, sqlconnection);
sqlconnection.Open();
int i = cmd.ExecuteNonQuery();
if(i>0)
{
MessageBox.Show("密码更新成功");
}
}
else
{
MessageBox.Show("新旧密码不一致");
}
sqlconnection.Close();
}
Connection类的代码如下:
class Connection
{
public static SqlConnection GetDbConnection()
{
string source = System.Configuration.ConfigurationManager.AppSettings["source"];
SqlConnection conn = new SqlConnection(source);
return conn;
} }
配置文件代码:
<appSettings>
<add key="source" value="Server=localhost;Initial Catalog=你的数据库;Integrated Security=SSPI;" />
</appSettings>