Imports
System.Data.OleDb
Namespace
Classes
Public
Class
DataOperations
Private
ConnectionString
As
String
=
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=NorthWind.accdb"
LastException
Exception
Function
LoadCustomerRecordsUsingDataTable()
DataTable
Dim
selectStatement =
"SELECT Cust.CustomerIdentifier, CT.ContactTypeIdentifier, Cust.CompanyName, "
&
"Cust.ContactName, CT.ContactTitle, Cust.Address AS Street, Cust.City, "
"Cust.PostalCode, Cust.Country, Cust.Phone, Cust.ModifiedDate "
"FROM Customers AS Cust INNER JOIN ContactType AS CT ON "
"Cust.ContactTypeIdentifier = CT.ContactTypeIdentifier;"
customerDataTable =
New
Using cn
OleDbConnection
With
{.ConnectionString = ConnectionString}
Using cmd
OleDbCommand
{.Connection = cn}
Try
cmd.CommandText = selectStatement
cn.Open()
customerDataTable.Load(cmd.ExecuteReader())
customerDataTable.Columns(
"CustomerIdentifier"
).ColumnMapping = MappingType.Hidden
"ContactTypeIdentifier"
"ModifiedDate"
Catch
ex
LastException = ex
End
Using
Return
customerDataTable
System.Data.SqlClient
"Data Source=KARENS-PC;"
"Initial Catalog=NorthWindAzure;Integrated Security=True"
SqlConnection
SqlCommand
MainForm
Sub
MainForm_Shown(sender
Object
, e
EventArgs)
Handles
Me
.Shown
accessOperations
DataOperationsAccess
customersFromAccessDataTable
DataTable =
accessOperations.LoadCustomerRecordsUsingDataTable()
If
accessOperations.LastException
Is
Nothing
Then
' If no run time exceptions populate DataGridView with the DataTable
customersAccessDataGridView.DataSource = customersFromAccessDataTable
' Expand all columns so all data is visible
customersAccessDataGridView.ExpandColumns
' Split column headers where a field name is PostalCode make it Postal Code.
customersAccessDataGridView.Columns.
Cast(Of DataGridViewColumn).
ToList().
ForEach(
(col)
col.HeaderText = col.HeaderText.SplitCamelCase()
)
Else
MessageBox.Show(accessOperations.LastException.Message)
sqlServerOperations
DataOperationsSqlServer
customersFromSqlServerDataTable
sqlServerOperations.LoadCustomerRecordsUsingDataTable()
sqlServerOperations.LastException
customersSqlServerDataGridView.DataSource = customersFromSqlServerDataTable
customersSqlServerDataGridView.ExpandColumns
customersSqlServerDataGridView.Columns.
closeApplicationButton_Click(sender
closeApplicationButton.Click
Close()
''' <summary>
''' Return a DataTable of customers by country
''' </summary>
''' <param name="pCountry">Valid Country name</param>
''' <returns>Customers from pCounty</returns>
SimpleReadExample(pCountry
<SQL>
SELECT
Customers.CustomerIdentifier,
Customers.CompanyName,
Customers.ContactName,
Customers.Country
FROM Customers
WHERE (((Customers.Country)=@Country));
</SQL>.Value
cmd.Parameters.AddWithValue(
"@Country"
, pCountry)
[CustomerIdentifier]
,[CompanyName]
,[ContactName]
,[ContactId]
,[Address]
,[City]
,[Region]
,[PostalCode]
,[CountryIdentifier]
,[Phone]
,[Fax]
,[ContactTypeIdentifier]
,[ModifiedDate]
FROM
[NorthWindAzureForInserts].[dbo].[Customers]
Cust.CustomerIdentifier ,
Cust.CompanyName ,
Cust.ContactName ,
Cust.ContactId ,
Cust.Address ,
Cust.City ,
Cust.Region ,
Cust.PostalCode ,
Cust.CountryIdentifier ,
Cust.Phone ,
Cust.Fax ,
Cust.ContactTypeIdentifier ,
Cust.ModifiedDate ,
C.
Name
Customers
AS
Cust
INNER
JOIN
Countries
C
ON
Cust.CountryIdentifier = C.CountryIdentifier;
Cust.[Address]
Street ,
Cust.CountryIdentifier = C.CountryIdentifier
WHERE
( Cust.CountryIdentifier = @CountryIdentifier );
DECLARE
@CountryIdentifier
INT
= 4;
When working with parameters for queries e.g. SELECT/WHERE, UPDATE, DELETE with MS-Access parameters are ordinal based meaning they are added to a command object in the same order as in the SQL statement and since this is the case generally speaking developers will use a question mark to name a parameter as the name does not matter since parameters are ordinal in nature. Example of a MS-Access SELECT with two parameters.
C.CustomerIdentifier,
C.CompanyName,
C.ContactName,
C.ContactTypeIdentifier,
CT.ContactTitle,
C.Country
ContactType
CT
C.ContactTypeIdentifier = CT.ContactTypeIdentifier
C.ContactTypeIdentifier = ?
AND
C.Country = ?;
C.CustomerIdentifier ,
C.CompanyName ,
C.ContactName ,
C.ContactTypeIdentifier ,
CT.ContactTitle ,
( C.ContactTypeIdentifier = @ContactTypeIdentifier )
( C.Country = @Country );
C.ContactTypeIdentifier = @ContactTypeIdentifier
C.Country = @Country;
GetDataTableListLocal()
List(Of DataTable)
dataTables =
List(Of DataTable)()
totalRecords = 0
tableIndex = 1
Using cn =
Using cmd =
'
' Used to chunk data in quarters of total records
"SELECT Cust.CustomerIdentifier,Cust.CompanyName,Cust.ContactName,C.[Name] AS Country "
"FROM dbo.Customers AS Cust "
"INNER JOIN dbo.Countries AS C ON Cust.CountryIdentifier = C.CountryIdentifier "
"ORDER BY Cust.CustomerIdentifier "
"OFFSET @Offset ROWS FETCH NEXT 25 ROWS ONLY;"
countStatement =
"SELECT COUNT(Cust.CustomerIdentifier) FROM dbo.Customers AS Cust"
cmd.CommandText = countStatement
' Get total records in table
totalRecords = Convert.ToInt32(cmd.ExecuteScalar())
cmd.Parameters.Add(
"@OffSet"
, SqlDbType.Int)
For
rowIndex = 0
To
totalRecords - 1
rowIndex
Mod
25 <> 0
Continue
cmd.Parameters(
).Value = rowIndex
dt =
DataTable()
{.TableName = $
"Table_{tableIndex}"
}
dt.Load(cmd.ExecuteReader())
dataTables.Add(dt)
tableIndex += 1
Next
dataTables
@OffSet
CREATE
PROCEDURE
dbo.FetchCustomers(@Offset
BEGIN
Cust.CustomerIdentifier,
Cust.CompanyName,
Cust.ContactName,
Country
dbo.Customers
dbo.Countries
ORDER
BY
Cust.CustomerIdentifier
OFFSET @Offset
ROWS
FETCH
NEXT
25
ONLY
END
GetDataTableListStoredProcedure()
"FetchCustomers"
cmd.CommandType = CommandType.StoredProcedure
ReferenceItem
Property
Id()
Integer
Name()
Overrides
ToString()
RetrieveMultipleResults()
_
(employeelist
List(Of ReferenceItem),
categoryList
List(Of ReferenceItem))
employeeList
List(Of ReferenceItem)
isCategory =
True
cmd.CommandText =
"SELECT CategoryID, CategoryName FROM dbo.Categories;"
"SELECT EmployeeID, FirstName + ' ' + LastName As FullName FROM dbo.Employees"
reader = cmd.ExecuteReader()
Do
While
reader.HasRows
reader.Read()
isCategory
categoryList.Add(
ReferenceItem()
{.Id = reader.GetInt32(0), .Name = reader.GetString(1)})
employeeList.Add(
Loop
reader.NextResult()
False
(employeeList, categoryList)
Form1
Button1_Click(sender
Button1.Click
ops
dualResult = ops.RetrieveMultipleResults()
CategoryComboBox.DataSource = dualResult.categoryList
EmployeeComboBox.DataSource = dualResult.employeelist
Contacts.FirstName ,
Contacts.LastName ,
Countries.
CountryName ,
Cust.ModifiedDate
dbo.ContactType
Cust.ContactTypeIdentifier = CT.ContactTypeIdentifier
dbo.Contacts
Cust.ContactId = Contacts.ContactId
Cust.CountryIdentifier = Countries.CountryIdentifier
Cust.CountryIdentifier
CustomerEntity
CustomerIdentifier
Company
ContactIdentifier
?
FirstName
LastName
ContactTypeIdentifier
ContactTitle
Street
City
PostalCode
CountryIdentifier
CountyName
$
"{CustomerIdentifier} -- {Company}"
EntityFrameworkCodeFirstWithExistingDatabase
''' Read data using Entity Framework which does the same as the SQL SELECT
''' in the project BasicUpdatingDataProvider with less code overall and
''' more functionality.
ReadCustomers()
List(Of CustomerEntity)
customerData
Using context
NorthWindEntity
customerData =
(From customer
In
context.Customers
Join contactType
context.ContactTypes
On
customer.ContactTypeIdentifier _
Equals contactType.ContactTypeIdentifier
Join contact
context.Contacts
customer.ContactId Equals contact.ContactId
Select
{
.CustomerIdentifier = customer.CustomerIdentifier,
.Company = customer.CompanyName,
.ContactIdentifier = customer.ContactId,
.FirstName = contact.FirstName,
.LastName = contact.LastName,
.ContactTypeIdentifier = contactType.ContactTypeIdentifier,
.ContactTitle = contactType.ContactTitle,
.Street = customer.Address,
.City = customer.City,
.PostalCode = customer.PostalCode,
.CountryIdentifier = customer.CountryIdentifier,
.CountyName = customer.Country.Name
}).ToList()
DataGridView1.DataSource = ops.ReadCustomers()
customerView
BindingListView(Of CustomerEntity)
customerView =
BindingListView(Of CustomerEntity)(ops.ReadCustomers())
DataGridView1.DataSource = customerView
TOP
(10) PERCENT
C.[
]
CHECKSUM(NEWID());
DELETE
OUTPUT
deleted.ID
(CountryIdentifier = 2)
INSERT
INTO
Customers(CompanyName, ContactName)
inserted.ID
VALUES
(
'ABC Inc'
,
'Miss Joan'
SELECT COLUMN_NAME ,
CHARACTER_MAXIMUM_LENGTH ,
ORDINAL_POSITION ,
IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME =
'Customers'
AND DATA_TYPE =
'nvarchar';