寫(xiě)注冊(cè)表動(dòng)態(tài)創(chuàng)建SQL Server ODBC數(shù)據(jù)源
發(fā)表時(shí)間:2024-02-16 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]各個(gè)參數(shù)的意義:DBServer: 數(shù)據(jù)庫(kù)所在主機(jī)DBName: 數(shù)據(jù)庫(kù)名稱DSN: 數(shù)據(jù)源名稱UID: 登陸用戶返回值:-1: 沒(méi)有安裝SQL Server驅(qū)動(dòng)程序-2: 其他錯(cuò)誤0: 成功實(shí)現(xiàn)函數(shù):int MakeSQLServerODBCDSN(LPCTSTR DBServer,L...
各個(gè)參數(shù)的意義:
DBServer: 數(shù)據(jù)庫(kù)所在主機(jī)
DBName: 數(shù)據(jù)庫(kù)名稱
DSN: 數(shù)據(jù)源名稱
UID: 登陸用戶
返回值:
-1: 沒(méi)有安裝SQL Server驅(qū)動(dòng)程序
-2: 其他錯(cuò)誤
0: 成功
實(shí)現(xiàn)函數(shù):
int MakeSQLServerODBCDSN(LPCTSTR DBServer,LPCTSTR DBName,LPCTSTR DSN,LPCTSTR UID)
{
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\\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\\ODBC.INI\\ODBC Data Sources","SQL Server",DSN);
regKey.Close();
return 0;
}