Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' WindowsDB Solution:
- 'App.Config
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <configSections>
- </configSections>
- <startup>
- <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
- </startup>
- <connectionStrings>
- <add name="ContactDB" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\BOLENTABLET\DataFiles\ContactDB.accdb" providerName="System.Data.OleDb"/>
- </connectionStrings>
- </configuration>
- 'ContactForm.vb
- Imports WindowsDAL
- Public Class ContactForm
- Private Sub ContactForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- SetDataGrid(ContactDataGridView)
- Dim bs As New BindingSource()
- bs.DataSource = AccessStore.GetAll()
- ContactDataGridView.DataSource = bs
- End Sub
- Private Sub SetDataGrid(contactDataGridView As DataGridView)
- With contactDataGridView
- .Dock = DockStyle.Fill
- .AllowUserToAddRows = False
- .AllowUserToDeleteRows = False
- .AllowUserToOrderColumns = True
- .AlternatingRowsDefaultCellStyle.BackColor = Color.WhiteSmoke
- .Anchor = (AnchorStyles.Left Or AnchorStyles.Top Or
- AnchorStyles.Right Or AnchorStyles.Bottom)
- .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
- .BorderStyle = BorderStyle.Fixed3D
- .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
- .ReadOnly = True
- ' DataGridView Column Headers Bold – must be set in run mode.
- .ColumnHeadersDefaultCellStyle.Font =
- New Font(.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold)
- End With
- End Sub
- End Class
- 'WindowsDAL.DataStore
- Imports System.Data.OleDb
- Public Class DataStore
- Public Shared ReadOnly Property ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ContactDB").ConnectionString
- End Class
- 'WindowsDAL.Contacts.vb
- Public Class Contact
- Public Property Id As Long
- Public Property FirstName As String
- Public Property LastName As String
- Public Property Address As String
- Public Property City As String
- Public Property StateCode As String
- Public Property ZipCode As String
- End Class
- 'WindowsDAL.AccessStore.vb
- Imports System.Data.OleDb
- Public Class AccessStore
- Public Shared Function GetAll() As List(Of Contact)
- Dim contacts As New List(Of Contact)
- Dim query As String = "SELECT Id, FirstName, LastName, Address, City, StateCode, ZipCode " _
- & "FROM Contact"
- Using connection As New OleDbConnection(DataStore.ConnectionString)
- Using cmd As New OleDbCommand(query, connection)
- connection.Open()
- Using reader As OleDbDataReader = cmd.ExecuteReader
- Do While reader.Read
- Dim newContact As New Contact With {
- .Id = Convert.ToInt64(reader("Id")),
- .FirstName = reader("FirstName"),
- .LastName = reader("LastName"),
- .Address = reader("Address"),
- .City = reader("City"),
- .StateCode = reader("StateCode"),
- .ZipCode = reader("ZipCode")}
- contacts.Add(newContact)
- Loop
- End Using
- End Using
- End Using
- Return contacts
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement