📄 dynodbcdlg.cpp
字号:
// DynODBCDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DynODBC.h"
#include "DynODBCDlg.h"
#include <odbcinst.h>
#pragma comment(lib,"odbccp32.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
/////////////////////////////////////////////////////////////////////////////
// CDynODBCDlg dialog
CDynODBCDlg::CDynODBCDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDynODBCDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDynODBCDlg)
// 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 CDynODBCDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDynODBCDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDynODBCDlg, CDialog)
//{{AFX_MSG_MAP(CDynODBCDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_GENDB, OnGendb)
ON_BN_CLICKED(IDC_GENTBL, OnGentbl)
ON_BN_CLICKED(IDC_RMVDTSRC, OnRmvdtsrc)
ON_BN_CLICKED(IDC_GENDTSRC, OnGendtsrc)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDynODBCDlg message handlers
BOOL CDynODBCDlg::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 CDynODBCDlg::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 CDynODBCDlg::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 CDynODBCDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDynODBCDlg::OnOK()
{
// TODO: Add extra validation here
// CDialog::OnOK();
}
void CDynODBCDlg::OnCancel()
{
// TODO: Add extra cleanup here
// CDialog::OnCancel();
}
void CDynODBCDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
CDialog::OnOK();
}
void CDynODBCDlg::OnExit()
{
CDialog::OnOK();
}
void CDynODBCDlg::OnGendb()
{
CString sDriver="Microsoft Access Driver (*.mdb)";
CString sAccessFile,sPath,sSql;
char cFile[MAX_PATH+1];
GetModuleFileName(NULL,cFile,MAX_PATH);
sPath.Format("%s",cFile);
int nPos=sPath.ReverseFind('\\');
sPath=sPath.Left(nPos);
sAccessFile.Format("%s\\%s",sPath,"Test.mdb");
sSql.Format("CREATE_DB=\"%s\"\0\0",sAccessFile);
SQLConfigDataSource(NULL,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)",sSql);
CString sMessage;
sMessage.Format("在文件夹\n\n%s\n\n下产生了数据库文件 Test.mdb 。\n\n"
"请在该处查找并用 ACCESS 2000 打开查看。",sPath);
MessageBox(sMessage,"动态ODBC演示");
}
void CDynODBCDlg::OnGendtsrc()
{
char cFile[MAX_PATH+1];
CString sPath,sSql;
GetModuleFileName(NULL,cFile,MAX_PATH);
sPath.Format("%s",cFile);
int nPos = sPath.ReverseFind('\\');
sPath = sPath.Left(nPos);
sSql.Format("DSN=演示数据源;DBQ=Test.mdb;DEFAULTDIR=%s\0\0",sPath);
SQLConfigDataSource(NULL,ODBC_ADD_DSN,"MicroSoft Access Driver (*.mdb)",sSql);
MessageBox("请打开 ODBC 数据源管理器,在“用户DSN选项卡”下查看,\n\n"
"在 “用户数据源” 列表中增加了一项 “演示数据源”。");
}
void CDynODBCDlg::OnGentbl()
{
CDatabase db;
db.OpenEx("DSN=演示数据源");
db.ExecuteSQL("Create table Studnet(姓名 varchar(12),年龄 INT)");
db.ExecuteSQL("Create table Teacher(姓名 varchar(12),年龄 INT)");
db.Close();
MessageBox("现在请再次用 ACCESS 2000 打开数据库 Test.mdb 查看,\n\n"
"库中增加了两张表,分别为 表Student 和 表Teacher 。");
}
void CDynODBCDlg::OnRmvdtsrc()
{
SQLConfigDataSource(NULL,ODBC_REMOVE_DSN,"Microsoft Access Driver (*.mdb)","DSN=演示数据源");
MessageBox("请打开 ODBC 数据源管理器,在用户DSN选项卡下查看,\n\n"
"在 “用户数据源” 列表中的 “演示数据源” 项已经消失。");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -