📄 databasemonitordlg.cpp
字号:
// DatabaseMonitorDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DatabaseMonitor.h"
#include "DatabaseMonitorDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CDatabaseMonitorDlg dialog
CDatabaseMonitorDlg::CDatabaseMonitorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDatabaseMonitorDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDatabaseMonitorDlg)
m_csLogFileSize = _T("");
m_csCAvailableSize = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pConnection = NULL;
m_bFirst = true;
m_iMonitorInterval = 3600*1000;
}
void CDatabaseMonitorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDatabaseMonitorDlg)
DDX_Text(pDX, IDC_STATIC_LOG_FILE_SIZE, m_csLogFileSize);
DDX_Text(pDX, IDC_STATIC_C_AVAILABLE_SIZE, m_csCAvailableSize);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDatabaseMonitorDlg, CDialog)
//{{AFX_MSG_MAP(CDatabaseMonitorDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_BN_CLICKED(ID_SHRINK, OnShrink)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDatabaseMonitorDlg message handlers
BOOL CDatabaseMonitorDlg::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
HKEY hKey;
if(ERROR_SUCCESS==RegCreateKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\",&hKey))
{
char szBuf[1000];
strcpy(szBuf, "C:\\thunder\\washing\\Washingsvr\\DatabaseMonitor.exe");
// Add the name to the EventMessageFile subkey.
if (RegSetValueEx(hKey, // subkey handle
"WashingDatabaseMonitor", // value name
0, // must be zero
REG_EXPAND_SZ, // value type
(LPBYTE) szBuf, // pointer to value data
strlen(szBuf) + 1)) // length of value data
{
TRACE("Error\n");
}
RegCloseKey(hKey);
}
if(ConnectDB()==FALSE)
return FALSE;
SetTimer(UINT(this),m_iMonitorInterval, NULL);
SetDBOption();
GetInfo();
return TRUE; // return TRUE unless you set the focus to a control
}
void CDatabaseMonitorDlg::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 CDatabaseMonitorDlg::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 CDatabaseMonitorDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CDatabaseMonitorDlg::ConnectDB()
{
//_chdrive
char szServerIP[64];
char szDBName[32];
char szUser[32];
char szPwd[32];
memset(szServerIP, 0, sizeof(szServerIP));
memset(szDBName, 0, sizeof(szDBName));
memset(szUser, 0, sizeof(szUser));
memset(szPwd, 0, sizeof(szPwd));
char szIniFile[MAX_PATH];
strcpy(szIniFile, "C:\\Thunder\\Thunder.ini");
// GetPrivateProfileString(_T("MainServer"), _T("DataBaseServerIp"), "127.0.0.1",
// szServerIP, sizeof(szServerIP), szIniFile);
strcpy(szServerIP,"127.0.0.1");
// strcpy(szServerIP,"192.168.0.160");
GetPrivateProfileString(_T("Washing"), _T("DataBaseName"), "Washing",
szDBName, sizeof(szDBName), szIniFile);
GetPrivateProfileString(_T("Washing"), _T("UserName"), "sa",
szUser, sizeof(szUser), szIniFile);
GetPrivateProfileString(_T("Washing"), _T("PassWord"), "",
szPwd, sizeof(szPwd), szIniFile);
CString strCon = "";
CString strTemp = "";
strTemp.Format("Provider=sqloledb;Data Source=%s", szServerIP);
strCon +=strTemp;
strTemp.Format(";Initial Catalog=%s",szDBName);
strCon +=strTemp;
strTemp.Format("; user id = %s",szUser);
strCon +=strTemp;
strTemp.Format("; password = %s",szPwd);
strCon +=strTemp;
try
{
m_pConnection.CreateInstance( __uuidof(Connection) );
m_pConnection->Open(strCon.GetBuffer(0), "", "", adConnectUnspecified);
}
catch(...)
{
AfxMessageBox("无法连接数据库。");
OnCancel();
return FALSE;
}
return TRUE;
}
void CDatabaseMonitorDlg::OnCancel()
{
// TODO: Add extra cleanup here
if(AfxMessageBox("确认退出吗?", MB_YESNO) ==IDYES)
{
KillTimer(UINT(this));
CDialog::OnCancel();
}
}
CDatabaseMonitorDlg::~CDatabaseMonitorDlg()
{
if(m_pConnection)
{
m_pConnection.Release();
}
}
void CDatabaseMonitorDlg::OnTimer(UINT nIDEvent)
{
if(m_bFirst)
m_bFirst = false;
GetInfo();
CDialog::OnTimer(nIDEvent);
}
void CDatabaseMonitorDlg::OnShrink()
{
if(atoi(m_csLogFileSize) < 1000)
{
AfxMessageBox("日志文件小于1G,没有必要压缩日志文件。");
return;
}
m_csHint = "真的要压缩数据库吗?\n必须在停止营业后压缩。";
ShrinkDB();
}
bool CDatabaseMonitorDlg::BackupDatabase()
{
_CommandPtr pCmd = NULL;
_ParameterPtr pPrm = NULL;
FieldPtr pTmp;
try
{
long iReturn, iLogFileSize = 0, iMainFileSize = 0;
// iReturn = GetFirstLongField("SELECT TOP 1 [size] * (8192/1024) / 1024 FROM SYSFILES WHERE [name] = 'karaok_log'", &iLogFileSize);
// iReturn = GetFirstLongField("SELECT TOP 1 [size] * (8192/1024) / 1024 FROM SYSFILES WHERE [name] = 'karaok'", &iMainFileSize);
iReturn = GetFirstLongField("SELECT TOP 1 [size] * (8192/1024) / 1024 FROM SYSFILES WHERE [name] = 'washing_log'", &iLogFileSize);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -