|
|||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis article discusses how to extend ADO.NET TableAdapter review
This will invoke the Data Source Configuration Wizard and, after answering a few short questions such as which database, which objects, etc., Visual Studio will add not only a new Data Source, but also a new DataSet with one or more TableAdapter objects inside. You can double-click the XSD file in Solution Explorer to get a "picture" of these objects. Here's the one used in this article's example: Figure 1
As you can see, we are using the good old NorthWind database. For the record, Orders is the ADO.NET DataTable and of course the Another way to create a Figure 2
At this point, you could build a form and put that TableAdapter to good use. Just drag and drop the table or columns from the Data Sources window onto a form's design view. As you drop the table onto your form, this is what you might see appear in the form's component tray:
Left to right, these components are: the strongly typed
Each query will have its own generated SELECT * FROM dbo.EMPLOYEES WHERE LASTNAME > @LNAME1 AND LASTNAME < @LNAME2
Your call to the TableAdapter's Fill method might look like this: Me.EmployeesTableAdapter.Fill(Me.HRDBDataSet.Employees,_
"AAAAA","HHHHH")
This technique works well as long as your query in the The generated Looking again at the DataSet in DataSet Designer, we see that even a very simple Figure 3
Each
All of these objects are more-or-less built-in and private. However, these generated objects and methods can be used to fetch data -- even update data -- from the database. At this point, you may well ask yourself: OK. Where have they hidden the code? To dig into the bowels of
How to get TableAdapter to process dynamic SQL SELECT QueriesThe problem with what we've seen of Public Overridable Overloads Function Fill(ByVal dataTable As _
NorthWindDBDataSet.OrdersDataTable) As Integer
Me.Adapter.SelectCommand = Me.CommandCollection(0)
If (Me.ClearBeforeFill = True) Then
dataTable.Clear()
End If
Dim returnValue As Integer = Me.Adapter.Fill(dataTable)
Return returnValue
End Function
Clearly, if we want to change DataAdapter's select command on the fly, we have to gain access to A "partial class" means that you can add your own code to the class in a separate file and just "extend" its functionality. A partial class lets you split the definition of a class -- its properties, methods, etc. -- over two or more source files. Thus, you can add your own class file and put your hack in this file. Step-by-step exampleAs mentioned before, our examples are all using the Orders Table from the NorthWind database. So begin by adding a simple Now make room at the top of the form by moving the grid down a bit. At the top of the form add a couple of text boxes, one labeled Customer ID Like: and the other labeled Ship Country Like:. Give the text boxes appropriate names such as Figure 4
Preparing to hackNow we can add our hack. From Solution Explorer, begin by opening up the file NorthWindDBDataSet.Designer.vb. In the code, find the namespace statement for the TableAdapter. This might require a few clicks of the Find Next button. The namespace naming convention is simply: DataSetName + Namespace NorthWindDBDataSetTableAdapter
Copy and paste this namespace statement or, if you're old-fashioned like me, copy the name down on a Post-it sticky. Next, find the declaration for the TableAdapter class. It will be named using the naming convention: Object + Now, add a new Class file to your Project! In the class file, add a namespace statement with a name that is identical to that used in the auto-generated DataSet. Next, change the class declaration so it exactly matches that of the Option Strict Off
Option Explicit On
Imports System
Namespace NorthwindDBDataSetTableAdapters
Partial Public Class OrdersTableAdapter
Inherits System.ComponentModel.Component
End Class
End Namespace
Now we can hack the code in the class adding to our own file. Let's add a new property called Public Property SelectCommand() As SqlClient.SqlCommand()
Get
If (Me._commandCollection Is Nothing) Then
Me.InitCommandCollection()
End If
Return Me._commandCollection
End Get
Set(ByVal value As SqlClient.SqlCommand())
Me._commandCollection = value
End Set
End Property
The Set property procedure here means that code calling TableAdapter can pass in a properly formed Public Function FillByWhere(ByVal dataTable As _
NorthWindDBDataSet.OrdersDataTable, ByVal WhereExp As String) _
As Integer
Dim stSelect As String
stSelect = Me._commandCollection(0).CommandText
Try
Me._commandCollection(0).CommandText += " WHERE " + WhereExp
Return Me.Fill(dataTable)
Catch ex As Exception
Finally
Me._commandCollection(0).CommandText = stSelect
End Try
End Function
To complete the picture, simply write the necessary code behind the form's Fill button. See the code snippet sample below. You can start by moving the auto-generated code that calls the
See the sample button Private Sub btnFill_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnFill.Click
Dim stCustID As String = Trim(uxCustID.Text)
Dim stShipCntry As String = Trim(uxShipCntry.Text)
' Pass in a SELECT with no WHERE that conforms to the the DataTAble
' used by the TableAdapter's DataSet
Me.OrdersTableAdapter.SelectCommand(0).CommandText = _
"SELECT * FROM Orders"
' Build a string containing WHERE criteria (without the WHERE)
Dim stWhere As String = ""
If stCustID <> "" Then
stWhere = "CustomerID LIKE '" + stCustID + "%' AND "
End If
If stShipCntry <> "" Then
stWhere = "ShipCountry LIKE '" + stShipCntry + "%' "
Else
stWhere = Replace(stWhere, " AND ", "")
End If
If stWhere = "" Then
Me.OrdersTableAdapter.Fill(Me.NorthwindDBDataSet.Orders)
Else
Me.OrdersTableAdapter.FillByWhere( _
Me.NorthwindDBDataSet.Orders, stWhere)
End If
End Sub
That's about it, Ladies and Gentlemen! History
|
||||||||||||||||||||||||||||||