急救
发表在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);
}


}
分享到:
精彩评论 2
大麦571
学分:1 LV1
2010-12-21
沙发
您好:
请问您这个是哪本书中的问题。
详细说明一下,具体的章节例子号,方便我们查找!
jxcbf
学分:0 LV1
TA的每日心情
伤心
2023-03-21 16:59:14
2010-12-22
板凳
VC数据库系统开发完全手册资产设备管理系统资产类别
首页上一页 1 下一页尾页 2 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照