C# 尝试按id搜索,然后在gridview中显示数据,但出现语法错误

C# 尝试按id搜索,然后在gridview中显示数据,但出现语法错误,c#,asp.net,C#,Asp.net,请解释一下我为什么会犯这个错误好吗?我知道我的代码没有安全性,但这不是供公众访问的。你能解释一下我哪里出了问题吗?我想按id搜索,然后在gridview中显示数据。我已将gridview与数据链接 异常详细信息:System.Data.SqlClient.SqlException:“EquipmentRegister”附近的语法不正确行:53在源错误中突出显示 源错误: Line 51: SqlDataAdapter da = new SqlDataAdapter(que

请解释一下我为什么会犯这个错误好吗?我知道我的代码没有安全性,但这不是供公众访问的。你能解释一下我哪里出了问题吗?我想按id搜索,然后在gridview中显示数据。我已将gridview与数据链接

异常详细信息:System.Data.SqlClient.SqlException:“EquipmentRegister”附近的语法不正确行:53在源错误中突出显示

源错误:

    Line 51:         SqlDataAdapter da = new SqlDataAdapter(queryString, con);
    Line 52:         DataSet ds = new DataSet();
    Line 53:         da.Fill(ds);
    Line 54:         gvRegister.DataSource = ds;
    Line 55:         gvRegister.DataBind();
以下是apsx文件:

private void rep_bind()
    {
        string theConnectString = System.Configuration.ConfigurationManager.ConnectionStrings["EquipRegisterConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(theConnectString);
        string queryString = ("SELECT * EquipmentRegister WHERE EngineerRef like '" + txtEngRef.Text + "%'");
        SqlCommand com = new SqlCommand(queryString, con);
        com.Connection = con;
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(theConnectString, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        gvRegister.DataSource = ds;
        gvRegister.DataBind();

    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {


        string theConnectString = System.Configuration.ConfigurationManager.ConnectionStrings["EquipRegisterConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(theConnectString);
        string queryString = ("SELECT EngineerRef from EquipmentRegister  WHERE EngineerRef like'" + txtEngRef.Text + "%'");
        SqlCommand com = new SqlCommand(queryString,con);
        com.Connection = con;
        con.Open();
        SqlDataReader dr;
        dr = com.ExecuteReader();

        if (dr.HasRows)
        {
            dr.Read();
            rep_bind();
            gvRegister.Visible = true;

        }
        else
        {
            gvRegister.Visible = false;

        }
堆栈跟踪

    [SqlException (0x80131904): Incorrect syntax near 'EquipmentRegister'.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1789294
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5340642
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +244
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1691
   System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +61
   System.Data.SqlClient.SqlDataReader.get_MetaData() +90
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +377
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) +1421
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +177
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +137
   System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41
   System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +10
   System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +140
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +316
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) +88
   ViewRegister.rep_bind() in c:\Users\Michelle\Desktop\COMF510_65300_HS_task_2\ViewRegister.aspx.cs:53
   ViewRegister.btnSearch_Click(Object sender, EventArgs e) in c:\Users\Michelle\Desktop\COMF510_65300_HS_task_2\ViewRegister.aspx.cs:77
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628614
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

sqlDataAdapter是用connectionString而不是查询初始化的。这就是您得到sql异常的原因。
改用接收SqlCommand的构造函数


此外,还应使用参数化查询。

1。如果还没有,请了解SQL注入以及如何使用参数化查询来避免它。2.
txtEngRef.Text
中的值是多少?您是否尝试过使用断点并遍历(将鼠标悬停在变量上)以查看它是否正确加载
dr
?你有没有仔细检查过你的SQL,以确保该调用中应该有行?我在编写SQL时学到的一点是,为了便于阅读,在所有大写字母中都有它们的特殊单词(
SELECT FROM WHERE LIKE
etc)。我还首先在SQL中创建调用,然后将其带到VisualStudio(这只是我想要做的,其他人做的不同)。对我来说,我可以使用我期望的确切变量测试调用,因此我知道我的SQL是正确的。我对web开发也相当陌生,这些只是我在开始阶段做的一些事情,可能会对您有所帮助。我对这种编程语言很陌生,我将它改为queryString,它在另一个地方出现了相同的错误:“EquipmentRegister”附近的语法不正确。首先,这意味着这个问题已经解决,因为那一行没有错误。请编辑您的问题以包含新错误的堆栈跟踪。刚刚意识到我从查询中漏掉了“From”,这可能是抛出错误吗?好的,因此错误不再存在。谢谢你的指导。现在的问题是它点击了‘其他’:(为什么这不起作用!!谢谢你,我将为此标记答案:)