简体   繁体   English

C ++ ADO连接

[英]C++ ADO Connection

I am trying to read an excel file. 我正在尝试读取Excel文件。 I am trying to make a simple ADO connection using C++. 我正在尝试使用C ++建立一个简单的ADO连接。 But while opening the connection it is getting failed. 但是在打开连接时却失败了。

this is the code am using 这是代码正在使用

#import "C:\Program Files (x86)\Common Files\System\ado\msado15.dll" rename_namespace("ADOCG") rename("EOF", "EndOfFile")

int _tmain(int argc, _TCHAR* argv[])
{
    ADOCG::_ConnectionPtr conn = NULL;
    ADOCG::_RecordsetPtr rset = NULL;

    conn.CreateInstance(__uuidof(ADOCG::Connection));
    rset.CreateInstance(__uuidof(ADOCG::Recordset));

    try
    {
        //bstr_t bstrCnxt("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\sample.mdb;Extended Properties='MSAccess 12.0;HDR=Yes'"); 
        conn->Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\weightreport.xls;Extended Properties='Excel 12.0;HDR=Yes'","","", ADOCG::adConnectUnspecified);

    }
    catch(const char * str)
    {

    }
}

You should initialize COM first. 您应该首先初始化COM。 Add if (FAILED(::CoInitialize(nullptr))) {return 0;} above _ConnectionPtr and Add ::CoUninitialize(); _ConnectionPtr上方添加if (FAILED(::CoInitialize(nullptr))) {return 0;}并添加::CoUninitialize(); at the end of _tmain _tmain的末尾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM