gpt4 book ai didi

c++ - SQL 查询返回 DB_E_NOTABLE

转载 作者:行者123 更新时间:2023-11-30 03:32:37 24 4
gpt4 key购买 nike

我正在构建我的第一个 MFC 应用程序。我已成功连接到数据库,但当我尝试执行查询时,hResult 变为 DB_E_NOTABLE

这是我的代码:

class CCitiesTable : public CTable<CAccessor<CCitiesTableAccessor> >
{
public:

bool SelectAll(CCitiesArray& oCitiesArray)
{
CDataSource oDataSource;
CSession oSession;
CCitiesTable oCitiesTable;

try
{
HRESULT hResult = CoInitialize(0);

if (FAILED(hResult))
{
//Message( _T("Unable to CoInitialize COM Interface.") );
return FALSE;
}
CDBPropSet oDBPropSet(DBPROPSET_DBINIT);
oDBPropSet.AddProperty(DBPROP_INIT_DATASOURCE, _T("LENOVO2\\SQL2008") ); // сървър
oDBPropSet.AddProperty(DBPROP_AUTH_USERID, _T("sa") );
oDBPropSet.AddProperty(DBPROP_AUTH_PASSWORD, _T("massive") );
oDBPropSet.AddProperty(DBPROP_INIT_CATALOG, _T("PhoneBookDB" ));
oDBPropSet.AddProperty(DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, false);
oDBPropSet.AddProperty(DBPROP_INIT_LCID, 1033L);
oDBPropSet.AddProperty(DBPROP_INIT_PROMPT, static_cast<short>(4));

// Свързваме се към базата данни
hResult = oDataSource.Open(_T("SQLOLEDB.1"), &oDBPropSet);

if (FAILED(hResult))
{
//Message(_T("Unable to connect to SQL Server database. Error: %d"), hResult);
return FALSE;
}

//Open session
hResult = oSession.Open(oDataSource);

if (FAILED(hResult))
{
//Message(_T("Unable to open session. Error: %d"), hResult);
oDataSource.Close();
return FALSE;
}

//Make query
CString strQuery;
strQuery = _T("SELECT * FROM dbo.CITIES");

// Execute query
hResult = oCitiesTable.Open(oSession, strQuery);

执行上面的代码后,hResult 是 DB_E_NOTABLE。除了 table 在那里。数据库:PhoneBookDB 架构:dbo 表:CITIES。该查询在 SQL Server Management Studio 中运行良好。

最佳答案

根据 CTable::Open 的文档, CTable::Open 需要一个表名,而不是一个选择查询。您得到的错误是说 "SELECT * FROM dbo.CITIES" 不是表名(它不是)。

szTableName [in] The name of the table to open, passed as an ANSI string.

你需要使用:

CString strTable = _T("CITIES");

// Open Table
hResult = oCitiesTable.Open(oSession, strTable);

关于c++ - SQL 查询返回 DB_E_NOTABLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43392673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com