当前位置: 首页 > 知识库问答 >
问题:

SQL“关键字'from'附近的语法不正确。”

凌智
2023-03-14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace RockPaperApp
{
  public partial class WebForm1 : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["New"] != null)
        {
            Response.Redirect("/Game.aspx");
        }
    }

    protected void RegButton_Press(object sender, EventArgs e)
    {
        Response.Redirect("/Register.aspx");
    }

    protected void LogButton_Press(object sender, EventArgs e)
    {
        string username = UsernameLogTxt.Text;

        try
        {
            string conn = ConfigurationManager.ConnectionStrings["UserConS"].ToString();
            string CommandText = "pword from data Username=@username";

            using (SqlConnection connection = new SqlConnection(conn.ToString()))
            using (SqlCommand command = new SqlCommand(CommandText, connection))
            {
                command.Parameters.AddWithValue("@username", username);
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string realpass = reader[0].ToString();

                        if (realpass != PasswordLogTxt.Text)
                        {
                            Response.Write("<span style='color:red'>A Wrong Username of Password has been entered.</span>");
                        }
                        else
                        {
                            Session["New"] = UsernameLogTxt.Text;
                            Response.Redirect("/Game.aspx");
                        }
                    }

                    if (!reader.HasRows)
                    {
                        Response.Write("No such username exists.");
                    }
                }
                connection.Close();
            }

        }
        catch (SqlException ex)
        {
            Response.Write( ex.Message);
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Register.aspx");
    }
  }      
}

共有1个答案

慕容光启
2023-03-14

这不是有效的SQL查询:

"pword from data Username=@username"

也许当您打算复制/粘贴整个查询时,您复制/粘贴了部分查询?不过,它看起来甚至不像是查询的有效部分。

错误告诉您问题出在关键字from,因为这是错误发生后遇到的第一件事。对于SQL错误,总是查看错误位置之前解析的最后一件事,最后一件事是导致错误的原因。在本例中,关键字pword引起了这个问题,因为这在SQL中不是有效的关键字或标识符,查询解析器无法理解它。

 类似资料:
  • 我不熟悉SQL和visual studio等,但我改变了一些不允许我登录应用程序的内容。每当我按下登录按钮时,就会出现此错误 关键字“from”附近的语法不正确 这就是源头所在;

  • 堆栈跟踪: [SqlException(0x80131904):关键字“from”附近语法不正确。] System.Data.sqlclient.sqlConnection.onerror(SqlException exception,Boolean breakConnection,Action`1 wrapCloseInAction)+1791910 System.Data.sqlClient.

  • 当我输入用户名和密码时,我收到了这个错误消息,我不知道这个错误是什么,我如何修复它。错误消息是: 关键字from附近的语法不正确。 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。 异常详细信息:系统。数据SqlClient。SqlException:关键字“from”附近的语法不正确。 源错误: 第30行:Sql命令cmd=new Sq

  • 问题内容: 你好,我试图弄清楚为什么我有这个错误 关键字“表格”附近的语法不正确。 提前Thx 代码 : 问题答案: 发生这种情况是因为TABLE是T-SQL的保留关键字。 如果您确实需要使用该名称,则查询应将TABLE括在方括号中 我强烈建议更改该名称,并对该表的内容使用更具描述性的词

  • 问题内容: 您好,我试图弄清楚为什么在MSSQL中将兼容模式从80切换为100会破坏下面的功能? 这是我的功能: 问题答案: 尝试在with前面加一个半冒号: 给这篇文章读明白,为什么你需要做到这一点。尖刺: 但是,如果CTE不是批处理中的第一条语句,则必须在WITH关键字之前加上分号。作为一种最佳实践,我宁愿在所有CTE前面加上分号,以使这种一致的方法比记住我是否需要分号更容易。 就个人而言,我

  • 问题内容: 我的朋友向我发送了他在服务器2008中编写的命令,这些命令没有问题,但是我的副本和过去的命令在2012中均不起作用。请问有什么原因吗?这是代码: 这是错误: 消息156,级别15,状态1,第6行关键字’COMPUTE’附近的语法错误。 问题答案: 在SQL Server 2012中不再可用,这就是为什么您遇到该错误。看到这个页面: SQL Server 2012中停止使用的数据库引擎功