在强类型 ADO.NET TableAdapter 中参数化架构

发布于 2024-08-13 04:01:36 字数 414 浏览 4 评论 0原文

我正在尝试在 ADO.Net 中构建强类型数据集,但在 TableAdapters 的一个方面遇到了一些问题。

我的查询看起来像

SELECT *
FROM testdict.ModuleVariable
WHERE Module = ?

我的问题围绕 testdict 部分。我们使用多种不同的模式来访问数据(因为存在多路复用的 Sybase IQ 实例)。如何参数化此查询的架构部分?

我已经尝试过:

SELECT *
FROM ?.ModuleVariable
WHERE Module = ?

但没有成功。我目前的想法是,我可能必须继承 TableAdapter 并手动参数化架构,但我希望有一个更好的解决方案!

提前致谢

I'm trying to build a strongly-typed dataset in ADO.Net and am having a little trouble with one aspect of the TableAdapters.

My query looks like

SELECT *
FROM testdict.ModuleVariable
WHERE Module = ?

My problem revolves around the testdict part. We use several different schemas to access our data (because of a multiplexed Sybase IQ instance). How can I parameterize the schema portion of this query?

I've tried:

SELECT *
FROM ?.ModuleVariable
WHERE Module = ?

but to no avail. My current mindset is that I may have to inherit the TableAdapter and parameterize the schema manually but I am hoping there is a nicer solution!

Thanks in advance

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

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

发布评论

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

评论(2

甜味超标? 2024-08-20 04:01:36

首先,您无法通过简单地添加选择查询在设计时实现此目的。您无法参数化架构。

但是,有一个解决方案。以下是您可以执行此操作的方法。

1.) 将表拖放到类型化数据集设计器中,这将为该表创建类型化数据表以及用于访问数据库表的表适配器。表适配器了解数据表的架构。

2.) 现在在数据库中创建一个存储过程,它采用两个参数。一个是表的架构 [?.ModuleVariable],另一个是您的where 子句 或者您可能想要作为条件的任何内容。您可以根据需要创建此重载。然后,该存储过程将根据参数构造 sql 查询并在数据库上执行。这将将结果集返回到调用表适配器

3.) 从设计视图中向表适配器添加一个方法,该方法将从存储过程中获取结果。 确保结果的架构完全符合关联数据表的架构

4.) 现在,您可以从代码中创建表适配器的实例并调用该方法,该方法又将调用您的存储过程并返回填充有结果的数据表

玩得开心!

First of all you won't be able to achieve this in design-time by simply adding a select query. You cannot parametrize the schema.

However there's a solution for this.Here's how you can do that.

1.) Drag and drop the table into your typed-dataset designer which will create a typed-data-table for the table and the table-adapter to access database table. table adapter is aware of the schema of the data-table.

2.) Now create a stored-procedure in your database that takes two arguments. one is the schema of the table [?.ModuleVariable] and other would be your where clause or maybe anytihng you may want as criteria. You can create overloads of this as you wish. This stored-procedure will then construct the sql query based on the arguments and execute it on the database. This will return the result-set to calling table-adapter.

3.) From the design-view Add a method to table-adapter which will fetch results from the stored-procedure. Make sure that the schema of results exactly meets the schema of the associated data-table.

4.) Now from the code you can create an instance of the table adapter and call the method which in turn will call your stored-procedure and return you with the data-table filled in with results.

Have fun!

葬花如无物 2024-08-20 04:01:36

您可以重载表适配器或向表适配器添加新函数,因为它们被定义为分部类。这个新函数将架构名称作为参数。例如,Fill(table As (tableName), schemaName As String)。我的做法如下:

  1. 创建一个新文件并将其命名为 (dataSetName).(whatever_you_like).vb。
  2. 在顶部放置命名空间(dataSetName)TableAdapters。
  3. 使用 Import 语句可以轻松访问数据集中的表。导入(解决方案名称)。(数据集名称)。
  4. 定义新函数。这个示例函数有点简化,但我想这足以让您明白这个想法。

部分类 (tableName)TableAdapter
公共重载函数 Fill( _
表 As (表名), _
schemaName 作为字符串)作为整数

Dim args() As String = {schemaName, table.TableName}
Dim selectCmdText As String = "SELECT * FROM {0}.{1}"
selectCmdText = String.Format(selectCmdText, args)

Connection.Open()
Dim selectCmd As New MySqlCommand(selectCmdText, Connection)    
Dim adapter As New MySqlDataAdapter(selectCmd)

Dim returnValue As Integer = 0
returnValue = adapter.Fill(table)

Connection.Close()

Return returnValue

结束函数
结束课程

亲切的问候,
卡洛斯·马伦

You can overload or add a new function to the table adapter, because they are defined as partial classes. This new function would have the schema name as a parameter. For example, Fill(table As (tableName), schemaName As String). Here's how I'd do it:

  1. Create a new file and name it (dataSetName).(whatever_you_like).vb.
  2. At the top put Namespace (dataSetName)TableAdapters.
  3. Use an Import statement to easily access the tables in the data set. Imports (solutionName).(dataSetName).
  4. Define the new function. This example function is a bit simplified, but I guess it will be enough for you to get the idea.

Partial Class (tableName)TableAdapter
Public Overloads Function Fill( _
table As (tableName), _
schemaName As String) As Integer

Dim args() As String = {schemaName, table.TableName}
Dim selectCmdText As String = "SELECT * FROM {0}.{1}"
selectCmdText = String.Format(selectCmdText, args)

Connection.Open()
Dim selectCmd As New MySqlCommand(selectCmdText, Connection)    
Dim adapter As New MySqlDataAdapter(selectCmd)

Dim returnValue As Integer = 0
returnValue = adapter.Fill(table)

Connection.Close()

Return returnValue

End Function
End Class

Kind regards,
Carlos Mallen

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