odbc| Data | Data source several methods for registering ODBC data sources
National Defense Hkust Dinghao
ODBC (Open DB connectivity, open Database interconnection) is an application interface (API) specification. It defines a standard set of routines that use their applications to access data from a data source. Applications can use ODBC directly by referencing the API's functions, or by using Data Access Objects (DAO) or Remote Data Objects (RDO). However, when implementing ODBC, we must first configure the ODBC environment to register the data source in order to connect, access, and manipulate the data source when programming the database. This article describes several common ways to register an ODBC data source.
Manual configuration
1. ODBC Data Source Manager
In the development of the database, in order to achieve the configuration of ODBC, DSN definition registration purposes, Microsoft gave a manual solution. In the control panel of the Windows 9X operating system, there is an icon named ODBC data source (32 bits) that enables you to activate a program that specifically sets an ODBC environment for users (ODBC data source ADMINISTRATOR,ODBC). In the Windows 2000 operating system, the above icon is placed in the management tool in Control Panel.
This program, which is used to set up an ODBC environment, is called a desktop driver, and it supports several types of DBMS (database Management system, DBMS). When a user wants to add a data source and a required driver, you can configure a specific type of database through the ODBC Data Source Manager's configuration dialog box. In most cases, when writing a program that operates on a database, we need to at least know information such as database file name, System (local or remote), folder, and so on, and name the data source.
2. Defining the type of data source
Users can define the following three types of data sources:
User data Source: Created as a user data source located locally on the computer, and can only be used by the user who created the data source;
System Data Source: Created as a system data source belonging to a computer or system rather than a specific user, and the user must have access to use it;
File data Source: Specifies to be defined as a file data source in a file, which can be used by anyone who has installed the driver correctly.
3. Steps for registering data sources
For example, in Microsoft Access, if you create a database file named Myexample.mdb in the C:\myfile\ folder, the registration steps are as follows:
Locate the ODBC Data source icon in the Control Panel to open the interactive interface of the data Source manager;
The database file created by the author is used by the users on this computer, so select "User DSN" here;
Then press the "Add" button and in the pop-up Data Source Manager dialog box, select a driver for the data source you want to create, and the database file for this article is created with Microsoft Access, so select Microsoft Access Driver (*.mdb) ;
Press the Finish button to enter an interface titled "ODBC Microsoft Access Installation", where you set the data source name to "myDataSource", select the database File "C:\myfile\myexample.mdb", and then determine.
This completes the registration of a simple ODBC data source. Of course, in the above steps, users can set different options according to their own needs.
Once the registration is complete, we can manipulate the database in the programming of the database.
Registering a data source in VB
When programming a database in VB, you can use the Registerdatasource method to enter the connection information for the ODBC data source into the Windows registry. Its syntax is:
Registerdatasource DSN, driver, silent, attributes
Where the parameters represent the meaning of:
DSN: A string expression that is the name to use when referencing a block of data source description information. For example, if the data source is an ODBC remote database, this is the name of the server and, if it is a user DSN, the name of the registered data source.
Drive: A string expression that represents the name of the ODBC driver. It is not an ODBC driver Dynamic Connection Library (DLL) file name. For example, SQL Server is the driver name and SQLSRVR. DLL is the DLL file name. ODBC and the appropriate driver must be installed.
Silent: Boolean Type value True if you do not want to display the ODBC Driver dialog box to prompt for information about the driver, or False if you want to display the ODBC Driver dialog box. If silent is True, then attributes must contain all required information for the specified driver.
Attributes: string expression, which is the one to add to ODBC. INI file in the list of keywords. When writing a program, you can select several attributes parameters to set as needed.
For example: We can write the following VB program to achieve the same purpose as the manual registration above. The list of procedures is as follows:
Sub Registerdatasource ()
Dim Strattribs as String
' Build the keyword string.
' C:\myfile\myexample.l.mdb database file name (include path)
Strattribs = "Dbq=" _
& "C:\myfile\myexample.mdb" _
& chr$ () & "Oemtoansi=no" _
& chr$ () & "Server=sequel" _
& chr$ () & "Network=dbnmpntw" _
& chr$ () & "Database=workdb" _
& chr$ () & "Address=\ equel\pipe ql\query"
' Create a new registered DSN.
Rdoengine.rdoregisterdatasource "myDataSource",
"Microsoft Access Driver (*.mdb)", True,
Strattribs
End Sub
Private Sub Form_Load ()
Call Rdoregisterdatasource
End Sub
Note: In order to use Rdoengine and remote Data Objects in your code, you must first set a reference to the Microsoft Remote 2.0 object Library in the References dialog box, or you will cause a compilation error The first time you reference the Rdoengine object.
Merge with Registry
1. Solving ideas
After examining the changes to the Windows registry after an ODBC data source has been registered, we can find that the effect of the registered data source on the Windows registry is critical in [HKEY_CURRENT_USER Oftware\odbc\odbc. INI].
In this way, we export [HKEY_CURRENT_USER OFTWARE\ODBC\ODBC. INI], each time as a template, to register the data source information into the registry export file corresponding to the location, and then merge the file with the system registry.
2. Specific operations
First, we need to understand the meaning of regedit and its parameters. Regedit is the command to open the system registry. Enter in Start \ Run \. Its three parameters mean the following:
regedit/e Myfile.reg: Indicates that the registry file is exported to the Myfile.reg file, which is equivalent to copying the entire registry file into Myfile.reg;
REGEDIT/C Myfile.reg: means to import Myfile.reg into the registry, which is equivalent to covering the contents of the registry with the contents of the file;
REGEDIT/S Myfile.reg: Indicates that the contents of the Myfile.reg file are merged with the contents of the registry file.
We only need to export the useful parts in the registry and then add the information of the data source to be registered, then merge with the system registry. This merging process, we can either manually input to implement, can also be programmed to implement automatic merging.
Note: To prevent misoperation, be sure to make a backup of your system registry.
Summary
From the point of view of security, the author takes advantage of the ODBC Data source management tool in the Windows Control Panel to register the data source so as not to cause unnecessary loss due to the ignorance of the registry or negligence. Of course, there are utilities that can register ODBC data sources, and users can choose to use them for different needs.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.