Visual C + + implements automatic configuration of ODBC data sources (RPM)

Source: Internet
Author: User
Tags driver manager dsn odbc

To enable ODBC to work with the database, you must register the database with the ODBC Driver Manager, which can be done by defining a DSN or data source name. In general, we can only manually open the System control Panel, run the ODBC data Source Manager, manually configure the data source, but this work is too complex for users, we must consider using the program for users to complete these configuration work.

1. SQLConfigDataSource function Description

The ODBC API provides a function Sqlconfig DataSource that dynamically creates a data source. The function is prototyped as follows:

BOOL SQLConfigDataSource (HWND hwndparent,word frequest,
LPCSTR lpszdriver,lpcstr lpszattributes);
The parameters are described as follows:

(1) The parameter hwndparent is used to specify the parent window handle, which can be specified as null when you do not need to create a Data Source dialog box.

(2) The parameter frequest is used to specify the operation content of the function, the value is as follows:

ODBC_ADD_DSN: Add a new user data source;

ODBC_CONFIG_DSN: Modify an existing user data source;

ODBC_REMOVE_DSN: Delete an existing user data source;

ODBC_ADD_SYS_DSN: Add a new system data source;

ODBC_CONFIG_SYS_DSN: Configure or modify an existing system data source;

ODBC_REMOVE_SYS_DSN: Delete an existing system data source;

ODBC_REMOVE_DEFAULT_DSN: Deletes the missing data source Description section.

(3) Parameter lpszdriver for specifying the driver for an ODBC data source

Program categories, for example, in order to specify an Access data source, the parameter should be assigned the string "Microsoft Access Driver (*.mdb)"; To the SQL Server data source, the string "SQL Server" should be assigned.

(4) The parameter lpszattributes is used to specify ODBC data source properties. For example:

① to an Access data source:

"Dsn= myimage\0 dbq=d:\\imageprocess\\image.mdb\0
Defaultdir= d:\\imageprocess\0\0 "

Description: The string specifies the data source name (DNS) is myimage, the database file (DBQ) is D:\\imageprocess\\image.mdb, and the default database file path (DefaultDir) is d:\\imageprocess.

② to the SQL Server data source:

"Dsn=myimage\0 server=myet\0 database=image"

Description: This string specifies that the data source name (DSN) is the Myimage;sqlserver database server name (server) is Myet, and the database name is image.

2. Two issues to be aware of

(1) When we use the SQLConfigDataSource ODBC API function, we must declare the Odbcinst.h header file containing the system, so we select the workspace window FileView Open the header Files in Imageprocess.h, add # include "Odbcinst.h" in them.

(2) SQLConfigDataSource This API function must use the Odbccp32.dll, it is Microsoft provides 32-bit ODBC installs and manages the DLL, if is 16 Bit must be used Odbcinst.dll,odbccp32.dll there is an import library, so the solution is to add this odbccp32.lib to our project, we can open
Project system menu item, select the Add to Project submenu, in which the files entry is selected, open the \vc\lib\ directory under the VC installation directory, file type selected library files (. lib), choose the odbccp32.lib and press OK.

3. Program code

From the above see Set parameter lpszattributes need to set the path of the database file, in order to facilitate the user's use, you can save the database file to the project's debug folder, through the program to automatically get the database file path function, the code is as follows:

1 CString szpath;2GetModuleFileName (Null,spath.getbuffersetlength (max_path+1), MAX_PATH); 3 Szpath.releasebuffer ();4 intNPos;5npos=szpath.reversefind (' \ \ ');6Szpath=Szpath.left (nPos);7CString Szfile = spath +"\\image.mdb"; 8 Charszatr[ the]; 9sprintf (Szatr,"dsn=%s! dbq=%s! defaultdir=%s!!","MyImage", Szfile,szpath); Ten intNlen; OneNlen =strlen (SZATR); A  for(intI=0; i<nlen; i++)  - { -     if(Szatr [i] = = '!') theSzatr [i] = ' \0'; - }  - if(FALSE = = SQLConfigDataSource (NULL, ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)", (LPCSTR) szatr)) AfxMessageBox ("SQLConfigDataSource Failed");

After compiling and running the program, you can see the results of the operation through the Control Panel's ODBC data Source Manager or the registry, and you will notice that the database has been successfully registered.

Original address: http://dev.yesky.com/141/2010141.shtml

Visual C + + implements automatic configuration of ODBC data sources (RPM)

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.