重发!请大家指点一下里边的一段程序为什么执行不下去。

freeghost 2002-01-16 02:10:12
CDatabase database;


database.Open(NULL,false,false,_T(ODBC;DRIVER={SQL Server};Server=Abc;DATABASE=zcg;UID=sa;PWD=),true);
//确定以上连接数据库可以成功

CRecordset recordset(&database);
recordset.Open(AFX_DB_USE_DEFAULT_TYPE,"select * from printdata order by a1",CRecordset::readOnly);
...
...

请问这一段程序为什么在执行recordset.open时一直停在那里没有返回?该如何解决?




...全文
72 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
freeghost 2002-01-17
  • 打赏
  • 举报
回复
还是不行呀。
freeghost 2002-01-16
  • 打赏
  • 举报
回复
我把这一句改成:
recordset.Open(CRecordset::snapshot,_T("select * from printdata"),CRecordset::readOnly);
还是不行呀,到这里后就不反回一直在等。
guoduning 2002-01-16
  • 打赏
  • 举报
回复
在“select * from printdata order by a1”中不能有多余的空格。

变为 _T"select * from printdata order by a1"
还有可能是一个SQL的BUG:
BUG: Double Brackets in SQL Statements with MFC May Cause Error

Q257171


--------------------------------------------------------------------------------
The information in this article applies to:

The Microsoft Foundation Classes (MFC), used with:
Microsoft Visual C++, 32-bit Editions, version 6.0

--------------------------------------------------------------------------------


SYMPTOMS
An error may occur when, in order to preserve a set of square brackets, you add an extra set of brackets to an SQL statement. For example:



Select * from myview where [[tableA.field1]]
The error that is returned depends on which Open Database Connectivity (ODBC) driver is used and on the structure of the SQL statement, but can include a syntax error, an invalid table or field name, or a variety of other problems.



CAUSE
The Microsoft Foundation Classes (MFC) ODBC function CDatabase::ReplaceBrackets replaces square brackets in an SQL statement with a driver-dependent quote character (SQL_IDENTIFIER_QUOTE_CHAR). The CDatabase::ReplaceBrackets function, which is called by the CRecordset::Open function, is used frequently.

When double brackets are replaced with single brackets, the string length changes but the CDatabase::ReplaceBrackets function does not make the necessary adjustment. The SQL statement that is passed to the driver contains extra characters at the end of the string, which can cause errors.




WORKAROUND
Pad the end of your SQL statement with one blank space for each double bracket used. For example, in the following statement

"Select * from myview where [[tableA.field1]]"
place two spaces at the end of the statement:

"Select * from myview where [[tableA.field1]] "



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.



MORE INFORMATION

Steps to Reproduce Behavior
Create a table in a Microsoft SQL Server database that uses a keyword for a field name (for instance "key"). If the Data Source Name (DSN) setting or connection string has quoted identifiers turned off, double brackets are needed. (Note that this is just an example; there are a number of other situations where double brackets are needed as well.)


Create an MFC ODBC application with a recordset based on this new table and make the following call:

keyRS.Open(AFX_DB_USE_DEFAULT_TYPE, "Select [[key]] from TableA");
This call produces the following error:


Invalid object name 'TableAeA'.
Statement(s) could not be prepared.
Set a breakpoint on the call to the CDatabase::ReplaceBrackets function inside of the CRecordset::BuildSQL function. You will see that before the call, m_strSQL contains the following

Select [[key]] from TableA
whereas after the call returns, m_strSQL contains the following:

Select [key] from TableAeA
The CDatabase::ReplaceBrackets function removed the extra set of brackets but didn't correctly terminate the string. The null terminator remains in its original position, which makes the table name invalid.



Additional query words: escape character square brace braces

freeghost 2002-01-16
  • 打赏
  • 举报
回复
真的没人知道吗?
freeghost 2002-01-16
  • 打赏
  • 举报
回复
"select * from printdata order by a1"
这一句在SQL Client中执行是可以通过的。并且有结果返回,而且如果这一句有问题地话在C里也会报错的。

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧