CSDN博客

img taianmonkey

通过写入注册表添加ODBC数据源

发表于2004/5/22 12:40:00  1165人阅读

 

主要代码如下:

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

0 0

相关博文

我的热门文章

img
取 消
img