使用SqlDataSource和参数时出错

时间:2012-04-14 04:17:25

标签: c# sql c#-4.0

尝试使用SqlDataSource和Gridview的SelectCommand时出现以下错误。我觉得我应该工作,但我得到这个错误;我怀疑我很接近但却忽视了一些事情:

System.Data.SqlClient.SqlException (0x80131904): Invalid object name '@tbl'. 
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) 
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) 
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() 
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData() 
at System.Data.SqlClient.SqlDataReader.get_MetaData() 
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) 
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) 
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) 
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) 
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) 
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) 
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) 
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) 
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) 
at System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) 
at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) 
at System.Web.UI.WebControls.DataBoundControl.PerformSelect() 
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind() 
at System.Web.UI.WebControls.GridView.DataBind() 
at Test.BindData() in C:\Dev\Test.aspx.cs:line 45

ASPX:

   <asp:DropDownList ID="ddlTable" AutoPostBack="true" runat="server"/>

   <asp:SqlDataSource ID="sqlDS" runat="server" SelectCommand="Select * From [@tbl]">
       <SelectParameters>
            <asp:ControlParameter ControlID="ddlTable" Name="tbl" PropertyName="SelectedItem.Text" />
       </SelectParameters>
   </asp:SqlDataSource>

   <asp:GridView
    ID="grd"
    CssClass="auto"
    runat="server" 
    DataSourceID="sqlDS"
    AutoGenerateDeleteButton="true"
    AutoGenerateEditButton="true" />

代码:

sqlDS.ConnectionString = "valid connection string";
grd.DataBind();

1 个答案:

答案 0 :(得分:0)

这不是有效的SQL,这是你的问题。我不确定你想要得到什么,但你不能传递一个参数,它将作为一个表。您的查询需要从数据库中进行选择,您可以使用该参数进行过滤或选择。你不能将它用作表本身。也许你正在寻找更像这样的东西?

Select ID From tablename where columnname = '@tbl'