📄 ado.cpp
字号:
// ADO.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "ADO.h"
#include "ADODlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
_ConnectionPtr m_pConnection;//这是连接的智能指针
#define READ_FILE 0x10
#define WRITE_FILE 0x20
#define UNEXIST 0x30
#define EXIST 0x40
int WriteOrRead;//判断读写文件
int FileStatus;//判断文件是否存在
char LinkInfo[100] = "";
/////////////////////////////////////////////////////////////////////////////
// CADOApp
BEGIN_MESSAGE_MAP(CADOApp, CWinApp)
//{{AFX_MSG_MAP(CADOApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CADOApp construction
CADOApp::CADOApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CADOApp object
CADOApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CADOApp initialization
BOOL CADOApp::InitInstance()
{
AfxEnableControlContainer();
/////////////////////初始化COM对象///////////////////////////////
::CoInitialize(NULL); // COM 初始化
WriteOrRead = READ_FILE;
memset(LinkInfo,0,sizeof(LinkInfo));
if(!HandleDocFile(LinkInfo))
FileStatus = UNEXIST;
////////////连接数据库//////////////
if(UNEXIST == FileStatus)
{
exit:
CConnectInfo con;
con.DoModal();
sprintf(LinkInfo,"Provider=SQLOLEDB.1;Server=%s;Database=%s;uid=%s;pwd=%s",con.m_server,con.m_database,con.m_uid,con.m_pwd);
WriteOrRead = WRITE_FILE;
if(!HandleDocFile(LinkInfo))
return FALSE;
}
try
{
HRESULT hr;
hr = m_pConnection.CreateInstance("ADODB.Connection"); //创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open(LinkInfo,"","",adModeUnknown);
}
}
catch(_com_error e) //捕捉异常
{
CString errormessage;
errormessage.Format("连接失败!");
AfxMessageBox(errormessage); //显示错误信息
goto exit;
}
MessageBox(NULL,"连接数据库成功!","提示",MB_OK);
/////////////////////////////////////////////////////////////////
// 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
CADODlg 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 FALSE;
}
//////////////////////断开数据库//////////////////////////////////
int CADOApp::ExitInstance()
{
return CWinApp::ExitInstance();
}
bool CADOApp::HandleDocFile(char* buffer)
{
bool dwRet = TRUE;
HRESULT hr; // 函数执行返回值
DWORD size;
IStorage *pStg = NULL; // 根存储接口指针
IStorage *pSub = NULL; // 子存储接口指针
IStream *pStm = NULL; // 流接口指针
::CoInitialize(NULL); // COM 初始化
// 在实际的程序中则要使用条件判断和异常处理
if(READ_FILE == WriteOrRead)
{
hr = ::StgOpenStorage( // 建立复合文件
L"c:\\a.stg", // 文件名称
NULL,
STGM_READ | STGM_SHARE_EXCLUSIVE, // 打开方式
NULL,
0, // 保留参数
&pStg); // 取得根存储接口指针
if(!SUCCEEDED(hr))
{
dwRet = FALSE;
goto exit;
}
hr = pStg->OpenStorage( // 建立子存储
L"SubStg", // 子存储名称
NULL,
STGM_READ | STGM_SHARE_EXCLUSIVE, // 打开方式
NULL,
0,
&pSub); // 取得子存储接口指针
if(!SUCCEEDED(hr))
{
dwRet = FALSE;
goto exit;
}
hr = pSub->OpenStream( // 建立流
L"Stm", // 流名称
0,
STGM_READ | STGM_SHARE_EXCLUSIVE, // 打开方式
0,
&pStm); // 取得流接口指针
if(!SUCCEEDED(hr))
{
dwRet = FALSE;
goto exit;
}
hr = pStm->Read( // 向流中写入数据
buffer, // 数据地址
100, // 字节长度(注意,没有写入字符串结尾的\0)
&size); // 不需要得到实际写入的字节长度
if(!SUCCEEDED(hr))
{
dwRet = FALSE;
goto exit;
}
}
else
{
hr = ::StgCreateDocfile( // 建立复合文件
L"c:\\a.stg", // 文件名称
STGM_CREATE | STGM_WRITE | STGM_SHARE_EXCLUSIVE, // 打开方式
0, // 保留参数
&pStg); // 取得根存储接口指针
if(!SUCCEEDED(hr))
{
dwRet = FALSE;
goto exit;
}
hr = pStg->CreateStorage( // 建立子存储
L"SubStg", // 子存储名称
STGM_CREATE | STGM_WRITE | STGM_SHARE_EXCLUSIVE,
0,0,
&pSub); // 取得子存储接口指针
if(!SUCCEEDED(hr))
{
dwRet = FALSE;
goto exit;
}
hr = pSub->CreateStream( // 建立流
L"Stm", // 流名称
STGM_CREATE | STGM_WRITE | STGM_SHARE_EXCLUSIVE,
0,0,
&pStm); // 取得流接口指针
if(!SUCCEEDED(hr))
{
dwRet = FALSE;
goto exit;
}
hr = pStm->Write( // 向流中写入数据
buffer, // 数据地址
100, // 字节长度(注意,没有写入字符串结尾的\0)
&size); // 不需要得到实际写入的字节长度
if(!SUCCEEDED(hr))
{
dwRet = FALSE;
goto exit;
}
}
exit:
if( pStm ) pStm->Release();// 释放流指针
if( pSub ) pSub->Release();// 释放子存储指针
if( pStg ) pStg->Release();// 释放根存储指针
::CoUninitialize(); // COM 释放
return dwRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -