有些人可以解释一下我收到此错误的原因吗?我知道我的代码没有安全性,但这不适用于公共访问。请问你能解释我哪里错了吗?我想通过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
答案 0 :(得分:0)
使用connectionString而不是查询初始化sqlDataAdapter。这就是你得到sql异常的原因 使用收到SqlCommand的构造函数。
此外,您应该使用参数化查询。