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

来源:互联网 发布:数据统计分析平台 编辑:程序博客网 时间:2024/06/05 10:35
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

 

主要代码如下:

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

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击