mfc建立工程,view派生与clistview,后新建crecordset派生类,使用odbc数据源,想将数据库中数据显示在view下,怎么就是不行,这样写的:(好象就是open有问题,高手指教!)
void COdbc2View::OnShow()
{
CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
ctrlList.DeleteAllItems();
while(ctrlList.DeleteColumn(0));
UpdateWindow(); CString strSQL(_T("select * from stud"));
if(!ShowInformation(strSQL))
AfxMessageBox("数据获取失败!");}BOOL COdbc2View::ShowInformation(CString strSQL)
{
CRect rect;
CListCtrl& ctrlList=(CListCtrl&) GetListCtrl();
ctrlList.GetWindowRect(rect); try
{
BeginWaitCursor();
// 如果结果集已被打开,则关闭它
if(m_pRS->IsOpen()) m_pRS->Close();
// 打开结果集
m_pRS->Open(CRecordset::dynaset, strSQL);
if(!m_pRS->IsEOF())
{
m_pRS->MoveLast();
m_pRS->MoveFirst();
}
// 取得结果集的字段个数
int nFieldCount = m_pRS->GetODBCFieldCount();
CODBCFieldInfo fieldinfo;
// 读取字段信息
for(int n=0;n<nFieldCount;n++)
{
m_pRS->GetODBCFieldInfo(n, fieldinfo);
int nWidth = ctrlList.GetStringWidth(fieldinfo.m_strName) + 15;
ctrlList.InsertColumn(n, fieldinfo.m_strName,
LVCFMT_LEFT, nWidth);
}
// 读取记录信息
CString strValue;
m_pRS->MoveFirst();
int nCount = 0;
while(!m_pRS->IsEOF())
{
ctrlList.InsertItem(nCount, strValue);
for(int j=0;j<nFieldCount;j++)
{
m_pRS->GetFieldValue(j, strValue);
ctrlList.SetItemText(nCount, j, strValue);
}
m_pRS->MoveNext();
nCount ++;
}
EndWaitCursor();
}
catch(CDBException *e)
{
e->ReportError();
EndWaitCursor();
return FALSE;
}
return TRUE;
}
void COdbc2View::OnShow()
{
CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
ctrlList.DeleteAllItems();
while(ctrlList.DeleteColumn(0));
UpdateWindow(); CString strSQL(_T("select * from stud"));
if(!ShowInformation(strSQL))
AfxMessageBox("数据获取失败!");}BOOL COdbc2View::ShowInformation(CString strSQL)
{
CRect rect;
CListCtrl& ctrlList=(CListCtrl&) GetListCtrl();
ctrlList.GetWindowRect(rect); try
{
BeginWaitCursor();
// 如果结果集已被打开,则关闭它
if(m_pRS->IsOpen()) m_pRS->Close();
// 打开结果集
m_pRS->Open(CRecordset::dynaset, strSQL);
if(!m_pRS->IsEOF())
{
m_pRS->MoveLast();
m_pRS->MoveFirst();
}
// 取得结果集的字段个数
int nFieldCount = m_pRS->GetODBCFieldCount();
CODBCFieldInfo fieldinfo;
// 读取字段信息
for(int n=0;n<nFieldCount;n++)
{
m_pRS->GetODBCFieldInfo(n, fieldinfo);
int nWidth = ctrlList.GetStringWidth(fieldinfo.m_strName) + 15;
ctrlList.InsertColumn(n, fieldinfo.m_strName,
LVCFMT_LEFT, nWidth);
}
// 读取记录信息
CString strValue;
m_pRS->MoveFirst();
int nCount = 0;
while(!m_pRS->IsEOF())
{
ctrlList.InsertItem(nCount, strValue);
for(int j=0;j<nFieldCount;j++)
{
m_pRS->GetFieldValue(j, strValue);
ctrlList.SetItemText(nCount, j, strValue);
}
m_pRS->MoveNext();
nCount ++;
}
EndWaitCursor();
}
catch(CDBException *e)
{
e->ReportError();
EndWaitCursor();
return FALSE;
}
return TRUE;
}
CDBVariant varValue;
if(m_basicset.GetRecordCount()!=0) m_basicset.MoveFirst();
char buf[20];
int i=0;
while(!m_basicset.IsEOF())
{
int temp=0;
m_basicset.GetFieldValue(temp,varValue);
sprintf(buf,"%d",varValue.m_lVal); m_basic.InsertItem(i,buf);
m_basicset.GetFieldValue(1,varValue);
m_basic.SetItemText(i,1,varValue.m_pstring->GetBuffer(1));
http://blog.csdn.net/bluebohe/archive/2004/04/05/21072.aspx
if(!m_pRS->IsEOF())
{
m_pRS->MoveLast();
m_pRS->MoveFirst();
}
可能是个死循环。
如果每个数据表都映射了相应的CRecordSet类当然可以,不过看楼主的代码好像是没有进行映射和数据绑定。
CDatabase db;
db.OpenEx(_T("DSN=ODBCName; UID=***; PWD=***;"));
pset = new CRecordset(db);
m_sql.Format("select * from tablename");
pset->Open(CRecordset::snapshot,m_sql);
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
// 对记录集的操作
CDatabase *pDb;
pDb = new CDatabase;
CRecordset* dbset;
dbset=new CRecordset(pDb);//
TRY
{
pDb->OpenEx("Driver={SQL Server};Server=SATURN;Network=dbmssocn;Database=telstar;UID=***;PWD=***");
if(!pDb->IsOpen ())
AfxMessageBox("Open dsnName unsuccess.");//m_databaseinfo);
else
{
CString strSQL=_T("select * from TABLE1");
dbset->Open(CRecordset::dynaset,strSQL,CRecordset::readOnly);
//统计table信息
short FieldCount=dbset->GetODBCFieldCount ();
do { dbset->MoveNext(); }
while (!dbset->IsEOF());
long RecordCount=dbset->GetRecordCount(); CString title[MAX_PATH]={""};
CODBCFieldInfo fieldinfo;
CStringArray strColTitle;
if(!dbset->IsBOF()) dbset->MoveFirst();
if(strColTitle.GetSize()) strColTitle.RemoveAll(); //显示列标题(字段名)
for(int i=0;i<FieldCount;i++)
{
dbset->GetODBCFieldInfo(i,fieldinfo);
strColTitle.Add(LPCTSTR(fieldinfo.m_strName));
m_showlist.InsertColumn(i,strColTitle[i]);//title[i]);
} CString item[MAX_PATH];
CString strField; //显示记录
dbset->MoveFirst();
for(i=0;i<RecordCount;i++)
{
m_showlist.InsertItem(i,"");
for(int j=0;j<FieldCount;j++)
{
dbset->GetFieldValue (j,strField);
m_showlist.SetItemText (i,j,strField);
m_showlist.SetColumnWidth (j,LVSCW_AUTOSIZE);
}
dbset->MoveNext ();
}
dbset->Close ();
delete dbset; pDb->Close();
}
}
CATCH(CDBException, e)
{ // The error code is in e->m_nRetCode
dbset->Close ();
delete dbset; pDb->Close();
CString strTip=_T("Database operate Fail!\n");
strTip+=_T("Because:\n")+e->m_strError;
AfxMessageBox(strTip);
}
END_CATCH
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
if(m_basicset.GetRecordCount()!=0) m_basicset.MoveFirst();
char buf[20];
int i=0;
while(!m_basicset.IsEOF())
{
int temp=0;
m_basicset.GetFieldValue(temp,varValue);
sprintf(buf,"%d",varValue.m_lVal); m_basic.InsertItem(i,buf);
m_basicset.GetFieldValue(1,varValue);
m_basic.SetItemText(i,1,varValue.m_pstring->GetBuffer(1));
CDatabase m_db;
m_db.OpenEx (TEXT("DSN=ODBCName;"),FALSE);
CRcordset m_rs(&m_db);
m_rs.Open(CRecordset::dynaset,strSQL);
记得把m_pRS->换成m_rs.
另外,ShowInformation()有个小BUG,在CRecordset返回空记录集时会出错。是在
// 读取记录信息
CString strValue;
m_pRS->MoveFirst(); //这里,对空记录集进行移动到最前,会产生一个超越数据区错误。将这句改为if (!m_pRS.IsBOF() && !m_pRS.IsEOF()) m_RS->MoveFirst();