Imports System.Data
Imports System.Data.SqlClient
Public NotInheritable Class SampleDataUtil
Private Sub New()
End Sub
'This method assumes that you have the Northwind sample database installed
'This method will return a flat DataSet with Customer information from the Northwind database.
Public Shared Function GetCustomers(ByVal serverAddress As String) As DataSet
Dim connectionString As String = String.Format("Data Source={0}; Initial Catalog=Northwind; Integrated Security=True", serverAddress)
Dim customerDataSet As New DataSet("Northwind")
'Create a SqlConnection.
Using conn As New SqlConnection(connectionString)
'Create the select Command that will retrieve all fields in the Customers table.
Dim customerSelectCommand As New SqlCommand("SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers", conn)
'Create a data adapter using the Command object created above.
Dim customerAdapter As New SqlDataAdapter(customerSelectCommand)
Try
'Fill the DataSet.
customerAdapter.Fill(customerDataSet, "Customers")
Catch e As Exception
'Silently fail if something goes wrong and write the exception message to the debug output window.
System.Diagnostics.Debug.WriteLine(e.Message)
End Try
End Using
Return customerDataSet
End Function
'This method assumes that you have the Northwind sample database installed.
'This method will return a hierarchical DataSet with Customer/Orders information from the Northwind database.
Public Shared Function GetCustomersOrders(ByVal serverAddress As String) As DataSet
Dim connectionString As String = String.Format("Data Source={0}; Initial Catalog=Northwind; Integrated Security=True", serverAddress)
'Initialize the DataSet with Customer data.
Dim customerOrderDataSet As DataSet = GetCustomers(serverAddress)
Using conn As New SqlConnection(connectionString)
Dim orderSelectCommand As New SqlCommand("SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate, ShippedDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry FROM Orders", conn)
Dim orderAdapter As New SqlDataAdapter(orderSelectCommand)
Try
orderAdapter.Fill(customerOrderDataSet, "Orders")
'After filling the Orders DataTable, add a DataRelation between the Customers DataTable and the Orders DataTable.
customerOrderDataSet.Relations.Add("Customer_Orders", customerOrderDataSet.Tables("Customers").Columns("CustomerID"), customerOrderDataSet.Tables("Orders").Columns("CustomerID"))
Catch e As Exception
System.Diagnostics.Debug.WriteLine(e.Message)
End Try
End Using
Return customerOrderDataSet
End Function
'This method assumes that you have the Northwind sample database installed
'This method will return the top 10 products that are in stock
Public Shared Function GetTop10ProductsInStock(ByVal serverAddress As String) As DataSet
Dim connectionString As String = String.Format("Data Source={0}; Initial Catalog=Northwind; Integrated Security=True", serverAddress)
Dim top10ProductsDataSet As New DataSet("Northwind")
Using conn As New SqlConnection(connectionString)
Dim productSelectCommand As New SqlCommand("SELECT TOP 10 UnitsInStock, ProductID FROM Products ORDER BY UnitsInStock DESC", conn)
Dim productAdapter As New SqlDataAdapter(productSelectCommand)
Try
productAdapter.Fill(top10ProductsDataSet, "Products")
Catch e As Exception
System.Diagnostics.Debug.WriteLine(e.Message)
End Try
End Using
Return top10ProductsDataSet
End Function
End Class