ADO基础知识:使用Connection对象连接数据库

使用ADO中的Connection对象连接数据库:


int _tmain(int argc, _TCHAR* argv[])
{
    CoInitialize(NULL);
    Connection15Ptr pConnect;
    HRESULT RET;
    try { 
    RET  = pConnect.CreateInstance("ADODB.Connection");
    
    pConnect->PutConnectionString("Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=.");
    pConnect->PutConnectionTimeout(10);
    pConnect->PutCursorLocation(adUseClient);


    pConnect->Open(_bstr_t(""),_bstr_t(""),_bstr_t(""),adConnectUnspecified);
    cout<<"登录成功了..\n";
    pConnect->Close();
    }
    catch(_com_error &e)
    {
        if(pConnect)
        cout<<"Error"<<(LPCSTR)e.Description()<         else
            cout<<"connection 未初始..."<     }
    CoUninitialize();
return 0;
}

你可能感兴趣的:(ADO基础知识:使用Connection对象连接数据库)