c#修改密码代码运行时显示密码附近有语法错误,求大神指点一下
代码如下:privatevoidbutton1_Click(objectsender,EventArgse){if(textBox1.Text.Trim().Length...
代码如下:
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim().Length == 0 || textBox2.Text.Trim().Length == 0 || textBox3.Text.Trim().Length == 0)
{
MessageBox.Show("请输入完整!");
return;
}
if (textBox2.Text.Trim() != textBox3.Text.Trim())
{
MessageBox.Show("两次密码输入不一致!");
return;
}
String ConnString = "Data Source=.;Initial Catalog=IntelligentProducePaperDB;Integrated Security=True";
SqlConnection conn = new SqlConnection(ConnString);
DataTable dt = new DataTable();
String sql = "SELECT * FROM UserInfo where 用户名=管理员 and 密码 =xxxxxx " + textBox1.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 UserInfo set 密码 ='" + textBox2.Text.Trim() + "' where 用户名=管理员 '" + "'";
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 button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim().Length == 0 || textBox2.Text.Trim().Length == 0 || textBox3.Text.Trim().Length == 0)
{
MessageBox.Show("请输入完整!");
return;
}
if (textBox2.Text.Trim() != textBox3.Text.Trim())
{
MessageBox.Show("两次密码输入不一致!");
return;
}
String ConnString = "Data Source=.;Initial Catalog=IntelligentProducePaperDB;Integrated Security=True";
SqlConnection conn = new SqlConnection(ConnString);
DataTable dt = new DataTable();
String sql = "SELECT * FROM UserInfo where 用户名=管理员 and 密码 =xxxxxx " + textBox1.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 UserInfo set 密码 ='" + textBox2.Text.Trim() + "' where 用户名=管理员 '" + "'";
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("密码更新成功!");
}
}
} 展开
1个回答
展开全部
两处sql分别是
string sql = "SELECT * FROM UserInfo where 用户名='管理员' and 密码 ='" + textBox1.Text.Trim()+"'";
sql = "UPDATE UserInfo set 密码 ='" + textBox2.Text.Trim() + "' where 用户名='管理员'";
更多追问追答
追问
那如果我有多个用户,是不是改成
string sql = "SELECT * FROM UserInfo where 用户名=' ' and 密码 ='" + textBox1.Text.Trim()+"'";
sql = "UPDATE UserInfo set 密码 ='" + textBox2.Text.Trim() + "' where 用户名=' '";
追答
没你这样写的,我不清楚你这是 winform还是web,如果是winform,你需要在登录窗口把登录的用户名保存下来,比如就放在program.cs里,加一行
public static string myuser="";
然后登录录成功之后就写
program.myuser=登录的用户名;
然后上面两处sql就是
string sql =string.Format("SELECT * FROM UserInfo where 用户名='{0}' and 密码 ='{1}'",program.myuser,textBox1.Text.Trim());
sql =string.Format("UPDATE UserInfo set 密码 ='{0}' where 用户名='{1}'",textBox2.Text.Trim(),program.myuser);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询