📄 db.cpp
字号:
// DB.cpp: implementation of the CDB class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "ado2.h"
#include "DB.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern _ConnectionPtr g_pConnection;
extern _RecordsetPtr g_pRecordset;
CDB::CDB()
{
}
CDB::~CDB()
{
}
////////////////////////////////////////////////////////////////////////////////
//连接数据库函数
//可以连接access,sqlserver和oracle三种数据库
//创建时间:2003.12.1
//修改时间:
////////////////////////////////////////////////////////////////////////////////
BOOL CDB::ConnectDataBase(int DBtype,CString servername,CString tablename ,
CString username ,CString password )
{
HRESULT hr;//创建数据库变量
try
{
hr = g_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
// hr = g_pConnection.CreateInstance(__uuidof(Connection));///创建Connection对象
g_pRecordset.CreateInstance(__uuidof(Recordset)); ///建立记录集
if(SUCCEEDED(hr))
{
switch (DBtype)//所连接的数据库类型
{
case 1://access
if(g_pConnection->Open(_bstr_t("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+servername),
"","",adModeUnknown)!=S_OK)
{
if(g_pConnection->Open(_bstr_t("Provider=Microsoft.Jet.OLEDB.3.51;Data Source="+servername),
"","",adModeUnknown)!=S_OK)
{
return false;
}
}
//hr = m_pConnection->Open(_bstr_t("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+dlg.GetFileName()),//hole.mdb",
//"","",adModeUnknown);///连接数据库
///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要
//改为:Provider=Microsoft.Jet.OLEDB.3.51;
break;
case 2://sqlserver
if(g_pConnection->Open(_bstr_t("Provider=SQLOLEDB;Integrated security=SSPI;Initial Catalog=" +servername+";Data Source="+tablename),
"","",adModeUnknown)!=S_OK)
{
return false;
}
break;
case 3://oracle
break;
default://其他模式
break;
}
}
}
catch(_com_error e)///捕捉异常
{
return false;
}
return true;
}
//////////////////////////////////////////////////
//关闭数据库
//2003.12.1
//////////////////////////////////////////////////
void CDB::CloseDataBase()
{
if (g_pConnection != NULL)
{
g_pConnection->Close();
}
else
{
::AfxMessageBox("不存在要关闭的数据库!");
}
}
void CDB::Query(CString strSql)
{
if (g_pConnection != NULL)
{
g_pRecordset->Open((_variant_t)strSql,g_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
else
{
::AfxMessageBox("请先打开数据库");
}
}
BOOL CDB::ConnectDB()
{
////////////连接数据库//////////////
HRESULT hr;
try
{
hr = g_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
g_pRecordset.CreateInstance("ADODB.Recordset");
if(SUCCEEDED(hr))
{
hr = g_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=地面点.mdb","","",adModeUnknown);///连接数据库
///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51; }
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return false;
}
return true;
}
void CDB::Query()
{
if(g_pConnection!=NULL)
{
g_pRecordset->Open("SELECT * FROM surfacePoint ORDER BY id ASC ",_variant_t((IDispatch*)g_pConnection,true),
adOpenStatic,adLockOptimistic,adCmdText);
}
else
{
::AfxMessageBox("请先打开数据库");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -