简体   繁体   English

C ++如何使两个语句SQL与OLEDB一起工作?

[英]c++ how to make two statement sql work with OLEDB?

I have the following SQL statement: 我有以下SQL语句:

USE "ws_results_db_2011_09_11_09_06_24";SELECT table_name FROM INFORMATION_SCHEMA.Tables WHERE table_name like 'NET_%_STAT' order by table_name

I am using the following C++ code to execute it: 我正在使用以下C ++代码执行它:

IDBCreateCommandPtr spDBCreateCommand = GetTheDBCreateCommandPointer();
ICommandTextPtr spCommandText;
spDBCreateCommand->CreateCommand(NULL, IID_ICommandText, reinterpret_cast<IUnknown **>(&spCommandText));
spCommandText->SetCommandText(DBGUID_SQL, GetTheQueryText());
IRowsetPtr spRowset;
spCommandText->Execute(NULL, IID_IRowset, NULL, NULL, reinterpret_cast<IUnknown **>(&spRowset));
RowHandles hRows(spRowset, 0);
ULONG   rowCount;
ULONG maxRowCount = 1;
spRowset->GetNextRows(DB_NULL_HCHAPTER, 0, maxRowCount, &rowCount, hRows.get_addr());

Two notes: 两个注意事项:

  1. Error handling is omitted for brevity 为简便起见,省略了错误处理
  2. RowHandles implements the RAII concept for HROW * RowHandles实现RAII概念HROW *

Anyway, I fail to execute the two SQL statements. 无论如何,我无法执行两个SQL语句。 What happens is that spCommandText->Execute returns S_OK , but sets spRowset to NULL . 发生的情况是spCommandText->Execute返回S_OK ,但是将spRowset设置为NULL

If I execute the same spCommandText->Execute the second time (by moving back the instruction pointer during the debugging session), then a valid IRowset pointer is returned - I successfully obtain the correct column information using it. 如果我第二次执行相同的spCommandText->Execute (通过在调试会话期间移回指令指针),则返回有效的IRowset指针-我成功地使用它获得了正确的列信息。 But spRowset->GetNextRows sets rowCount to 0 and returns DB_S_ENDOFROWSET - no luck. 但是spRowset->GetNextRowsrowCount设置为0并返回DB_S_ENDOFROWSET不走运。

The code is working fine when I execute a single SQL statement. 当我执行一个SQL语句时,代码运行良好。

What am I doing wrong? 我究竟做错了什么?

Thanks. 谢谢。

It is up to the client to split the sql commands - isql does this on the ; 由客户端来拆分sql命令-isql在上进行; ie you are asking for two commands the use and the select. 即您要使用和选择两个命令。
So the fix is to do the two commands by separate sets of CreateCommands and execute. 因此,解决方法是通过单独的CreateCommands集执行两个命令并执行。

Also note in this case you can do the commands as one SQL statement 另请注意,在这种情况下,您可以将命令作为一条SQL语句执行

SELECT table_name FROM ws_results_db_2011_09_11_09_06_24.INFORMATION_SCHEMA.Tables 
  WHERE table_name like 'NET_%_STAT' 
  order by table_name

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

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