Chinaunix首页 | 论坛 | 博客
  • 博客访问: 269281
  • 博文数量: 45
  • 博客积分: 1618
  • 博客等级: 上尉
  • 技术积分: 530
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-11 10:13
文章分类

全部博文(45)

文章存档

2012年(1)

2011年(25)

2010年(19)

我的朋友

分类: C/C++

2010-12-23 10:57:15

#include <windows.h>
#include <stdio.h>

#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")

int main(int argc, char* argv[])
{

HRESULT hr = S_OK;
    try
    {
         CoInitialize(NULL);
          // Define string variables.

        _bstr_t strCnn("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=username;Password=passwd;Initial Catalog=database;Data Source=(local);Integrated Security=SSPI;");

       _RecordsetPtr pRstAuthors = NULL;

      // Call Create instance to instantiate the Record set

      hr = pRstAuthors.CreateInstance(__uuidof(Recordset));

      if(FAILED(hr))
      {
            printf("Failed creating record set instance\n");
            return 0;
       }

      //Open the Record set for getting records from Author table

      pRstAuthors->Open("SELECT Author_ID,username FROM Author",strCnn, adOpenStatic, adLockReadOnly,adCmdText);

      //Declare a variable of type _bstr_t

     _bstr_t valField1;
     int valField2;

     pRstAuthors->MoveFirst();

    //Loop through the Record set

    if (!pRstAuthors->EndOfFile)
    {
       while(!pRstAuthors->EndOfFile)
       {
          valField1 = pRstAuthors->Fields->GetItem("username")->Value;
          valField2 = pRstAuthors->Fields->GetItem("Author_ID")->Value.intVal;
          printf("%d - %s\n",valField2,(LPCSTR)valField1);
          pRstAuthors->MoveNext();
       }
    }

   }
   catch(_com_error & ce)
   {
      printf("Error:%s\n",ce.Description);
   }

  CoUninitialize();
  return 0;
}


阅读(1193) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~