📄 sqlaccess.cpp
字号:
// SQLAccess.cpp: implementation of the CSQLAccess class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SQLAccess.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//#include <comdef.h>
//#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","rsEOF")//导入ado动态连接库
CSQLAccess::CSQLAccess()
{
CoInitialize(NULL);
m_pCon=NULL;
m_pRs=NULL;
m_pCon=new _ConnectionPtr(__uuidof(Connection));//初始化对象
m_pCon->ConnectionString="File Name=E:\\mydata.udl";
if(m_pCon->Open("","","",adConnectUnspecified)!=0)//adConnectUnspecified
AfxMessageBox("数据库连接失败!");
}
CSQLAccess::CSQLAccess(CString DatabaseName,CString uid,CString pwd,CString Provider,CString HostName)
{
CoInitialize(NULL);
m_pCon=NULL;
m_pRs=NULL;
m_pCon=new _ConnectionPtr(__uuidof(Connection));//初始化对象
this->Provider=Provider;//所用的数据库的类型
this->Database=DatabaseName;//数据库名称
this->uid=uid; //用户名称
this->pwd=pwd; //用户密码
this->HostName=HostName; //数据库所在主机的ip
m_pCon->ConnectionString=(_bstr_t)("Provider=" + this->Provider + ";Password=" +this->pwd + ";Persist Security Info=True;User ID="
+this->uid + ";Initial Catalog=" + this->Database + ";Data Source=" +this->HostName);
HRESULT hr=S_OK;//s_ok=0 默认返回值
hr=m_pCon->Open("","","",NULL);
if(hr!=S_OK)
AfxMessageBox("数据库连接失败!");
else
AfxMessageBox("数据库连接成功!");
/*
Public Const HOSTNAME = "127.0.0.1" '主机IP地址
'Public Const HOSTNAME = "192.168.1.101" '主机IP地址
Public Const DBName = "UFDATA_001_2008" '"zhmisdb" ' '数据库名称
Public Const SYSDBName = "UFSystem" '"UFSystem" ' '系统数据库名称
Public Const UName = "sa" '用户名称
Public Const PWD = "ronger" '用户密码
Public Const msDSN = "Provider=SQLOLEDB.1;Password=" & PWD &
";Persist Security Info=True;User ID=" & UName & ";Initial Catalog=" & DBName & ";Data Source=" & HOSTNAME
*/
}
CSQLAccess::~CSQLAccess()
{
/*if(m_pRs!=NULL)
{
m_pRs->Close();
m_pRs.Release();
}
if(m_pCon!=NULL)
{
m_pCon->Close();
m_pCon.Release();
}
CoUninitialize();*/
if (adStateOpen == m_pCon->State)
{
m_pCon->Close();
m_pCon.Release();
}
if(adStateOpen == m_pRs->State)
{ m_pRs->Close();
m_pRs.Release();
}
CoUninitialize();
}
/*_ReordsetPtr*/ BOOL CSQLAccess::UpDateSQL(_bstr_t str, VARIANT *var, long op)
{
try
{
m_pRs=m_pCon->Execute(str,var,op);
/*if(m_pRs)
return TRUE;
return FALSE;*/
return TRUE;
}catch(CException *e)
{
AfxMessageBox("UpDate Faile");
return FALSE;
}
}
_RecordsetPtr CSQLAccess::QuerySQL(_bstr_t str, VARIANT *var, long op)
{
try
{
m_pRs=m_pCon->Execute(str,var,op);
if(!m_pRs->rsEOF)
return m_pRs;
else
return m_pRs=NULL;
}catch(CException *e)
{
//throw(e);
AfxMessageBox("Query Faile");
return m_pRs=NULL;
}
//m_pCon->BeginTrans();
}
long CSQLAccess::BeginTransSQl()
{
long rt;
try
{
return rt=m_pCon->BeginTrans();
}catch(CException *e)
{
return -1;
}
}
HRESULT CSQLAccess::CommitTransSQL()
{
try
{
return m_pCon->CommitTrans();
}catch(CException)
{
return -1;
}
}
HRESULT CSQLAccess::RollbackTransSQL()
{
try
{
return m_pCon->RollbackTrans();
}catch(CException *e)
{
return -1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -