zoukankan      html  css  js  c++  java
  • c++ 连接数据库

    #include <icrsint.h>
    #include<iostream>
    #include<iomanip>
    #include <string>
    using namespace std;
    #import "c:program filescommon filessystemadomsado15.dll"  no_namespace rename("EOF", "adoEOF")
    void main()
    {
        CoInitialize(NULL);
        _ConnectionPtr  sqlSp;
        HRESULT hr = sqlSp.CreateInstance(_uuidof(Connection));
        if (FAILED(hr))
        {
            cout << "_ConnectionPtr对象指针实例化失败!!!" << endl;
            return;
        }
        else {
            try {
                _bstr_t strConnect = "Driver={sql server};server=127.0.0.1,1433;uid=sa;pwd=123;database=test;";
                sqlSp->Open(strConnect, "", "", adModeUnknown);
            }
            catch (_com_error &e) {
                cout << e.Description() << endl;
            }
            _RecordsetPtr m_pRecordset;
            if (FAILED(m_pRecordset.CreateInstance(_uuidof(Recordset))))
            {
                cout << "记录集对象指针实例化失败!" << endl;
                return;
            }
            try {
                m_pRecordset->Open("select * from  T_test2", (IDispatch*)sqlSp, adOpenDynamic, adLockOptimistic, adCmdText);
            }
            catch (_com_error &e)
            {
                cout << e.Description() << endl;
            }
            try {
                m_pRecordset->MoveFirst();
                while (!m_pRecordset->adoEOF)
                {
                    string id = (char*)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("id"))->Value);
                    cout << id ; 
                    string name = (char*)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("name"))->Value);
                    cout << name ;
                    string age = (char*)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("age"))->Value);
                    cout << age << endl;
                    m_pRecordset->MoveNext();
                }
            }
            catch (_com_error &e)
            {
                cout << e.Description() << endl;
            }
        }
        system("pause");
    }
  • 相关阅读:
    点双连通分量
    CF 839 E-最大团
    线性基
    CF 859 E
    【bzoj1303】[CQOI2009]中位数图
    【bzoj2282】[Sdoi2011]消防
    【bzoj2654]】tree
    【bzoj2600】 [Ioi2011]ricehub
    【bzoj4027】 [HEOI2015]兔子与樱花
    【bzoj3670】 [Noi2014]动物园
  • 原文地址:https://www.cnblogs.com/linst/p/4908926.html
Copyright © 2011-2022 走看看