asp.net 程序修改密码

来源:百度知道 编辑:UC知道 时间:2024/06/09 02:46:44
我自己编写的
protected void Button1_Click(object sender, EventArgs e)
{
string strCon = "data source=.\\SQLEXPRESS;password=sa;user id=sa;database=training";

SqlConnection con = new SqlConnection(strCon);
SqlCommand cmd = con.CreateCommand();

cmd.CommandText = "update UserInfo set passWord='" + TextBox2.Text + "'where userName="+Convert.ToString(TextBox1.Text);
con.Open();
cmd.ExecuteNonQuery();
Label1.Text = "修改成功";
con.Close();

}
但是运行提示错误错误原因:内部错误:System.Data.SqlClient.SqlException: '=' 附近有语法错误。

源码:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="edit_password.aspx.cs" Inherits="Default2" Title="Untitled Page" %>
<asp:Content ID="Content1"

cmd.CommandText = "update UserInfo set passWord='" + TextBox2.Text + "'where userName='"+ TextBox1.Text+"'";

这样看下text属性本来就是string类型了,不用在转换。象这样的语句最好用参数表示。不燃会不兼容特殊字符。

username 没有用单引号括起来
cmd.CommandText = "update UserInfo set passWord='" + TextBox2.Text + "'where userName='"+Convert.ToString(TextBox1.Text)+"'";

在头文件中加上using System.Data.SqlClient;

单引号 建议用 cmd.Parameters.AddWithValue("","")