急急急,如何用vc++设计一个操作数据库的类 如何用vc++设计一个操作数据库的类主要实现对数据库表的增加删除修改查询功能并且能够提供连接数据库的方法和关闭连接方法 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在vckbase上有一个例子,有人封装过了.成真 try { if(!m_db.IsOpen()) m_db.OpenEx(_T("DSN=Client;UID=;PWD="),CDatabase::noOdbcDialog ); m_recentRS.m_pDatabase =&m_db; CString SQLstr; SQLstr=_T("SELECT TOP "); char NumStr[5]; itoa(nNumber,NumStr,10); SQLstr+=NumStr; SQLstr+=_T(" * FROM CallDetail order by Time DESC"); m_recentRS.Open(CRecordset::dynaset,SQLstr); } catch(CException *e) { //如果出错,把出错码传递到ErMsg中. char msg[128]; e->GetErrorMessage(msg,128,NULL); ErMsg=msg; } 用ADO吧// ADOConn.h: interface for the ADOConn class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_ADOCONN_H__2BB5D39D_2112_4008_B032_40397B5CABE2__INCLUDED_)#define AFX_ADOCONN_H__2BB5D39D_2112_4008_B032_40397B5CABE2__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000//要在程序中使用ADO对象,需引入ADO库文件#import "c:\program files\common files\system\ado\msado15.dll"no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF")class ADOConn {public: //添加一个指向Connection对象的指针 _ConnectionPtr m_pConnection; //添加一个指向Recordset对象的指针 _RecordsetPtr m_pRecordset;public: ADOConn(); virtual ~ADOConn(); //初始化--连接数据库 void OnInitADOConn(); //执行查询 _RecordsetPtr& GetRecordSet(_bstr_t bstrSQL); //执行除查询以外的SQL语句 BOOL ExecuteSQL(_bstr_t bstrSQL); //断开与数据库的连接 void ExitConnect();};#endif // !defined(AFX_ADOCONN_H__2BB5D39D_2112_4008_B032_40397B5CABE2__INCLUDED_)// ADOConn.cpp: implementation of the ADOConn class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "RsManager.h"#include "ADOConn.h"#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW#endif//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////ADOConn::ADOConn(){}ADOConn::~ADOConn(){}void ADOConn::OnInitADOConn(){ //初始化OLE/COM库环境 ::CoInitialize(NULL); try { //创建Connection对象 m_pConnection.CreateInstance(__uuidof(Connection)); //打开到数据源的连接 m_pConnection->Open("driver={SQL Server};Server=127.0.0.1;DATABASE=RsManager;UID=g;PWD=g","","",adModeUnknown); } catch(_com_error e) { AfxMessageBox(e.Description()); // AfxMessageBox("ADOConn::OnInitADOConn()错误"); }}//执行select语句,bstrSQL表示要执行的SQL语句_RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL){ try { if(m_pConnection==NULL) OnInitADOConn(); m_pRecordset.CreateInstance(_uuidof(Recordset)); m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); } catch(_com_error e) { AfxMessageBox(e.Description()); } return m_pRecordset;}//执行select以外的其它SQL语句BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL){ try { if(m_pConnection==NULL) OnInitADOConn(); m_pConnection->Execute(bstrSQL,NULL,adCmdText); return true; } catch(_com_error e) { AfxMessageBox(e.Description()); return false; }}void ADOConn::ExitConnect(){ //关闭记录集和连接 if(m_pRecordset!=NULL) m_pRecordset->Close(); m_pConnection->Close(); //释放环境 ::CoUninitialize();}然后再创建个类操作表// Employees.h: interface for the CEmployees class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_EMPLOYEES_H__D5008B3A_CD40_4E64_86C3_E69DC8EBE49E__INCLUDED_)#define AFX_EMPLOYEES_H__D5008B3A_CD40_4E64_86C3_E69DC8EBE49E__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000//为表CEmployees添加类class CEmployees {public: void SetEmp_Depid(int vDepid); int GetEmp_Depid(); void SetEmp_Sex(CString vEmpsex); CString GetEmp_Sex(); void SetEmp_Name(CString vEmpname); CString GetEmp_Name(); void SetEmp_Id(int vEmpid); int GetEmp_Id(); CEmployees(); virtual ~CEmployees();private: int Emp_Id; CString Emp_Name; CString Emp_Sex; int Emp_Depid;};#endif // !defined(AFX_EMPLOYEES_H__D5008B3A_CD40_4E64_86C3_E69DC8EBE49E__INCLUDED_) 冰天雪地,裸体跪求 DX 高手指点。 HELP !! 我的VC++程序里面 断点调试 后,总进入一个汇编调试界面 ??? 如何雅致地得知某一进程是关键(系统)进程?又如何终止关键(系统)进程呢?高分求解!!! 关于CListCtr和SetTimer的问题,很急!!! VC中的CDataGrid用法 大家都有活干吗? 关于写代码把打印设置里打印方向默认设为“横向”??? 帮忙查一下MSDN, 我的没有装全(盘老是读不出文件) 求MFC实现打印功能的代码 帮忙详解下面代码! 大家看看我的这个DSN有什么不对 vC中对象不出下拉菜单
{
if(!m_db.IsOpen())
m_db.OpenEx(_T("DSN=Client;UID=;PWD="),CDatabase::noOdbcDialog );
m_recentRS.m_pDatabase =&m_db;
CString SQLstr;
SQLstr=_T("SELECT TOP ");
char NumStr[5];
itoa(nNumber,NumStr,10);
SQLstr+=NumStr;
SQLstr+=_T(" * FROM CallDetail order by Time DESC");
m_recentRS.Open(CRecordset::dynaset,SQLstr);
}
catch(CException *e)
{
//如果出错,把出错码传递到ErMsg中.
char msg[128];
e->GetErrorMessage(msg,128,NULL);
ErMsg=msg;
}
// ADOConn.h: interface for the ADOConn class.
//
//////////////////////////////////////////////////////////////////////#if !defined(AFX_ADOCONN_H__2BB5D39D_2112_4008_B032_40397B5CABE2__INCLUDED_)
#define AFX_ADOCONN_H__2BB5D39D_2112_4008_B032_40397B5CABE2__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
//要在程序中使用ADO对象,需引入ADO库文件
#import "c:\program files\common files\system\ado\msado15.dll"no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF")
class ADOConn
{
public:
//添加一个指向Connection对象的指针
_ConnectionPtr m_pConnection;
//添加一个指向Recordset对象的指针
_RecordsetPtr m_pRecordset;
public:
ADOConn();
virtual ~ADOConn(); //初始化--连接数据库
void OnInitADOConn();
//执行查询
_RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);
//执行除查询以外的SQL语句
BOOL ExecuteSQL(_bstr_t bstrSQL);
//断开与数据库的连接
void ExitConnect();
};#endif // !defined(AFX_ADOCONN_H__2BB5D39D_2112_4008_B032_40397B5CABE2__INCLUDED_)// ADOConn.cpp: implementation of the ADOConn class.
//
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "RsManager.h"
#include "ADOConn.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////ADOConn::ADOConn()
{}ADOConn::~ADOConn()
{}void ADOConn::OnInitADOConn()
{
//初始化OLE/COM库环境
::CoInitialize(NULL);
try
{
//创建Connection对象
m_pConnection.CreateInstance(__uuidof(Connection));
//打开到数据源的连接
m_pConnection->Open("driver={SQL Server};Server=127.0.0.1;DATABASE=RsManager;UID=g;PWD=g","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
// AfxMessageBox("ADOConn::OnInitADOConn()错误");
}
}//执行select语句,bstrSQL表示要执行的SQL语句
_RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL)
{
try
{
if(m_pConnection==NULL)
OnInitADOConn();
m_pRecordset.CreateInstance(_uuidof(Recordset));
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
return m_pRecordset;
}//执行select以外的其它SQL语句
BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
try
{
if(m_pConnection==NULL)
OnInitADOConn();
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
return true;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
}void ADOConn::ExitConnect()
{ //关闭记录集和连接
if(m_pRecordset!=NULL)
m_pRecordset->Close();
m_pConnection->Close();
//释放环境
::CoUninitialize();
}
然后再创建个类操作表
// Employees.h: interface for the CEmployees class.
//
//////////////////////////////////////////////////////////////////////#if !defined(AFX_EMPLOYEES_H__D5008B3A_CD40_4E64_86C3_E69DC8EBE49E__INCLUDED_)
#define AFX_EMPLOYEES_H__D5008B3A_CD40_4E64_86C3_E69DC8EBE49E__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
//为表CEmployees添加类
class CEmployees
{
public:
void SetEmp_Depid(int vDepid);
int GetEmp_Depid();
void SetEmp_Sex(CString vEmpsex);
CString GetEmp_Sex();
void SetEmp_Name(CString vEmpname);
CString GetEmp_Name();
void SetEmp_Id(int vEmpid);
int GetEmp_Id(); CEmployees();
virtual ~CEmployees();
private:
int Emp_Id;
CString Emp_Name;
CString Emp_Sex;
int Emp_Depid;
};#endif // !defined(AFX_EMPLOYEES_H__D5008B3A_CD40_4E64_86C3_E69DC8EBE49E__INCLUDED_)