Write a registry to dynamically create an SQL Server ODBC Data Source

Source: Internet
Author: User
Tags sql server driver

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;
}
 

Related Article

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.