Windows环境中使用OLEDB连接SQL Server,本文只作为对前两篇博文的补充,并非推荐做法。

下面例子依次调用了:
  • 初始化环境
  • 连接SQL Server
  • 执行查询
  • 关闭连接

示例代码:
oledbTest.cpp

点击(此处)折叠或打开

  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <sys\timeb.h>
  4. #include <time.h>
  5. #include <stdio.h>
  6. #include <iostream>
  7. #include <map>
  8. #include <atldbcli.h>
  9. #include <atlconv.h>
  10. using namespace std;
  11.  
  12. typedef struct result
  13. {
  14.     char ename[19];
  15.     char cname[100];
  16.     DBSTATUS dwEnameStatus;
  17.     DBSTATUS dwCnameStatus;
  18. } RESULT;
  19.  
  20. int _tmain(int argc, _TCHAR* argv[])
  21. {
  22.     HRESULT hr;
  23.     CDataSource m_ds;
  24.     CSession m_session;
  25.  
  26.     //连接串指定数据库,用户名,密码
  27.     char szConnectionString[512] = "Provider=SQLNCLI10.1;Data Source=10.2.0.10;Initial Catalog=MYDB;User ID=user;Password=passwd;Application Name=SQLSERVER;MARS Connection=True";
  28.  
  29.     ::CoInitialize(NULL);
  30.  
  31.     //连接数据库
  32.     hr = m_ds.OpenFromInitializationString(CA2W(szConnectionString));
  33.     
  34.     //打开会话
  35.     hr = m_session.Open(m_ds);

  36.     DBORDINAL ulColumns = 0;
  37.     CDBPropSet dbPropSet(DBPROPSET_ROWSET);
  38.     CCommand<CManualAccessor, CRowset, CNoMultipleResults> m_command;
  39.     hr = m_command.CreateCommand(m_session);
  40.  
  41.     CComPtr<ICommandText> spCommandText;
  42.     hr = m_command.m_spCommand->QueryInterface(&spCommandText);
  43.     USES_CONVERSION;
  44.     
  45.     //查询语句
  46.     const TCHAR *cmd = _T(" select col1, col2 from table1 ");
  47.  
  48.     hr = spCommandText->SetCommandText(DBGUID_SQL, T2COLE(cmd));
  49.  
  50.     dbPropSet.AddProperty(DBPROP_ISequentialStream, true);
  51.  
  52.     hr = m_command.Open(&dbPropSet, NULL, false);
  53.  
  54.     DBCOLUMNINFO *m_pColumnInfo;
  55.     LPOLESTR m_pStrings;
  56.  
  57.     //获取列信息
  58.     hr = m_command.GetColumnInfo(&ulColumns
  59.         , &m_pColumnInfo
  60.         , &m_pStrings);
  61.  
  62.     BYTE * m_pRowInfoBuff;
  63.     
  64.     //初始化列绑定字段
  65.     DBCOUNTITEM cbRowSize = (m_pColumnInfo[0].ulColumnSize + 1) * sizeof(CHAR);
  66.     DBCOUNTITEM cbRowSize2 = (m_pColumnInfo[1].ulColumnSize + 1) * sizeof(CHAR);
  67.  
  68.     RESULT rst;
  69.  
  70.     int bufferSize = sizeof(rst.ename) + sizeof(rst.cname) +1;
  71.     m_pRowInfoBuff = new BYTE[bufferSize];
  72.  
  73.     hr = m_command.CreateAccessor(ulColumns, m_pRowInfoBuff, (DBLENGTH)bufferSize);
  74.  
  75.     //DBORDINAL* pLength = (DBORDINAL*)(m_pRowInfoBuff + cbRowSize);
  76.     //DBSTATUS* pStatus = (DBSTATUS*)(m_pRowInfoBuff + cbRowSize);
  77.     DBTYPE dbType=DBTYPE_STR;
  78.     DBLENGTH dbLength = (m_pColumnInfo[0].ulColumnSize + 1) * sizeof(TCHAR);
  79.     DBLENGTH dbLength2 = (m_pColumnInfo[1].ulColumnSize + 1) * sizeof(TCHAR);
  80.  
  81.     //m_command.AddBindEntry(1, dbType, dbLength, pValue, pLength, pStatus);
  82.     m_command.AddBindEntry(1, dbType, dbLength, rst.ename, NULL, &rst.dwEnameStatus);
  83.     m_command.AddBindEntry(2, dbType, dbLength2, rst.cname, NULL, &rst.dwCnameStatus);
  84.  
  85.     hr = m_command.Bind();
  86.  
  87.     //获取查询信息
  88.     while(S_OK == m_command.MoveNext())
  89.     {
  90.         printf(" ename = [%s] ,cname = [%s]\n", rst.ename, rst.cname);
  91.     }
  92.  
  93.     //关闭连接
  94.     m_command.Close();
  95.     m_session.Close();
  96.     m_ds.Close();
  97.  
  98.     ::CoUninitialize();
  99.  
  100.     ::getchar();
  101.  
  102. }


09-05 14:58