主要代码如下:
void RegisterDBSource(CString strDSName, CString strDBPath)
{
HKEY hKey;
DWORD nLabel;
CString strBaseKey = _T("SOFTWARE\\ODBC\\ODBC.INI");
CString strMid = strBaseKey + _T("\\ODBC Data Sources") ;
if(strDSName.IsEmpty()) return;
if(strDBPath.IsEmpty()) return;
CString strDataSource = strBaseKey + _T("\\") + strDSName;
CString strMdb = _T("Microsoft Access Driver (*.mdb)");
CString strDBDriver = _T("C:\\WINNT\\System32\\odbcjt32.dll");
CString strFIL = _T("Ms Access;");
CString strUID = _T("");
RegCreateKeyEx(HKEY_CURRENT_USER,
strMid,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hKey,
&nLabel );//获取数据源键值句柄
RegSetValueEx(hKey,
strDSName,
0,
REG_SZ,
(const unsigned char *)((LPCTSTR)strMdb),
strlen((LPCTSTR)strMdb)+1);///设置数据源类型
RegCreateKeyEx(HKEY_CURRENT_USER,
strDataSource,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hKey,
&nLabel );//创建数据源子键
RegSetValueEx(hKey,
_T("DBQ"),
0,
REG_SZ,
(const unsigned char *)((LPCTSTR)strDBPath),
strlen((LPCTSTR)strDBPath)+1);//数据库表的全路径
RegSetValueEx(hKey,
_T("Driver"),
0,
REG_SZ,
(const unsigned char *)((LPCTSTR)strDBDriver),
strlen((LPCTSTR)strDBDriver)+1);//ODBC驱动的全路径
RegSetValueEx(hKey,
_T("FIL"),
0,
REG_SZ,
(const unsigned char *)((LPCTSTR)strFIL),
strlen((LPCTSTR)strFIL)+1);//表的类型
RegSetValueEx(hKey,
_T("UID"),
0,
REG_SZ,
(const unsigned char *)((LPCTSTR)strUID),
strlen((LPCTSTR)strUID)+1);//必须项
DWORD DriverId = (DWORD)25;
RegSetValueEx(hKey,
_T("DriverId"),
0,
REG_DWORD,
(const BYTE *)(&DriverId),
sizeof(DWORD));//必须项
DWORD SafeTrans = (DWORD)0;
RegSetValueEx(hKey,
_T("SafeTransactions"),
0,
REG_DWORD,
(const BYTE *)(&SafeTrans),
sizeof(DWORD));//可选项
}
调试环境:WINDOWS2000 + VC6.0 + VSP5.0
本文地址:http://com.8s8s.com/it/it1167.htm