private void Button1_Click(object sender, System.EventArgs e)
{
string strconn="server=localhost;uid=sa;pwd=sa;database=aspnet";
string strcmd="select count(*) from user where(username like'"+TextBox1.Text+"'and pwd like '"+TextBox2.Text+"')";
SqlConnection myConnection=new SqlConnection ();
myConnection.ConnectionString=strconn;
SqlCommand myCommand=new SqlCommand (strcmd,myConnection);
myCommand.Connection.Open();
int flag=(int)myCommand.ExecuteScalar();
myCommand.Connection.Close();
myConnection.Close();
if(flag>0)
{
Session["username"]=TextBox1.Text;
Response.Redirect("shiwuban.aspx");
}
else
{
Label1.Text="对不起!用户名和密码不符,请重新输入.";
TextBox1.Text="";
TextBox2.Text=""; }
}
下面是错误提示在关键字 'user' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 在关键字 'user' 附近有语法错误。源错误:
行 60: SqlCommand myCommand=new SqlCommand (strcmd,myConnection);
行 61: myCommand.Connection.Open();
行 62: int flag=(int)myCommand.ExecuteScalar();
行 63: myCommand.Connection.Close();
行 64: myConnection.Close();
源文件: c:\inetpub\wwwroot\changgui\denglu.aspx.cs 行: 62 堆栈跟踪:
[SqlException: 在关键字 'user' 附近有语法错误。]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
System.Data.SqlClient.SqlCommand.ExecuteScalar()
changgui.denglu.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\changgui\denglu.aspx.cs:62
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573
{
string strconn="server=localhost;uid=sa;pwd=sa;database=aspnet";
string strcmd="select count(*) from user where(username like'"+TextBox1.Text+"'and pwd like '"+TextBox2.Text+"')";
SqlConnection myConnection=new SqlConnection ();
myConnection.ConnectionString=strconn;
SqlCommand myCommand=new SqlCommand (strcmd,myConnection);
myCommand.Connection.Open();
int flag=(int)myCommand.ExecuteScalar();
myCommand.Connection.Close();
myConnection.Close();
if(flag>0)
{
Session["username"]=TextBox1.Text;
Response.Redirect("shiwuban.aspx");
}
else
{
Label1.Text="对不起!用户名和密码不符,请重新输入.";
TextBox1.Text="";
TextBox2.Text=""; }
}
下面是错误提示在关键字 'user' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 在关键字 'user' 附近有语法错误。源错误:
行 60: SqlCommand myCommand=new SqlCommand (strcmd,myConnection);
行 61: myCommand.Connection.Open();
行 62: int flag=(int)myCommand.ExecuteScalar();
行 63: myCommand.Connection.Close();
行 64: myConnection.Close();
源文件: c:\inetpub\wwwroot\changgui\denglu.aspx.cs 行: 62 堆栈跟踪:
[SqlException: 在关键字 'user' 附近有语法错误。]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
System.Data.SqlClient.SqlCommand.ExecuteScalar()
changgui.denglu.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\changgui\denglu.aspx.cs:62
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573
请[user]使用string strcmd="select count(*) from [user] where(username like'"+TextBox1.Text+"'and pwd like '"+TextBox2.Text+"')";