📄 createmdbdlg.cpp
字号:
// CreateMdbDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CreateMdb.h"
#include "CreateMdbDlg.h"
#include "iostream.h"
#include "stdio.h"
#include "conio.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
// Function name : RunSqlScript
// Description : 执行SQL脚本, peckermen@163.com, 2003-09-15
// Return type : BOOL 成功返回TRUE
// Argument : LPCSTR SqlScriptFile SQL脚本文件名
// Argument : char separator SQL 分割符号, 缺省';'
// Argument : char Remark SQL 注释符号, 缺省'-'
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCreateMdbDlg dialog
CCreateMdbDlg::CCreateMdbDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCreateMdbDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCreateMdbDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCreateMdbDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCreateMdbDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCreateMdbDlg, CDialog)
//{{AFX_MSG_MAP(CCreateMdbDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CREATEDB, OnCreatedb)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCreateMdbDlg message handlers
BOOL CCreateMdbDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CCreateMdbDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CCreateMdbDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCreateMdbDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCreateMdbDlg::OnCreatedb()
{
// TODO: Add your control notification handler code here
::CoInitialize(NULL); //初始化OLE/COM库环境
HRESULT hr = S_OK;
//Set ActiveConnection of Catalog to this string
CString strcnn(_T("Provider=Microsoft.JET.OLEDB.4.0;Data source = ../test.mdb"));
try
{
ADOX::_CatalogPtr m_pCatalog = NULL;
hr = m_pCatalog.CreateInstance(__uuidof (ADOX::Catalog));
if(FAILED(hr))
{
_com_issue_error(hr);
}
else
{
m_pCatalog->Create(_bstr_t(strcnn)); //Create MDB
}
}
catch(_com_error &e)
{
// Notify the user of errors if any.
AfxMessageBox(_T("error"));
}
// _ConnectionPtr g_pConn;
g_pConn.CreateInstance(__uuidof(Connection));
g_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=../test.mdb;",
"", "", adConnectUnspecified);
// 创建数据库表
RunSqlScript(_T("../test.sql"));
//关闭数据库连接:
g_pConn->Close();
::CoUninitialize();
}
BOOL CCreateMdbDlg::RunSqlScript(LPCSTR SqlScriptFile, char separator, char Remark)
{
BOOL bRet = FALSE;
CFileFind finder;
CString ErrLog;
if (finder.FindFile(SqlScriptFile) == TRUE)
{
CFile fSql;
TCHAR *buffer, *pSQL, *p;
fSql.Open(SqlScriptFile, CFile::modeRead);
UINT nFileLength = fSql.GetLength();
buffer = (TCHAR *)malloc((nFileLength + 1) * sizeof(TCHAR));
_tcsnset(buffer, TCHAR('\0'), nFileLength + 1);
UINT nBytesRead = fSql.Read(buffer,nFileLength); //把sql文件内容读入内存缓冲区
fSql.Close();
p = pSQL = buffer;
BOOL bRunOK = FALSE;
BOOL bInNote = FALSE;
BOOL bSkip = FALSE;
CString strSQL;
TCHAR ch;
ErrLog = _T("");
while (p < (buffer + nFileLength)){
//判断是否是注释行
if (bInNote)
{
if (*p == TCHAR('\x0a'))
bInNote = FALSE;
}
else
{
if ((*p == Remark) && (*(p+1) == Remark))
{
bInNote = TRUE;
p++;
}
else
{
//判断是否是sql语句结束标志
if (*p == separator){
strSQL = _T("");
bSkip = FALSE;
while (pSQL < p)
{
if (bSkip == FALSE){
if ((*(pSQL) == Remark) &&(*(pSQL+1) == Remark))
{
bSkip = TRUE;
pSQL++;
}
else
{
ch = *pSQL;
strSQL = strSQL+ch;
}
}
else{
if (*pSQL = TCHAR('\x0a')){
bSkip = FALSE;
ch = TCHAR(' ');
strSQL = strSQL + ch;
}
}
pSQL++;
}
pSQL = p + 1;
ErrLog = ErrLog + _T("-- SQL Running ...\n");
ErrLog = ErrLog + strSQL;
_variant_t vRecords;
int m_nRecordsAffected = 0;
try
{
g_pConn->CursorLocation = adUseClient;
g_pConn->Execute(_bstr_t((LPCTSTR)strSQL), &vRecords,
adExecuteNoRecords);
m_nRecordsAffected = vRecords.iVal;
bRunOK = TRUE;
}
catch(_com_error &e)
{
bRunOK = FALSE;
}
if (bRunOK)
ErrLog = ErrLog + _T("\n-- Successed!\n");
else
{
ErrLog = ErrLog + _T("\n-- Failed!\n");
break;
}
}
}
}
p++;
}
free(buffer);
bRet = bRunOK;
}
return bRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -