Write a registry to dynamically create an SQL Server ODBC Data Source
Meanings of parameters:
Dbserver: host where the database is located
Dbname: Database Name
DSN: Data Source Name
UID: Login User
Return Value:
-1: the SQL Server Driver is not installed.
-2: other errors
0: Successful
Implementation functions:
Int makesqlserverodbcdsn (maid, maid)
{
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 (szdirverpath, "driver", & dwcount );
If (LRET! = Error_success) | (dwcount <1 ))
{
DWORD dwerr = getlasterror ();
Binstalldriver = false;
}
Regkey. Close ();
}
If (! Binstalldriver)
{
Return-1;
}
Cstring strkeyvaluename = "software // ODBC. ini //";
Strkeyvaluename + = DSN;
LRET = regkey. Create (HKEY_LOCAL_MACHINE, strkeyvaluename );
If (LRET! = Error_success)
{
Return-2;
}
Regkey. setvalue (dbname, "Database ");
Regkey. setvalue ("sqlsrv32.dll", "driver ");
Regkey. setvalue (dbserver, "server ");
Regkey. setvalue (UID, "lastuser ");
Regkey. m_hkey = HKEY_LOCAL_MACHINE;
Regkey. setkeyvalue ("software // ODBC. ini // ODBC Data Sources", "SQL Server", DSN );
Regkey. Close ();
Return 0;
}