How to Code Database Connections
How to Code a Database Connection
| |
VB.NET supports a wide range of database connections. The basics of making a connection are importing the database connection classes, declaring a connection object, and setting the parameters needed to connect: The database type, the location of the database, and the name of the database. The provided code connects to an Access database. SQL server will basically be the same except that you use the import statment Imports.System.Data.SQLServer and your connection string will be different. Notice how we have a function call KillReader. Unlike VB 6.0 recordset, a VB.NET reader must be cleared before it can be reused. Use VB.NET 2005 because it has been modifed to function like VB 6.0 in respect to multiple reader objects per one connection object. Older versions of .NET only supported one reader object per connection object and that was a problem. We use HEADLINE Scripting with lowercase prefixes for most objects. We also use acronyms for field names (AccountNumber=ACTNBR). Please pardon the formatting from the HTML editor. Indentations were lost, and it selected some funky fonts and colors.
Copyrighted by CompuCranks.com (2006)
'****************************************************************************************
Option Explicit On
Option Strict On
Imports System.Data
Imports System.Data.OleDb
'****************************************************************************************
Public Class frmMASTER_MAINTENANCE
'****************************************************************************************
Private dbcCSC As OleDbConnection
Private rdrARM As OleDb.OleDbDataReader, hldACTNBR As Long
'****************************************************************************************
Private Sub frmMASTER_MAINTENANCE_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'****************************************************************************************
Me.Top = 50
Me.Left = 50
dbcCSC = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & GetSetting("COMPUCRANKS", "DBSTART", "PATH_NAME"))
dbcCSC.Open()
Call prcBUILD_ACCOUNT_LIST()
End Sub
'****************************************************************************************
Private Sub frmMASTER_MAINTENANCE_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
'****************************************************************************************
Select Case e.KeyCode
Case Keys.F3
End
Case Keys.F12
Me.Close()
End Select
End Sub
'****************************************************************************************
Private Sub frmMASTER_MAINTENANCE_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
'****************************************************************************************
KillReader(rdrARM)
dbcCSC.Close()
dbcCSC = Nothing
End Sub
'****************************************************************************************
Private Function KillReader(ByRef wrkREADER As OleDb.OleDbDataReader) As Boolean
'****************************************************************************************
Try
wrkREADER.Close()
wrkREADER = Nothing
KillReader = True
Catch
KillReader = False
End Try
End Function
|
|
|
|
|
|