zoukankan      html  css  js  c++  java
  • VC连接SQL2005(例子ADO_2)

    (1)    在StdAfx.h中添加下面一句话:

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

    (2)    void CADO_2Dlg::OnBtnQuery() 
    
    {
    
           CoInitialize(NULL);                                                        // 初始化COM库
    
           _ConnectionPtr pConn(__uuidof(Connection));          // 建立Connection
    
           _RecordsetPtr pRst(__uuidof(Recordset));          // 建立Recordset
    
     
    
           _CommandPtr pCmd(__uuidof(Command));                     // 建立Command
    
     
    
           try
    
           {
    
                  //pConn->ConnectionString = "Provider=SQLOLEDB;Password=XXX;Persist Security Info=True;User ID=sa;Initial Catalog=pubs";       //此句正确
    
                  pConn->ConnectionString = "driver={SQL Server};Server=(local);DATABASE=pubs;UID=sa;PWD=xxx";
    
                  pConn->Open("", "", "", adConnectUnspecified);
    
     
    
                  //pRst = pConn->Execute("select * from authors", NULL, adCmdText);
    
                  //pRst->Open("select * from authors", _variant_t((IDispatch*) pConn), adOpenDynamic, adLockOptimistic, adCmdText);
    
                  pCmd->put_ActiveConnection(_variant_t((IDispatch*) pConn));
    
                  pCmd->CommandText = "select * from authors";
    
                  pRst = pCmd->Execute(NULL, NULL, adCmdText);
    
     
    
     
    
                  while(!pRst->rsEOF)
    
                  {
    
                         ((CListBox*)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("au_lname")); //查询au_lname字段
    
                         pRst->MoveNext();
    
                  }
    
           }
    
           catch(_com_error e)
    
           {
    
                  CString errormessage;
    
                  errormessage.Format("Error: %s", e.ErrorMessage());
    
                  AfxMessageBox(errormessage);
    
           }
    
     
    
           AfxMessageBox("查询结束!");
    
     
    
           pRst->Close();
    
           pConn->Close();
    
           pRst.Release();                                                               // 释放相应COM接口上的引用计数 
    
           pConn.Release();
    
           CoUninitialize();                                                       // 卸载COM库
    
    }
    
    
    
  • 相关阅读:
    字典树入门
    Cyclic Nacklace HDU 3746 KMP 循环节
    KMP字符串匹配 模板 洛谷 P3375
    Phone List POJ-3630 字典树 or 暴力
    stringstream istringstream ostringstream 三者的区别
    单词数 HDU 2072 字符串输入控制
    逆序单词 HIhoCoder 1366 字典树
    input框中修改placeholder的样式
    如何使用$.each()与$().each()以及他们的区别
    css解决input的阴影
  • 原文地址:https://www.cnblogs.com/joeblackzqq/p/1815287.html
Copyright © 2011-2022 走看看