学数据库,我先学odbc,我在CRecordset上建立个类,然后建立对象,启动默认连接,设了一个数据源Student Register,在菜单上加了一项Do(ID_DO)
函数如下:
void CODBCDemoView::OnDo()
{
CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
ctrlList.DeleteAllItems();
while(ctrlList.DeleteColumn(0));
UpdateWindow(); CString strSQL;
strSQL = _T("SELECT * FROM SalesByProduct;"); if(!ShowInformation(strSQL)) AfxMessageBox("数据获取失败!");
}
BOOL CODBCDemoView::ShowInformation(CString strSQL)
{
CRect rect;
CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
ctrlList.GetWindowRect(rect); try{
// get recordset field information
BeginWaitCursor();
if(m_pCommonRS->IsOpen()) m_pCommonRS->Close();
m_pCommonRS->Open(CRecordset::dynaset, strSQL);
if(!m_pCommonRS->IsEOF()){
m_pCommonRS->MoveLast();
m_pCommonRS->MoveFirst();
}
int nFieldCount = m_pCommonRS->GetODBCFieldCount();
CODBCFieldInfo fieldinfo;
for(int n=0;n<nFieldCount;n++){
m_pCommonRS->GetODBCFieldInfo(n, fieldinfo);
int nWidth = ctrlList.GetStringWidth(fieldinfo.m_strName) + 15;
ctrlList.InsertColumn(n, fieldinfo.m_strName, LVCFMT_LEFT, nWidth);
}
// get recordset data information
CString strValue;
m_pCommonRS->MoveFirst();
int nCount = 0; while(!m_pCommonRS->IsEOF()){
ctrlList.InsertItem(nCount, strValue);
for(int j=0;j<nFieldCount;j++){
m_pCommonRS->GetFieldValue(j, strValue);
ctrlList.SetItemText(nCount, j, strValue);
}
m_pCommonRS->MoveNext();
nCount ++;
}
EndWaitCursor();
} catch(CDBException *e){
e->ReportError();
EndWaitCursor();
return FALSE;
} return TRUE;
}
但编译过后却报:“数据被截断”;
“数据获取失败”;
???
函数如下:
void CODBCDemoView::OnDo()
{
CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
ctrlList.DeleteAllItems();
while(ctrlList.DeleteColumn(0));
UpdateWindow(); CString strSQL;
strSQL = _T("SELECT * FROM SalesByProduct;"); if(!ShowInformation(strSQL)) AfxMessageBox("数据获取失败!");
}
BOOL CODBCDemoView::ShowInformation(CString strSQL)
{
CRect rect;
CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
ctrlList.GetWindowRect(rect); try{
// get recordset field information
BeginWaitCursor();
if(m_pCommonRS->IsOpen()) m_pCommonRS->Close();
m_pCommonRS->Open(CRecordset::dynaset, strSQL);
if(!m_pCommonRS->IsEOF()){
m_pCommonRS->MoveLast();
m_pCommonRS->MoveFirst();
}
int nFieldCount = m_pCommonRS->GetODBCFieldCount();
CODBCFieldInfo fieldinfo;
for(int n=0;n<nFieldCount;n++){
m_pCommonRS->GetODBCFieldInfo(n, fieldinfo);
int nWidth = ctrlList.GetStringWidth(fieldinfo.m_strName) + 15;
ctrlList.InsertColumn(n, fieldinfo.m_strName, LVCFMT_LEFT, nWidth);
}
// get recordset data information
CString strValue;
m_pCommonRS->MoveFirst();
int nCount = 0; while(!m_pCommonRS->IsEOF()){
ctrlList.InsertItem(nCount, strValue);
for(int j=0;j<nFieldCount;j++){
m_pCommonRS->GetFieldValue(j, strValue);
ctrlList.SetItemText(nCount, j, strValue);
}
m_pCommonRS->MoveNext();
nCount ++;
}
EndWaitCursor();
} catch(CDBException *e){
e->ReportError();
EndWaitCursor();
return FALSE;
} return TRUE;
}
但编译过后却报:“数据被截断”;
“数据获取失败”;
???
解决方案 »
- 用ini文件指示table,field名,要求ini文件可以改变,修改table,增加field。用vc6.0生成根据ini文件的动态配置数据库工具。
- 关于CString数组的赋值问题
- 如何把内存中的内容虚拟成一个文件,而不用实际的磁盘IO读写?
- CreateDIBitmap()函数成功返回以后,CreateDIBitmap()的第4个参数难道可以释放掉?很奇怪?如下:
- 我的客户端机强行关闭后,服务器怎么不响应OnClose呢,关闭客户端软件就会有响应,为什么?
- 请教vc中数据库的问题
- 在vc中如何实现类似vb的sendkeys?
- 相同的ANSI C 在linux 下用了8s 在 WINDOWS 下用了14s
- 小小的问题
- 如何在工程中加入*.rc资源文件?
- 这个错误怎么解决?
- _variant_t类型转换
m_pCommonRS->MoveFirst();
int nCount = 0;while(!m_pCommonRS->IsEOF()){
ctrlList.InsertItem(nCount, strValue);问题1,这时候strValue还有没值。
for(int j=0;j<nFieldCount;j++)
{
m_pCommonRS->GetFieldValue(j, strValue);
ctrlList.SetItemText(nCount, j, strValue);
}
问题2:ctrlList.InsertItem(nCount, strValue);插入第一列。for(int j=0;j<nFieldCount;j++)插入了nFieldCount列。而你InsertColumn的时候,一共也只有nFieldCount列。你却想插入nFieldCount+1列,所以出错,数据被截断。
while(!m_pCommonRS->IsEOF())
{
m_pCommonRS->GetFieldValue(short(0), strValue);
ctrlList.InsertItem(nCount, strValue);问题1,这时候strValue还有没值。
for(int j=1;j<nFieldCount;j++)
{
m_pCommonRS->GetFieldValue(j, strValue);
ctrlList.SetItemText(nCount, j, strValue);
}
void CODBCDemoView::OnInitialUpdate()
{
CListView::OnInitialUpdate(); CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
ctrlList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
// TODO: You may populate your ListView with items by directly accessing
// its list control through a call to GetListCtrl().
m_pCommonRS = new CCommonRs;
}
我觉得应该没问题,这是一本书上说的,而且还有例子,但他的例子比这复杂(也就是我这是那的一部分),但例子却没有一点问题。
主要是这个函数,请看:
void RFX_Text( CFieldExchange* pFX, const char* szName, CString& value, int nMaxLength = 255, int nColumnType = SQL_VARCHAR, short nScale = 0 );注意int nMaxLength = 255如果你不指定长度的话就只有255所以超过255就会出错,使用这个函数时,指定长度,就不会出错了.