📄 ado_sql.cpp
字号:
#include "Stdafx.h"
#include "ado_sql.h"
#include "stdafx.h"
_ConnectionPtr ado_sql::pConnection=NULL;
ado_sql::ado_sql(void)
{
}
ado_sql::~ado_sql(void)
{
}
bool ado_sql::InitAccess(CString strDB)
{
/*HRESULT hr; */
/*hr=*/pConnection.CreateInstance("ADODB.Connection");
/*if(SUCCEEDED(hr))
AfxMessageBox(_T("fddf"));*/
CString strCn;
strCn.Empty();
strCn=_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=")+strDB;
/*_variant_t bcnstr=_variant_t(strCn); */
//打开一个连接
try {
pConnection->Open(_bstr_t(strCn),"", "", adModeUnknown);
//adOpenUnspecified
}
catch(_com_error e) {
CString errormessage;
errormessage.Format(_T("Warning: 连接数据库发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
AfxMessageBox(errormessage); //
return false;
}
catch(...) {
AfxMessageBox(_T("Warning :连接数据库时发生未知错误"));
return false;
}
return true;
}
bool ado_sql::InitSQLServer(CString server, CString db, CString UserName, CString Pwd)
{
/*HRESULT hr; */
/*hr=*/pConnection.CreateInstance("ADODB.Connection");
/*if(SUCCEEDED(hr))
AfxMessageBox(_T("fddf"));*/
CString strCn;
strCn.Empty();
strCn=_T("provider=SQLOLEDB;data source=")+server
+_T(";initial catalog=")+db
+_T(";userID=")+UserName
+_T(";Password=")+Pwd;
_variant_t bcnstr=_variant_t(strCn);
_variant_t bunstr=_variant_t(UserName);
_variant_t bpwdstr=_variant_t(Pwd);
//打开一个连接
try {
pConnection->Open(_bstr_t(bcnstr),_bstr_t(bunstr),
_bstr_t(bpwdstr),-1);//adOpenUnspecified
}
catch(_com_error e) {
CString errormessage;
errormessage.Format(_T("Warning: 连接数据库发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
AfxMessageBox(errormessage); //
return false;
}
catch(...) {
AfxMessageBox(_T("Warning :连接数据库时发生未知错误"));
return false;
}
return true;
}
void ado_sql::ExitDB()
{
if(pConnection!=NULL)
{
pConnection->Close();//关闭连接
pConnection.Release();//释放对象
}
}
bool ado_sql::OpenRecordset(CString &strSQL)
{
RdSetOperate.Recordset.CreateInstance(__uuidof(Recordset));
try
{
RdSetOperate.Recordset->Open(strSQL.AllocSysString(),pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error e)
{
CString strError;
strError.Format(_T("警告:打开数据表时发生异常,错误信息:%s"),e.ErrorMessage());
/*AfxMessageBox(strError);*/
return false;
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -