我有一个对话框,表现为主窗口的子窗口。对话框具有树视图控件。它们是使用资源编辑器和WinAPI创建的。
我正在使用ADO和C ++使用数据库中的一些数据填充树视图。不幸的是,我没有得到预期的结果。
为了理解我的问题,我将提供数据库表中的列的描述,我从中获取数据:
以下是上表的相关值:
ID | BrojUgovora
-----------------
1 | qwert
2 | prvi ugovor
3 | drugi ugovor
运行我的代码后,我希望得到以下结果: drugi ugovor prvi ugovor QWERT
但我明白了:
drugi ugovor
prvi ugovor
ID -------> why column name instead of column value???
当我通过lParam将我存储ID的部分注释到treeview节点时,我得到了正确的结果。
我需要将ID存储到lParam中,因此删除该部分代码不是一种选择。
以下是我在WM_INITDIALOG
中调用的函数:
/*********** REMARKS ***************
/**** Fills treeview control with the contract number
/**** In treeview node's LPARAM is stored the value of the primary key
/**** Returns the number of failed attempts to load string/autonumber field
/***********************************/
int InitTreeView(HWND hDlg)
{
// error result
int iNumberOfFailedLoads = 0;
//connect to database
ADODB::_ConnectionPtr pConn("ADODB.Connection");
try
{
HRESULT hr = pConn->Open(bstrConnect, username, password,
ADODB::adConnectUnspecified);
if (!SUCCEEDED(hr))
throw _com_error(hr);
ADODB::_CommandPtr pCmd("ADODB.Command");
pCmd->ActiveConnection = pConn;
pCmd->CommandType = ADODB::adCmdText;
pCmd->CommandText = L" select ID, BrojUgovora from UGOVORI;";
ADODB::_RecordsetPtr pRS = pCmd->Execute(NULL, NULL, ADODB::adCmdText);
ADODB::Fields* pFields = NULL;
hr = pRS->get_Fields(&pFields);
if (!SUCCEEDED(hr))
throw _com_error(hr);
if (pFields && pFields->GetCount() > 0)
{
while (!pRS->AdoNSEOF)
{
// load contract values into treeview
TVINSERTSTRUCT tvis = { 0 };
tvis.item.mask = TVIF_TEXT | TVIF_PARAM;
tvis.hInsertAfter = TVI_FIRST;
tvis.item.pszText = // this is string field
pRS->Fields->GetItem(L"BrojUgovora")->Value.bstrVal;
tvis.item.lParam = // this is autonumber field
(LPARAM)(pRS->Fields->GetItem(L"ID")->Value.lVal);
HTREEITEM hti = TreeView_InsertItem(GetDlgItem(hDlg, IDC_TREE1),
&tvis);
if (NULL == hti)
iNumberOfFailedLoads++;
pRS->MoveNext();
}
pRS->Close();
}
pConn->Close();
}
catch (_com_error &e)
{
if (pConn->State == ADODB::adStateOpen)
pConn->Close();
iNumberOfFailedLoads = -1;
}
return iNumberOfFailedLoads;
}
这是WM_INITDIALOG
处理程序:
case WM_INITDIALOG:
{
// needed for visual styles, long story
EnableThemeDialogTexture(hDlg, ETDT_ENABLETAB);
InitTreeView(hDlg);
}
return (INT_PTR)FALSE;
以下是我从调试器获得的内容:
/*********** REMARKS ***************
/**** Fills treeview control with the contract number
/**** In treeview node's LPARAM is stored the value of the primary key
/**** Returns the number of failed attempts to load string/autonumber field
/***********************************/
int InitTreeView(HWND hDlg)
{
// error result
int iNumberOfFailedLoads = 0;
//connect to database
ADODB::_ConnectionPtr pConn("ADODB.Connection");
try
{
HRESULT hr = pConn->Open(bstrConnect, username, password,
ADODB::adConnectUnspecified);
if (!SUCCEEDED(hr))
throw _com_error(hr);
ADODB::_CommandPtr pCmd("ADODB.Command");
pCmd->ActiveConnection = pConn;
pCmd->CommandType = ADODB::adCmdText;
pCmd->CommandText = L" select ID, BrojUgovora from UGOVORI;";
ADODB::_RecordsetPtr pRS = pCmd->Execute(NULL, NULL, ADODB::adCmdText);
ADODB::Fields* pFields = NULL;
hr = pRS->get_Fields(&pFields);
if (!SUCCEEDED(hr))
throw _com_error(hr);
if (pFields && pFields->GetCount() > 0)
{
while (!pRS->AdoNSEOF)
{
// load contract values into treeview
TVINSERTSTRUCT tvis = { 0 };
tvis.item.mask = TVIF_TEXT | TVIF_PARAM;
tvis.hInsertAfter = TVI_FIRST;
tvis.item.pszText =
pRS->Fields->GetItem(L"BrojUgovora")->Value.bstrVal;
tvis.item.lParam = // here is first breakpoint
(LPARAM)(pRS->Fields->GetItem(L"ID")->Value.lVal);
// MessageBeep(0) added just so I can put breakpoint there
MessageBeep(0); // here is second breakpoint
HTREEITEM hti = TreeView_InsertItem(GetDlgItem(hDlg, IDC_TREE1),
&tvis);
if (NULL == hti)
iNumberOfFailedLoads++;
pRS->MoveNext();
}
pRS->Close();
}
pConn->Close();
}
catch (_com_error &e)
{
if (pConn->State == ADODB::adStateOpen)
pConn->Close();
iNumberOfFailedLoads = -1;
}
return iNumberOfFailedLoads;
}
当我到达第一个断点时,正确地从数据库中读取字符串。
在第二个断点,它"神奇地"更改为ID
。
更改while
循环中的代码后,一切正常:
while (!pRS->AdoNSEOF)
{
wchar_t txt[50];
memset(txt, L'\0', sizeof(txt));
swprintf_s(txt, 50, L"%s",
pRS->Fields->GetItem(L"BrojUgovora")->Value.bstrVal);
//...
tvis.item.pszText = txt;
// the rest of the code is the same
我曾尝试使用动态字符串来存储数据库中的字符串,但失败了。我得到调试断言错误。当我尝试使用wstring
时会发生同样的情况。
我应该如何重写InitTreeView
函数以避免上述错误?
答案 0 :(得分:1)
我怀疑GetItem
方法返回一个临时值,一旦函数返回就会丢失;所以pszText
最终指向的字符串已被释放/覆盖。
尝试以下操作,看看它是否有所作为:
// load contract values into treeview
_variant_t varText = pRS->Fields->GetItem(L"BrojUgovora")->Value;
_variant_t varID = pRS->Fields->GetItem(L"ID")->Value;
TVINSERTSTRUCT tvis = { 0 };
tvis.item.mask = TVIF_TEXT | TVIF_PARAM;
tvis.hInsertAfter = TVI_FIRST;
tvis.item.pszText = varText.bstrVal;
tvis.item.lParam = varID.lVal;