【问题标题】:System.Data.SqlClient.SqlException: Incorrect syntax near '*'System.Data.SqlClient.SqlException:“*”附近的语法不正确
【发布时间】:2017-12-08 03:37:35
【问题描述】:

我正在尝试使用以下代码创建会话:

SqlConnection conn = new SqlConnection("Data Source=THIRD-I;Initial Catalog=sessionlogin;Integrated Security=True;");
SqlDataAdapter sda = new SqlDataAdapter("Select (*) From logintable Where username='" + UserName.Text + "' and password='" + Password.Text + "'",conn);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows[0][0].ToString()== "1")
{
    Session["user"] = UserName.Text;
    Response.Redirect("welcome.aspx");
}

【问题讨论】:

  • sda.Fill(dt);显示异常
  • 去掉*周围的括号。

标签: asp.net sql-server session exception


【解决方案1】:

您需要数据适配器的记录计数。所以SQL查询应该是new SqlDataAdapter("Select count(*) From logintable Where username='" + UserName.Text + "' and password='" + Password.Text + "'",conn);

【讨论】:

  • 我已经写了上面的查询。现在它说找不到资源。
【解决方案2】:

修复错误:

Select (*) 中删除括号,或者如果您要查找总数,请添加count

必须是select *select count(*)

但您的完整代码需要大量重构。

  1. 处理对象
  2. 参数化查询
  3. 假设您接受用户输入,您应该过滤输入

【讨论】:

  • 我将查询写成select count(*)。现在它说找不到资源
猜你喜欢
  • 2021-03-31
  • 2019-07-17
  • 2014-05-09
  • 2018-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多