请高手帮忙,身份验证的问题??

madinsect 2005-11-30 12:19:37
string strConn = "server=pisim;uid=sa;pwd=sa;database=scms";
string strCmd = "select count(*) from user where(uid like'"+txb_uid.Text+"' and pwd like '"+txb_pwd.Text+"')";
SqlConnection myConnection = new SqlConnection(strConn);
SqlCommand myCommand = new SqlCommand(strCmd,myConnection);
myCommand.Connection.Open();
//检验用户
int flag =(int)myCommand.ExecuteScalar();
myCommand.Connection.Close();
myConnection.Close();
if( flag > 0 )
{
Session["uid"]=txb_uid.Text;
Response.Redirect("main.aspx");
}
else
{
Response.Redirect("login.aspx");
}


代码如上:
运行之后出现这个错:
“异常详细信息: System.Data.SqlClient.SqlException: 在关键字 'user' 附近有语法错误。”
呜呜 想了很久不知道怎么回事?请高手帮忙啊!
...全文
119 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
一夜清懈 2005-11-30
  • 打赏
  • 举报
回复
结贴
madinsect 2005-11-30
  • 打赏
  • 举报
回复
哦!!晕!!对不起,忘记了啊!
感谢啊!!
zhengsb 2005-11-30
  • 打赏
  • 举报
回复
string strCmd = "select count(*) from [user] where(uid like'"+txb_uid.Text+"' and pwd like '"+txb_pwd.Text+"')";

user是SqlServer保留字,类似的还有Date,name等,最好不要使用这类东东做表名或字段名,另外你的代码还有点问题
SqlConnection myConnection =null;
int flag=0;
try
{
myConnection =new SqlConnection(strConn);

SqlCommand myCommand = new SqlCommand(strCmd,myConnection);
myCommand.Connection.Open();
//检验用户
int flag =(int)myCommand.ExecuteScalar();
}
catch
{}
finally
{
if(myConnection!=null) myConnection.Close();//确保关闭数据库

}

一夜清懈 2005-11-30
  • 打赏
  • 举报
回复
user 是关键字 [user]

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧