这是我一个系统中用户登陆的时候的代码,为什么老是输入用户名和密码后出错,看了一个下午看不出来了,各位大哥帮忙看看啊!!!!
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;namespace yzq
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Button Button2;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//string ConnectionString = "Server=(local);User id=sa;Pwd=;Database=yzq";
//SqlConnection thisConnection = new SqlConnection(ConnectionString); } #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
String sConnectionString = "server=(local);uid=sa;pwd=;database=yzq";
System.Data.SqlClient.SqlConnection myConnection = new SqlConnection(sConnectionString); // 打开数据库连接
myConnection.Open();
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "select count(*) from user where uer='"+TextBox2.Text+"' and pw='"+TextBox1.Text+"'";
System.Int32 count = (int) myCommand.ExecuteNonQuery();
//String sTest = "";
//
if(count>0)
{
Response.Write("<script>alert('登录成功!')</script>");
Session["admin"]=TextBox1.Text;
//Response.Redirect("main.aspx");
}
else
{Response.Write("<script>alert('密码错误或用户不存在!!')</script>");}
}
}
}输入用户名和密码后按确定他就提示
在关键字 'user' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 在关键字 'user' 附近有语法错误。源错误:
行 78:
行 79:
行 80: System.Int32 count = (int) myCommand.ExecuteNonQuery();
行 81: //String sTest = "";
行 82: //
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;namespace yzq
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Button Button2;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//string ConnectionString = "Server=(local);User id=sa;Pwd=;Database=yzq";
//SqlConnection thisConnection = new SqlConnection(ConnectionString); } #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
String sConnectionString = "server=(local);uid=sa;pwd=;database=yzq";
System.Data.SqlClient.SqlConnection myConnection = new SqlConnection(sConnectionString); // 打开数据库连接
myConnection.Open();
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "select count(*) from user where uer='"+TextBox2.Text+"' and pw='"+TextBox1.Text+"'";
System.Int32 count = (int) myCommand.ExecuteNonQuery();
//String sTest = "";
//
if(count>0)
{
Response.Write("<script>alert('登录成功!')</script>");
Session["admin"]=TextBox1.Text;
//Response.Redirect("main.aspx");
}
else
{Response.Write("<script>alert('密码错误或用户不存在!!')</script>");}
}
}
}输入用户名和密码后按确定他就提示
在关键字 'user' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 在关键字 'user' 附近有语法错误。源错误:
行 78:
行 79:
行 80: System.Int32 count = (int) myCommand.ExecuteNonQuery();
行 81: //String sTest = "";
行 82: //
解决方案 »
- 删除报错,望高手解决
- detailsview 怎么点击编辑进入编辑模式阿
- 网站站长群,欢迎大家的加入,散分
- [求助] 泛型集合的Remove方法无效。
- 请问如何在vb.net/asp.net调用以cfc?wsdl为结尾的webservice?
- 急!服务器端动态添加Button控件 ,注册事件后, 事件怎么不触发啊?
- 域名泛解析后如何在网站上实现“用户名.xxx.com”
- 可以看到帖子是已经结了的,可所有人都0分。 分跑哪里去了。
- 请问一下,在asp.net中用什么方法可以改变字体~!~!~!!
- 在aspx中怎么才能把IE的工具栏和菜单给抹掉?就象showdialog出来的一样,但用的是redirect方式?谢谢
- 简单问题,关于web控件dropdownlist!在线等急!
- 连接数据库的小问题??
改成:myCommand.CommandText = "select count(*) from [user] where uer='"+TextBox2.Text+"' and pw='"+TextBox1.Text+"'";
你换成users或其他名字试试!
用户名和密码明明是对的,数据库里面存在的,为什么会这样啊,我把数据库名也改为 uer啦
SELECT COUNT(*) AS Expr1
FROM uer
WHERE (uer = 'admin') AND (pw = 'admin')
放到SQL Server上执行,返回数是1啊,晕死!!!
我把System.Int32 count = (int) myCommand.ExecuteNonQuery();
改成System.Int32 count = (int) myCommand.ExecuteScalar(); 就行啦,哈哈,谢谢大家!特别感谢adminyao(程序傻子)!!!