直接看代码吧
/////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <atlbase.h>
#include <atlcomcli.h>
#include <atlstr.h>#import "C:\Program Files\Common Files\System\ado\msado15.dll" \
raw_interfaces_only named_guids no_namespace rename("EOF","ADOEOF")#if defined(_DEBUG)
#include "./Debug/msado15.tlh"
#else
#include "./Release/msado15.tlh"
#endifint _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL); CComBSTR bstrConnString(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="));
bstrConnString += _T(".\\modb.xls");
bstrConnString += _T(";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\""); CComPtr<_Connection> spConn;
HRESULT hr = spConn.CoCreateInstance(_T("ADODB.Connection"));
CComPtr<_Recordset> spRecordset;
hr = spRecordset.CoCreateInstance(_T("ADODB.Recordset")); CComBSTR bstrEmpty;
hr = spConn->Open(bstrConnString,bstrEmpty,bstrEmpty,adConnectUnspecified);
if(FAILED(hr))
{
CoUninitialize();
return -1;
} CComVariant varConn((IDispatch*)spConn);
hr = spRecordset->Open(CComVariant(_T("select * from [DemoData$]")),varConn,adOpenDynamic,adLockOptimistic,adCmdText);
if(FAILED(hr))
{
spConn->Close();
CoUninitialize();
return -1;
} VARIANT_BOOL bvarEOF = VARIANT_FALSE;
hr = spRecordset->MoveFirst();
hr = spRecordset->get_ADOEOF(&bvarEOF);
while(!bvarEOF)
{
CComPtr<Fields> spFields;
long nFieldsCount = 0;
hr = spRecordset->get_Fields(&spFields);
hr = spFields->get_Count(&nFieldsCount); CComBSTR bstrFieldName;
CComVariant varFieldValue;
for(int i = 0; i < nFieldsCount; ++i)
{
CComPtr<Field> spField;
hr = spFields->get_Item(CComVariant(i),&spField);
if(SUCCEEDED(hr))
{
CComBSTR bstrBuffer;
bstrFieldName.Empty();
varFieldValue.Clear();
hr = spField->get_Name(&bstrFieldName);
ATLASSERT(SUCCEEDED(hr));
hr = spField->get_Value(&varFieldValue);
ATLASSERT(SUCCEEDED(hr));
varFieldValue.ChangeType(VT_BSTR);
// ......
// ......
}
}
hr = spRecordset->MoveNext();
hr = spRecordset->get_ADOEOF(&bvarEOF);
}
hr = spRecordset->Close(); CComPtr<_Command> spCommand;
hr = spCommand.CoCreateInstance(_T("ADODB.Command"));
hr = spCommand->put_ActiveConnection(varConn); CComBSTR bstrCommand(_T("create table DataDemo2(id INT,Name CHAR(32))"));
hr = spCommand->put_CommandText(bstrCommand);
hr = spCommand->put_CommandType(adCmdText); CComPtr<_Recordset> spRecordsetRet;
hr = spCommand->Execute(NULL,NULL,adCmdText,&spRecordsetRet); // 问题在这里 hr 返回值为 DB_SEC_E_PERMISSIONDENIED
// 建个工作表不行,
// Error Number: 0x80040E09
// Symbolic Name: DB_SEC_E_PERMISSIONDENIED
// Text description: Permission denied.
// ????? 谁搞过,帮忙
hr = spConn->Close();
CoUninitialize();
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <atlbase.h>
#include <atlcomcli.h>
#include <atlstr.h>#import "C:\Program Files\Common Files\System\ado\msado15.dll" \
raw_interfaces_only named_guids no_namespace rename("EOF","ADOEOF")#if defined(_DEBUG)
#include "./Debug/msado15.tlh"
#else
#include "./Release/msado15.tlh"
#endifint _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL); CComBSTR bstrConnString(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="));
bstrConnString += _T(".\\modb.xls");
bstrConnString += _T(";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\""); CComPtr<_Connection> spConn;
HRESULT hr = spConn.CoCreateInstance(_T("ADODB.Connection"));
CComPtr<_Recordset> spRecordset;
hr = spRecordset.CoCreateInstance(_T("ADODB.Recordset")); CComBSTR bstrEmpty;
hr = spConn->Open(bstrConnString,bstrEmpty,bstrEmpty,adConnectUnspecified);
if(FAILED(hr))
{
CoUninitialize();
return -1;
} CComVariant varConn((IDispatch*)spConn);
hr = spRecordset->Open(CComVariant(_T("select * from [DemoData$]")),varConn,adOpenDynamic,adLockOptimistic,adCmdText);
if(FAILED(hr))
{
spConn->Close();
CoUninitialize();
return -1;
} VARIANT_BOOL bvarEOF = VARIANT_FALSE;
hr = spRecordset->MoveFirst();
hr = spRecordset->get_ADOEOF(&bvarEOF);
while(!bvarEOF)
{
CComPtr<Fields> spFields;
long nFieldsCount = 0;
hr = spRecordset->get_Fields(&spFields);
hr = spFields->get_Count(&nFieldsCount); CComBSTR bstrFieldName;
CComVariant varFieldValue;
for(int i = 0; i < nFieldsCount; ++i)
{
CComPtr<Field> spField;
hr = spFields->get_Item(CComVariant(i),&spField);
if(SUCCEEDED(hr))
{
CComBSTR bstrBuffer;
bstrFieldName.Empty();
varFieldValue.Clear();
hr = spField->get_Name(&bstrFieldName);
ATLASSERT(SUCCEEDED(hr));
hr = spField->get_Value(&varFieldValue);
ATLASSERT(SUCCEEDED(hr));
varFieldValue.ChangeType(VT_BSTR);
// ......
// ......
}
}
hr = spRecordset->MoveNext();
hr = spRecordset->get_ADOEOF(&bvarEOF);
}
hr = spRecordset->Close(); CComPtr<_Command> spCommand;
hr = spCommand.CoCreateInstance(_T("ADODB.Command"));
hr = spCommand->put_ActiveConnection(varConn); CComBSTR bstrCommand(_T("create table DataDemo2(id INT,Name CHAR(32))"));
hr = spCommand->put_CommandText(bstrCommand);
hr = spCommand->put_CommandType(adCmdText); CComPtr<_Recordset> spRecordsetRet;
hr = spCommand->Execute(NULL,NULL,adCmdText,&spRecordsetRet); // 问题在这里 hr 返回值为 DB_SEC_E_PERMISSIONDENIED
// 建个工作表不行,
// Error Number: 0x80040E09
// Symbolic Name: DB_SEC_E_PERMISSIONDENIED
// Text description: Permission denied.
// ????? 谁搞过,帮忙
hr = spConn->Close();
CoUninitialize();
return 0;
}
链接字符串中 用“\"Excel 8.0;HDR=Yes;IMEX=1\""); 就会出错, 如果用Excel 8.0 就不会出错。请问什么原因啊