Use VC program to create SQL Server ODBC data sources

zhaozj2021-02-16  48

When publishing software related to a database, if the program accesses the database in an ODBC, you need to create an ODBC data source when the software is installed.

Some of the ODBC data sources are stored in the registry.

HKEY_LOCAL_MECHINE / SOFTWARE / ODBC / ODBCINST.INI

From this key, you can know if the system has installed SQL Server drivers.

HKEY_LOCAL_MECHINE / Software / ODBC / ODBC.INI

From this key, you can know the data source that the system has established.

The implementation function is as follows:

//// function: makesqlserordbcdsn //// Purpose: Configuring SQL Server Data Source // Parameters: // [In] // STRDBSERVER: The host // strDBName: Database Name // strDSN: Data Source Name // StruID : Login User // Return Value: // -1: No SQL Server Driver // -2: Other Error // 0: Successfully // Int MakesqlServerodBCDSN (LPCTSTSTSTSTSERVER, LPCTSTSTSTSTSTSTSTSTSTSTSTSTSN, LPCTSTSTSTRUID) {BOOL BINSTALLDRIVER = TRUE; CREGKEY RegKey;

Long lret = regkey.open (HKEY_LOCAL_MACHINE, "Software // ODBC // odbcinst.ini // sql server"); if (lret! = Error_success) {binstallDriver = false;} else {char szdirverpath [max_path] = "" DWORD dwcount = 100; lret = regkey.queryvalue (szdirPath, "driver", & dwcount); if ((LRET! = Error_Success) || (dwcount <1)) {dWord dwerr = getLastError (); binstallDriver = false;} regKey .Close ();} if (bInstallDriver!) {return -1;} CString strKeyValueName = "Software // ODBC // ODBC.INI //"; strKeyValueName = strDSN; lRet = regKey.Create (HKEY_LOCAL_MACHINE, strKeyValueName); if (LRET! = Error_Success) {RETURN-2;} regkey.setValue (strdbname, "Database"); RegKey.SetValue ("SQLSRV32.DLL", "Driver"); RegKey.SetValue (strDbserver, "server"); regkey .SetValue (STRUID, "Lastuser"); RegKey.m_hKey = HKEY_LOCAL_MACHINE; RegKey.SetKeyValue ("Software // ODBC // ODBC.INI // ODBC Data Sources", "SQL Server", strDSN; regkey.close () Return 0;}

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

New Post(0)