📄 test3dlg.cpp
字号:
// test3Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "test3.h"
#include "test3Dlg.h"
#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()
/////////////////////////////////////////////////////////////////////////////
// CTest3Dlg dialog
CTest3Dlg::CTest3Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CTest3Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTest3Dlg)
m_strDatabaseLocation = _T("");
m_strDatabaseName = _T("");
m_nInitSizeDatabase = 0;
m_nMaxSizeDatabase = 0;
m_strLDFLogicalName = _T("");
m_strMDFLogicalName = _T("");
m_strServerName = _T("");
m_strServerPassword = _T("");
m_nDatabaseGrowth = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTest3Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTest3Dlg)
DDX_Text(pDX, IDC_DATABASELOCATION, m_strDatabaseLocation);
DDX_Text(pDX, IDC_DATABASENAME, m_strDatabaseName);
DDX_Text(pDX, IDC_INITSIZEDATABASE, m_nInitSizeDatabase);
DDV_MinMaxInt(pDX, m_nInitSizeDatabase, 3, 100);
DDX_Text(pDX, IDC_MAXSIZEDATABASE, m_nMaxSizeDatabase);
DDX_Text(pDX, IDC_LDFLOGICALNAME, m_strLDFLogicalName);
DDX_Text(pDX, IDC_MDFLOGICALNAME, m_strMDFLogicalName);
DDX_Text(pDX, IDC_SERVERNAME, m_strServerName);
DDX_Text(pDX, IDC_SERVERPASSWORD, m_strServerPassword);
DDX_Text(pDX, IDC_DATABASEGROWTH, m_nDatabaseGrowth);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTest3Dlg, CDialog)
//{{AFX_MSG_MAP(CTest3Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BEGINCREATE, OnCreateDatabase)
ON_BN_CLICKED(IDC_BTNCHOOSEFOLDER, OnSetDatabaseLocation)
ON_BN_CLICKED(IDC_CANCEL, OnCancel)
ON_WM_RBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTest3Dlg message handlers
BOOL CTest3Dlg::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
::CoInitialize(NULL);
pConn.CreateInstance(_uuidof(Connection));
pRst.CreateInstance(_uuidof(Recordset));
pComm.CreateInstance(_uuidof(Command));
return TRUE; // return TRUE unless you set the focus to a control
}
void CTest3Dlg::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 CTest3Dlg::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 CTest3Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTest3Dlg::OnCreateDatabase()
{
// TODO: Add your control notification handler code here
int nDbGrowth;
int nInitSizeDb;
int nMaxSizeDb;
CString strDbGrowth;
CString strDatabaseLocation;
CString strDatabaseName;
CString strLDFLogicalName;
CString strMDFLogicalName;
CString strServerName;
CString strServerPassword;
CString strSQL;//exchange
CString strConnectionstring;//exchange
CString tempFolderPath;
CString MDFPath;
CString LDFPath;
UpdateData(TRUE);
//开始数据处理
nDbGrowth=m_nDatabaseGrowth;
strDbGrowth.Format("%d",nDbGrowth);
strDbGrowth=strDbGrowth+"%";
nInitSizeDb=m_nInitSizeDatabase;
nMaxSizeDb=m_nMaxSizeDatabase;
strDatabaseLocation=m_strDatabaseLocation;
strDatabaseName=m_strDatabaseName;
strLDFLogicalName=m_strLDFLogicalName;
strMDFLogicalName=m_strMDFLogicalName;
strServerName=m_strServerName;
strServerPassword=m_strServerPassword;
//文件路径转化
if(!FolderPath.IsEmpty())
{
tempFolderPath=FolderPath;
MDFPath=tempFolderPath+strMDFLogicalName+".mdf";
LDFPath=tempFolderPath+strMDFLogicalName+".ldf";
strMDFLogicalName=strMDFLogicalName+"__data";
strLDFLogicalName=strLDFLogicalName+"_log";
//创建数据库语句
strSQL.Format("CREATE DATABASE %s ON PRIMARY (Name=%s,filename=\'%s\',size=%d,maxsize=%d,filegrowth=%s)\
log on (name=%s,filename=\'%s\',size=%d, maxsize=%d,filegrowth=%s)",\
strDatabaseName,strMDFLogicalName,MDFPath,nInitSizeDb,nMaxSizeDb,strDbGrowth,\
strLDFLogicalName,LDFPath,nInitSizeDb,nMaxSizeDb,strDbGrowth);
//varSQL="CREATE DATABASE MyDataBase ON PRIMARY (Name=MyDataBase_data, filename ='d:\\MyDataBase.mdf', size=3, maxsize=5, filegrowth=10%)
//log on (name=MyDataBase_log,filename='d:\\MyDataBase.ldf',size=3, maxsize=20,filegrowth=10%)";
//连接数据库语句
strConnectionstring.Format("Provider=SQLOLEDB.1;Password=%s;Persist Security Info=True;User ID=%s;\
Initial Catalog=master;Data Source=ERIC-PC",strServerPassword,strServerName);
_variant_t varSQL=_variant_t(strSQL);
_bstr_t bstrConnectionstring=(_bstr_t)strConnectionstring;
// _bstr_t bstrConnectionstring=" Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=master;Data Source=ERIC-PC";
//使用系统数据库//////
try
{
pConn->Open(bstrConnectionstring,"sa","sa",adConnectUnspecified);
pRst->CursorLocation=adUseClient;
// varSQL="CREATE DATABASE MyDataBase ON PRIMARY (Name=MyDataBase_data, filename ='d:\\MyDataBase.mdf', size=3, maxsize=5, filegrowth=10%) log on (name=MyDataBase_log,filename='d:\\MyDataBase.ldf',size=3, maxsize=20,filegrowth=10%)";
pRst->Open(varSQL,bstrConnectionstring,adOpenStatic,adLockOptimistic,adCmdUnknown);
if(adStateOpen==pRst->State)
pRst->Close();//在这里已经关闭数据集了
}
catch(_com_error &e)
{
AfxMessageBox(e.Description());
}
//关闭已经创建的连接///////////
pConn->Close();
}
UpdateData(FALSE);
}
void CTest3Dlg::OnCancel()
{
// TODO: Add your control notification handler code here
::CoUninitialize();
exit(0);
}
void CTest3Dlg::OnSetDatabaseLocation()
{
//todo:locate folder
UpdateData(TRUE);
BROWSEINFO bi;
LPITEMIDLIST pidlBrowse;
char DisplayName[MAX_PATH];
char path[MAX_PATH];
bi.hwndOwner = m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = DisplayName;
bi.lpszTitle = "选择文件夹";
bi.ulFlags = BIF_EDITBOX;
bi.lpfn = NULL;
pidlBrowse = SHBrowseForFolder(&bi);
if (pidlBrowse)
{
SHGetPathFromIDList(pidlBrowse,path);
FolderPath=path;
m_strDatabaseLocation=path;
}
UpdateData(FALSE);
}
//
void CTest3Dlg::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//可以再这里面修菜单状态变化后的名字,列入“已创建数据库”
CMenu oMenu;
if (oMenu.LoadMenu(IDR_DYNAMICCREATION))
{
CMenu* pPopup = oMenu.GetSubMenu(0);
ASSERT(pPopup != NULL);
if(m_IsCreateDatabase)
{
pPopup->ModifyMenu(ID_CREATEDATABASE,MF_BYCOMMAND,ID_CREATEDATABASE,"创建数据库");
pPopup->ModifyMenu(ID_CREATETABLE,MF_BYCOMMAND,ID_CREATETABLE,"创建表");
pPopup->ModifyMenu(ID_CREATECOLUMN,MF_GRAYED,ID_CREATECOLUMN,"创建字段");
}
if(m_IsCreateTable)
{
pPopup->ModifyMenu(ID_CREATEDATABASE,MF_BYCOMMAND,ID_CREATEDATABASE,"创建数据库");
pPopup->ModifyMenu(ID_CREATETABLE,MF_BYCOMMAND,ID_CREATETABLE,"创建表");
pPopup->ModifyMenu(ID_CREATECOLUMN,MF_GRAYED,ID_CREATECOLUMN,"创建字段");
}
if(m_IsCreateColumn)
{
pPopup->ModifyMenu(ID_CREATEDATABASE,MF_BYCOMMAND,ID_CREATEDATABASE,"创建数据库");
pPopup->ModifyMenu(ID_CREATETABLE,MF_BYCOMMAND,ID_CREATETABLE,"创建表");
pPopup->ModifyMenu(ID_CREATECOLUMN,MF_GRAYED,ID_CREATECOLUMN,"创建字段");
}
CPoint oPoint;//定义一个用于确定光标位置的位置
// 确定鼠标位置以便在该位置附近显示菜单
GetCursorPos( &oPoint);//获取当前光标的位置,以便使得菜单可以跟随光标
SetForegroundWindow();
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,oPoint.x, oPoint.y, this);
}
CDialog::OnRButtonDown(nFlags, point);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -