Manuel said:
Whenever I want to use a Typed Data Set I find myself writing A LOT of
code which I think shouln't be necessary. For example, if I wanted to get
a DataTable of the Customers table I would have to write all this:
---------------
Dim myCnn As New Data.SqlClient.SqlConnection("TheConnectionString")
Dim myDA As New MyDBTableAdapters.CustomersTableAdapter
myDA.Connection = myCnn
Dim tblCustomers As New MyDB.CustomersDataTable
myDA.Fill(tblCustomers)
myCnn.Close()
'... finally work with the tblCustomers
I use the Enterprise Library for handling filling and updating typed
datasets. We modified so I don't have to create a command object and add
the parameters for each stored procedure parameter when I am calling an
Update or Insert sp.
Dim db As Database = DatabaseFactory.CreateDatabase()
Dim ds As Schema.Customers = New Schema.Customers()
db.LoadDataSet(CommandType.StoredProcedure, "MySpName", ds, New String() {
"tblCustomer" }
But, the code above is shortened from what I do..because I use Logging and
Caching as well. So, something more along the lines:
DAL Assembly Code:
<AutoComplete()> _
Public Function Fetch() As Schema.Customer.tblCustomerDataTable
' Check security here.
...
Dim db As Database = DatabaseFactory.CreateDatabase()
Dim ds As Schema.Customer = New Schema.Customer()
Try
' Fetch the dataset.
db.LoadDataSet( _
CommandType.StoredProcedure, _
"Customer_Fetch", _
ds, _
New String() { "tblCustomer" } _
)
' Return the datatable.
Return ds.tblCustomer
Catch ex As SqlException
Throw MyCustomDatabaseException(ex)
End Try
End Function
In the business logic layer, we have something like:
<Transaction(TransactionOption.Required)> _
Class Customer ' In MyNamespace.BLL namespace.
Inherits ServicedComponent
<AutoComplete()> _
Public Function Fetch() As Schema.Customer.tblCustomerDataTable
Dim customerDAL As DAL.Customer = New DAL.Customer()
Dim dt As Schema.Customer.tblCustomerDataTable
Try
' Send fetch to DAL.
dt = customerDAL.Fetch()
If dt.Count = 0
' Throw exception for no rows found, handle in UI.
End If
Return dt
Catch ex As MyCustomDatabaseException
' Any exception handling here.
Finally
' Cleanup.
customerDAL.Dispose()
End Try
End Function
....
End Class
The above code is similar to the Insert, Update, other Fetch's and Delete
methods.
HTH,
Mythran