使用参数在 SqlDataSource SelectCommand 中配置表名称

发布于 2024-10-07 23:00:56 字数 435 浏览 0 评论 0原文

我有一个 ASP.NET 3.5 Web 表单,其中的 DropDownList 绑定到数据库中的公司名称表。我还有一个数据绑定 GridView,我想根据 DropDownList 中选择的公司名称使用数据库中的数据进行更新,以便 GridView 的 SqlDataSource 的 SelectCommand 为:

SELECT Registration, Telephone, Profile FROM {CompanyName}_VehicleData

其中 {CompanyName} 是 DropDownList 中选择的任何内容。我已使用命令和参数编辑器创建一个指向 DropDownList 的 SelectedValue 的 ControlParameter,但我不知道如何编写 SelectCommand 查询以将参数连接到“_VehicleData”。有什么想法。

I have an ASP.NET 3.5 web form with a DropDownList bound to a table of company names in a database. I also have a data bound GridView which I would like to update with data from the database depending on the company name selected in the DropDownList, so that the SelectCommand for the GridView's SqlDataSource is:

SELECT Registration, Telephone, Profile FROM {CompanyName}_VehicleData

Where {CompanyName} is whatever is selected in the DropDownList. I've used the Command and Parameter Editor to create a ControlParameter pointing to the SelectedValue of the DropDownList, but I don't know how to write the SelectCommand query to concatenate the parameter to '_VehicleData'. Any thoughts.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞旧 2024-10-14 23:00:56

如果您使用的是 sqldatasource,您可以在后面的代码中设置 select 命令。

<sqldatasource>.SelectCommand = "select registration, telephone, profile " & _
                                "from " & <dropdown>.selectedvalue & "_VehicleData"
<sqldatasource>.SelectType = SqlDataSourceCommandType.Text

If you are using a sqldatasource you could set the select command in the code behind.

<sqldatasource>.SelectCommand = "select registration, telephone, profile " & _
                                "from " & <dropdown>.selectedvalue & "_VehicleData"
<sqldatasource>.SelectType = SqlDataSourceCommandType.Text
柏拉图鍀咏恒 2024-10-14 23:00:56

@Colin:您说解决方案是编写一个存储过程,在将连接值 (DropDown.SelectedValue + "_VehicleData") 注入到 SQL 命令之前,使用 INFORMATION_SCHEMA.TABLES 视图评估连接值 (DropDown.SelectedValue + "_VehicleData") 是否映射到真实表。您能给存储过程的代码片段吗?

@Colin: You said that the solution is writing a stored procedure that evaluates whether the concatenated value (DropDown.SelectedValue + "_VehicleData") maps to a real table using the INFORMATION_SCHEMA.TABLES view before injecting it into a SQL command. Can you please give a code snippet for the stored proc?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文