How to connect encrypted Access databases in Visual Basic 6.0

zhaozj2021-02-16  50

I have seen how to introduce how to connect and use the Access database in Visual Basic, in fact, in the professional database software development, in order to ensure the security of information in the database, the database file is often required to prevent illegal users from passing. Other conventional means open it. So how do you build a connection with an encrypted database in Visual Basic? In developing the school's dormitory management information system, the author summarizes some methods and techniques, and is now written in communication with peers.

First, establish a database

Because there are database connections in Visual Basic 6.0, the database is not supported by the database, in order to facilitate the problem, the database mentioned in this article takes the Access 97 version database as an example.

A database is created in Microsoft Access 97, such as: ssgl.mdb, setting password, such as "1234", putting the project file created in the database file and VB in the same directory.

If only Access 2000 is available on the user's computer, you can create a SSGL.MDB database in Access 2000, and set your password, and use the Database Utility in Access 2000 to convert the database into an Access 97 version of the ACCESS 97 version.

Of course, you can also create a database directly in the Visual Basic 6.0 integrated development environment, and then set a password in Access 97.

By setting a password for the database file, in general, illegal users cannot open the database with conventional means, the information in the database has played a certain security and confidentiality.

Second, connect the encrypted Access database

In Visual Basic 6.0, you want to establish a connection with the database, a lot of technical means, such as data controls, data objects, data environment designers, and more. Developers can choose according to their own conditions and users' needs.

Limited to the space, the following only describes the difference between the encrypted Access database and the connectionless Access database in connection. The reader can see other information about the connection and access of the database without encrypted databases.

1, use controls

1 DATA control

The DATA control is a built-in data control in Visual Basic 6.0, which can be connected and accessed by setting the CONNECT, DatabaseName, and RecordSource properties of the DATA control.

There are two ways to connect the encrypted database through the DATA control:

One way is to change the default value of the CONNECT attribute of the Data control in the "Attribute Window" in the "Properties Window", and the setting method of other attributes, the setting method without the encrypted Access database. The connection is the same.

Another method is to be implemented by code to assign a value to the Connect property through the code.

Such as: data1.connect = "; PWD = 1234"

Data1.DatabaseName = app.path "/ssgl.mdb"

Among them, "1234" is the password of the Access database file SSGL.MDB.

2ADODC control

ADODC control is an ActiveX control that creates a connection to the database using Microsoft ActiveX Data Objects (ADO). Before using the ADODC control, first add the ADODC control to the control toolbox. The method is as follows: In VB 6.0 Select the "Project" menu, click the "Parts" menu item to select the "Microsoft Ado Data Control 6.0 (OLEDB)" option in the pop-up "Part" dialog box.

There are two ways to connect to encrypted databases through the ADODC control:

One way is to set a valid connection string in the "Properties Window" in the "Properties Window" in the design state, and add "after the connection string"; Jet OLEDB: Database Password = 1234 ", then Set the CommandType of the AdoDC control, the property of the RecordSource can be created to the encrypted database. Another approach is to create a connection when running, dynamically through the code, dynamically set the connection, commandty, and recordsource properties to create a connection. Just add "to" Jet OLEDB: Database Password = 1234 "as long as the valid connection string of the Connectionstring property;

2, use data objects

1 DAO data object

To correctly reference the DAO data object to establish a connection to the database, first select the "Project" menu in the VB integrated development environment, then click the "Reference" menu item, select "Microsoft Dao 3.51 Object" in the "Reference" dialog box Library "options to add DAO data object type libraries.

Next, you can use the following code to create a connection to the encrypted Access database SSGL.mdb.

DIM DB AS DATABASE

SET DB = OpenDatabase (app.path "/ssgl.mdb", false, false, "; pwd = 1234")

2 ADO data object

ADO is the latest technology in the processing relational database and non-relational databases launched by Microsoft, as well as technical connection and access to data connections and access by Microsoft. In VB 6.0, both ADODC controls, ADO data objects, and DataEnvironment (Data Environment Designers) are ado technology, so they are similar to how they process encrypted Access databases.

To correctly reference the ADO data object, you should select the "Project" menu in the VB 6.0 integrated development environment, then click the "Reference" menu item, select the "Microsoft ActiveX Data Objects 2.1 Library" option in the "Reference" dialog box pop-up. Add ADO data object type libraries.

The following code can be used to establish the connection to the encrypted Access database SSGL.mdb.

DIM CNN As Adodb.Connection

DIM RST As Adodb.Recordset

Set cnn = new adodb.connection

CNN.Provider = "Microsoft.jet.OleDb.3.51"

CNN.Connectionstring = "data source =" & app.path & "/ssgl.mdb;" & _

"JET OLEDB: Database Password = 1234"

CNN.Open

3 use DataEnvironment (data environment designer)

There are two ways to connect to the encrypted Access database via DataENVIRONMENT:

One way is to add a string after the effective connection string of the Connectionsource attribute of the DataEnvironment's Connection object of DataENVIRONMENT, "Jet OLEDB: Database Password = 1234".

Another way is to write as follows in the DataEnvironment_Initialize () event:

Private sub DataENVIRONMENT_INITIALIZE ()

DIM STRCONN AS STRING

StrConn = "provider = microsoft.jet.Oledb.3.51;" & _

"Data Source =" & app.path & "/ssgl.mdb;" & _ "; Jet OLEDB: Database Password = 1234"

DataENVIRONMENT1.CONNECTION1.CONNECTIONSTRING = STRCONN

End Sub

The above methods and related codes have been debugged, validated and passed in the Windows 98 operating system environment, Visual Basic 6.0.

转载请注明原文地址:https://www.9cbs.com/read-23133.html

New Post(0)