这是我一个系统中用户登陆的时候的代码,为什么老是输入用户名和密码后出错,看了一个下午看不出来了,各位大哥帮忙看看啊!!!!
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:  //
 

解决方案 »

  1.   

    myCommand.CommandText = "select count(*)  from user where uer='"+TextBox2.Text+"' and pw='"+TextBox1.Text+"'";
    改成:myCommand.CommandText = "select count(*)  from [user] where uer='"+TextBox2.Text+"' and pw='"+TextBox1.Text+"'";
      

  2.   

    呵呵,数据库的字段名不能随便取的,user就是个关键字.也可以把user列改名为user_name,起名字还是取长点好:)
      

  3.   

    数据表名不能用user的 可能会产生冲突
    你换成users或其他名字试试!
      

  4.   

    user cann't use in table name
      

  5.   

    我把 user 改成 [user]  后好象是正常了,不过数据库里面正确的用户名和密码输进去他老是提示“密码错误或用户不存在!!”
    用户名和密码明明是对的,数据库里面存在的,为什么会这样啊,我把数据库名也改为 uer啦
      

  6.   

    你可以把"select count(*)  from [user] where uer='"+TextBox2.Text+"' and pw='"+TextBox1.Text+"'";这一段在程序运行的时候打印出来,看看具体是些什么,然后到查询分析器去执行,看看能不能找到资料,也许是你没有去空格,也许是其它的原因,自己仔细找找
      

  7.   

    我把
    SELECT COUNT(*) AS Expr1
    FROM uer
    WHERE (uer = 'admin') AND (pw = 'admin')
    放到SQL Server上执行,返回数是1啊,晕死!!!
      

  8.   

    哈哈,成功啦!!
    我把System.Int32 count = (int) myCommand.ExecuteNonQuery();
    改成System.Int32 count = (int) myCommand.ExecuteScalar();  就行啦,哈哈,谢谢大家!特别感谢adminyao(程序傻子)!!!