Question

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

Was it helpful?

Solution

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!

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top