Question Database connection close

Raz

Member
Joined
Jun 7, 2010
Messages
11
Programming Experience
Beginner
Hi

I am developing a small application that is required to encrypt the database (MS Access) before existing.

I have a class called Class1 in the project folder. This class holds the information about my database connection and it is connected to my form.

My Problem: When I access the data once, then when I click the "Encrypt Database and exist" button to encrypt the database and exist the application, the system generate error message because connection to database is still open (.ldb format of database file appear in folder). Because I did not know how to explain my situation I have created a project with 2 forms and class file to demonstrate my situation.

The project is attached and I use VB.net 2005

I will be glad if you solve the connection closure problem in attached project.

Many Thanks
 

Attachments

  • DatabaseConnection.zip
    57.2 KB · Views: 21
You could start an app that waits for the LDB to disappear (file system watcher class) and then encrypts the DB. Your app can decrypt on startup, before use, unless LDB is there already
 
Database Connection

I have a class attached to each of my form. I call this class but unfortunately it does not close the connection once it open, any changes you can do form me please:

Imports System.Data.OleDb
Public Class Class1
Private dbComm As OleDbCommand
Private dbCon As OleDbConnection
Private Const strconn As String = _
"Provider=Microsoft.JET.OLEDB.4.0;data source=C:\Program Files\System\Databases\dbData.mdb"

Public Sub New()
dbCon = New OleDbConnection(strconn)
End Sub

Public Function ConList(ByVal str) As OleDbDataAdapter
' Close the connection if open
If dbCon.State = ConnectionState.Open Then
dbCon.Close()
End If
'Open connection to database
dbCon.Open()
Dim da As OleDbDataAdapter
'populate data adapter
da = New OleDbDataAdapter(str, dbCon)
Return da
' Close connection
dbCon.Close()
' empty data adapter
da.Dispose()
End Function

Public Function ConAdd(ByVal str As String) As Boolean
' Close the connection if open
If dbCon.State = ConnectionState.Open Then
dbCon.Close()
End If
'Open connection to database
dbCon.Open()
' command text
dbComm = New OleDbCommand(str, dbCon)
If dbComm.ExecuteNonQuery > 0 Then
ConAdd = True
Else
ConAdd = False
End If
' Close connection
dbCon.Close()
End Function
End Class


Many Thanks
 
You could do yourself a lot of favours by reading the DW2 link in my signature, section "Creating a SImple Data APp"

It will teach you how to do data access in a modern, sensible way so that you can spend more time programming important things and less time writing (bad quality!) SQL statements and database access code
 
Back
Top