首页上一页 1 下一页尾页 2 条记录 1/1页
VC数据库开发完全手册固定资产设备管理系统资产类别
发表在C语言图书答疑
2010-12-18
是否精华
是
否
版块置顶:
是
否
如何处理VC datagrid 控件的单击事件并把datagrid 表格中的数据显示在对话框的文本框中。下面是我用一个封装的ADO类去访问数据库,并单击datagrid 控件时,出现Runtime Error对话框,调试时,我把断点设置在
if ((!theApp.datamanage->GetRecordset()->ADOEOF)&&(!theApp.datamanage->GetRecordset()->BOF))此代码处,按F5调试运行,单击datagrid 控件运行停留在断点处,当按F10单步执行代码时,出现Unhandled exception in Equipment.exe: 0xC0000005:Access Violation 对话框。请高手赐教。msado15.dll己导入。数据库已正确连接上.怎样修改断点处代码.
关键代码如下:
// DataManage.h: interface for the CDataManage class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_DATAMANAGE_H__EE6DEDE3_191B_451C_940E_7AC519174199__INCLUDED_)
#define AFX_DATAMANAGE_H__EE6DEDE3_191B_451C_940E_7AC519174199__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CDataManage
{
public:
CDataManage();
virtual ~CDataManage();
public:
_RecordsetPtr GetRecordset();
bool ExecSql(CString sql);
bool ConnectDatabase(CString constr);
_ConnectionPtr GetConn();
_RecordsetPtr Record1;
public:
_ConnectionPtr DataConn;
_RecordsetPtr DataRecord;
};
#endif // !defined(AFX_DATAMANAGE_H__EE6DEDE3_191B_451C_940E_7AC519174199__INCLUDED_)
// DataManage.cpp: implementation of the CDataManage class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Equipment.h"
#include "DataManage.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDataManage::CDataManage()
{
DataConn.CreateInstance(_uuidof(Connection));
DataRecord.CreateInstance(_uuidof(Recordset));
Record1.CreateInstance(_uuidof(Recordset));
}
CDataManage::~CDataManage()
{
DataRecord->Close();
Record1->Close();
DataConn->Close();
DataRecord.Release();
Record1.Release();
DataConn.Release();
}
_ConnectionPtr CDataManage::GetConn()
{
return DataConn;
}
bool CDataManage::ConnectDatabase(CString constr)
{
DataConn->ConnectionString=_bstr_t (constr);
try
{
DataConn->CursorLocation=adUseClient;
DataConn->Open(" "," ", " ",-1);
}
catch(...)
{
return false;
}
return true;
}
bool CDataManage::ExecSql(CString sql)
{
_bstr_t sqltext=sql;
try{
DataConn->Execute(sqltext,NULL,adCmdText);
}
catch(_com_error&e){
AfxMessageBox(e.ErrorMessage(),0,0);
return false;
}
return true;
}
_RecordsetPtr CDataManage::GetRecordset()
{
return (DataRecord);
}
BOOL CEquipmentApp::InitInstance()
{
AfxEnableControlContainer();
// ::CoInitialize(NULL);
AfxOleInit();
datamanage = new CDataManage();
datamanage->ConnectDatabase("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=EquipmentManage");
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CEquipmentDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return TRUE;
}
void CEquipKind::OnClickDatagrid()
{
// TODO: Add your control notification handler code here
CString str;
_variant_t temp;
if ((!theApp.datamanage->GetRecordset()->ADOEOF)&&(!theApp.datamanage->GetRecordset()->BOF))
{
temp = theApp.datamanage->GetRecordset()->GetFields()->GetItem((long)0)->Value;
str =temp.bstrVal;
kind.SetWindowText(str);
}
}
if ((!theApp.datamanage->GetRecordset()->ADOEOF)&&(!theApp.datamanage->GetRecordset()->BOF))此代码处,按F5调试运行,单击datagrid 控件运行停留在断点处,当按F10单步执行代码时,出现Unhandled exception in Equipment.exe: 0xC0000005:Access Violation 对话框。请高手赐教。msado15.dll己导入。数据库已正确连接上.怎样修改断点处代码.
关键代码如下:
// DataManage.h: interface for the CDataManage class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_DATAMANAGE_H__EE6DEDE3_191B_451C_940E_7AC519174199__INCLUDED_)
#define AFX_DATAMANAGE_H__EE6DEDE3_191B_451C_940E_7AC519174199__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CDataManage
{
public:
CDataManage();
virtual ~CDataManage();
public:
_RecordsetPtr GetRecordset();
bool ExecSql(CString sql);
bool ConnectDatabase(CString constr);
_ConnectionPtr GetConn();
_RecordsetPtr Record1;
public:
_ConnectionPtr DataConn;
_RecordsetPtr DataRecord;
};
#endif // !defined(AFX_DATAMANAGE_H__EE6DEDE3_191B_451C_940E_7AC519174199__INCLUDED_)
// DataManage.cpp: implementation of the CDataManage class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Equipment.h"
#include "DataManage.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDataManage::CDataManage()
{
DataConn.CreateInstance(_uuidof(Connection));
DataRecord.CreateInstance(_uuidof(Recordset));
Record1.CreateInstance(_uuidof(Recordset));
}
CDataManage::~CDataManage()
{
DataRecord->Close();
Record1->Close();
DataConn->Close();
DataRecord.Release();
Record1.Release();
DataConn.Release();
}
_ConnectionPtr CDataManage::GetConn()
{
return DataConn;
}
bool CDataManage::ConnectDatabase(CString constr)
{
DataConn->ConnectionString=_bstr_t (constr);
try
{
DataConn->CursorLocation=adUseClient;
DataConn->Open(" "," ", " ",-1);
}
catch(...)
{
return false;
}
return true;
}
bool CDataManage::ExecSql(CString sql)
{
_bstr_t sqltext=sql;
try{
DataConn->Execute(sqltext,NULL,adCmdText);
}
catch(_com_error&e){
AfxMessageBox(e.ErrorMessage(),0,0);
return false;
}
return true;
}
_RecordsetPtr CDataManage::GetRecordset()
{
return (DataRecord);
}
BOOL CEquipmentApp::InitInstance()
{
AfxEnableControlContainer();
// ::CoInitialize(NULL);
AfxOleInit();
datamanage = new CDataManage();
datamanage->ConnectDatabase("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=EquipmentManage");
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CEquipmentDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return TRUE;
}
void CEquipKind::OnClickDatagrid()
{
// TODO: Add your control notification handler code here
CString str;
_variant_t temp;
if ((!theApp.datamanage->GetRecordset()->ADOEOF)&&(!theApp.datamanage->GetRecordset()->BOF))
{
temp = theApp.datamanage->GetRecordset()->GetFields()->GetItem((long)0)->Value;
str =temp.bstrVal;
kind.SetWindowText(str);
}
}