?? mysqldlg.cpp
字號:
// MySQLDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MySQL.h"
#include "MySQLDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "AboutDlg.h"
/////////////////////////////////////////////////////////////////////////////
// CMySQLDlg dialog
CMySQLDlg::CMySQLDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMySQLDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMySQLDlg)
m_sSQL = _T("select * from user");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
ColNum=0;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMySQLDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMySQLDlg)
DDX_Control(pDX, IDC_DBNAME, m_cDbName);
DDX_Control(pDX, IDC_LIST, m_cList);
DDX_Text(pDX, IDC_SQL, m_sSQL);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMySQLDlg, CDialog)
//{{AFX_MSG_MAP(CMySQLDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SELECT, OnSelect)
ON_CBN_SELCHANGE(IDC_DBNAME, OnSelchangeDbname)
ON_BN_CLICKED(IDC_ABOUT, OnAbout)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMySQLDlg message handlers
BOOL CMySQLDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
m_cList.SetBkColor(RGB(180,200,220));
m_cList.SetTextBkColor(RGB(180,200,220));
m_cList.SetExtendedStyle(LVS_EX_INFOTIP|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
InitDatabase();
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CMySQLDlg::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 CMySQLDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
LRESULT CMySQLDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
switch(message)
{
case WM_SEL_CH:
m_cDbName.GetWindowText(SelDbName);
mysql_select_db(MyData,SelDbName);
break;
default:
break;
}
return CDialog::WindowProc(message, wParam, lParam);
}
void CMySQLDlg::InitDatabase()
{
MyData=mysql_init((MYSQL*)0);
mysql_real_connect(MyData,NULL,NULL,NULL,NULL,MYSQL_PORT,NULL,0);
My_Res=mysql_list_dbs(MyData,NULL);
if(!mysql_num_rows(My_Res))
return;
int i,
j;
CString DbName;
i=0;
DbName.Empty();
while(My_Row=mysql_fetch_row(My_Res))
{
i=mysql_num_fields(My_Res);
for(j=0;j<i;j++)
{
if(DbName.IsEmpty())
DbName=My_Row[j];
m_cDbName.AddString(My_Row[j]);
}
}
mysql_free_result(My_Res);
m_cDbName.SetCurSel(0);
mysql_select_db(MyData,DbName);
return;
}
void CMySQLDlg::OnSelect()
{
// TODO: Add your control notification handler code here
UpdateData();
if(ColNum)
{
for(int i=0;i<ColNum;i++)
m_cList.DeleteColumn(0);
}
m_cList.DeleteAllItems();
if(m_sSQL.IsEmpty())
{
MessageBox("請輸入SQL語句 ...",
"MySQL",MB_OK | MB_ICONINFORMATION);
return;
}
mysql_query(MyData,m_sSQL);
My_Res=mysql_store_result(MyData);
if(!My_Res)
{
MessageBox("mysql_store_result(...) 函數出錯!",
"MySQL",MB_OK | MB_ICONERROR);
return;
}
for(ColNum=0;My_Field=mysql_fetch_field(My_Res);ColNum++)
m_cList.InsertColumn(ColNum,My_Field->name,LVCFMT_CENTER,80,30);
if(!mysql_num_rows(My_Res))
{
MessageBox("程序在數據庫中未檢測到任何數據 ...",
"MySQL",MB_OK | MB_ICONINFORMATION);
return;
}
int i,j,k;
i=0;
while(My_Row=mysql_fetch_row(My_Res))
{
j=mysql_num_fields(My_Res);
for(k=0;k<j;k++)
if(k==0)
m_cList.InsertItem(i,My_Row[k]);
else
m_cList.SetItemText(i,k,My_Row[k]);
++i;
}
mysql_free_result(My_Res);
GetDlgItem(IDC_SQL)->SetFocus();
return;
}
void CMySQLDlg::OnSelchangeDbname()
{
// TODO: Add your control notification handler code here
PostMessage(WM_SEL_CH);
}
void CMySQLDlg::OnAbout()
{
// TODO: Add your control notification handler code here
CAboutDlg dlg;
dlg.DoModal();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -