?? adotestdlg.cpp
字號:
// ADOTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ADOTest.h"
#include "ADOTestDlg.h"
#include "DbOperater.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
char strServer[MAX_SERVER_NAME];
char strUID[MAX_USER_NAME];
char strPWD[MAX_PASSWORD];
char strDBname[MAX_DATABASE_NAME];
_ConnectionPtr pADOConn;//ADO連接類
long retADOConn=-1;//ADO連接返回
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()
/////////////////////////////////////////////////////////////////////////////
// CADOTestDlg dialog
CADOTestDlg::CADOTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CADOTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CADOTestDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CADOTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CADOTestDlg)
DDX_Control(pDX, IDC_BUTTON_QUERY, m_QueryCntrl);
DDX_Control(pDX, IDC_EDIT_QUERY_RESULT, m_QueryResultCntrl);
DDX_Control(pDX, IDC_EDIT_QUERY_SENTENCE, m_QuerySentenceCntrl);
DDX_Control(pDX, IDC_BUTTON_DISCONNECT, m_DisConnectCntrl);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CADOTestDlg, CDialog)
//{{AFX_MSG_MAP(CADOTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
ON_BN_CLICKED(IDC_BUTTON_DISCONNECT, OnButtonDisconnect)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON_QUERY, OnButtonQuery)
ON_EN_CHANGE(IDC_EDIT_QUERY_SENTENCE, OnChangeEditQuerySentence)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CADOTestDlg message handlers
BOOL CADOTestDlg::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
//初始化數據庫連接信息
SetDlgItemText(IDC_EDIT_ADDRESS,"192.168.1.28");
SetDlgItemText(IDC_EDIT_USER,"sa");
SetDlgItemText(IDC_EDIT_PWD,"");
SetDlgItemText(IDC_EDIT_DATABASE,"NorthWind");
//初始化數據庫連接狀態
bDBConnected = FALSE;
//初始化OLE
AfxOleInit();
return TRUE; // return TRUE unless you set the focus to a control
}
void CADOTestDlg::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 CADOTestDlg::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 CADOTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CADOTestDlg::OnButtonConnect()
{
// TODO: Add your control notification handler code here
GetDlgItemText(IDC_EDIT_ADDRESS,Server);
GetDlgItemText(IDC_EDIT_USER,UID);
GetDlgItemText(IDC_EDIT_PWD,PWD);
GetDlgItemText(IDC_EDIT_DATABASE,DBname);
//把輸入的數據庫連接信息保存在全局變量中
strcpy(strServer,(LPCTSTR)Server);
strcpy(strUID,(LPCTSTR)UID);
strcpy(strPWD,(LPCTSTR)PWD);
strcpy(strDBname,(LPCTSTR)DBname);
CString strInfo;
strInfo.Format("Server:%s;User:%S;PSW:%s;Database:%s",
Server,UID,PWD,DBname);
CEdit* pInfo;
pInfo = (CEdit*)GetDlgItem(IDC_EDIT_INFO);
if(InitDb()==0)
{
bDBConnected = TRUE;
m_DisConnectCntrl.EnableWindow(TRUE);
strInfo = strInfo+"\r\n數據庫連接成功";
pInfo->SetWindowText(strInfo);
m_QuerySentenceCntrl.SetWindowText("select * from Categories");
}
else
{
bDBConnected = FALSE;
strInfo = strInfo+"\r\n數據庫連接失敗";
pInfo->SetWindowText(strInfo);
}
}
void CADOTestDlg::OnButtonDisconnect()
{
// TODO: Add your control notification handler code here
if(bDBConnected)
{
bDBConnected = FALSE;
FreeDb();
m_DisConnectCntrl.EnableWindow(FALSE);
m_QueryCntrl.EnableWindow(FALSE);
CString strInfo;
strInfo.Format("Server:%s;User:%S;PSW:%s;Database:%s",
Server,UID,PWD,DBname);
strInfo += "\r\n數據庫連接被斷開";
CEdit* pInfo;
pInfo = (CEdit*)GetDlgItem(IDC_EDIT_INFO);
pInfo->SetWindowText(strInfo);
}
}
void CADOTestDlg::OnDestroy()
{
if(bDBConnected)
{
bDBConnected = FALSE;
m_DisConnectCntrl.EnableWindow(FALSE);
m_QueryCntrl.EnableWindow(FALSE);
FreeDb();
}
CDialog::OnDestroy();
}
void CADOTestDlg::OnButtonQuery()
{
// TODO: Add your control notification handler code here
//設置操作數據庫的字符串
_RecordsetPtr pRecordset;
char SQLstrTemp[500];
char * pSqlstr=SQLstrTemp;
memset(pSqlstr,0x00,500);
CString strSQL;
m_QuerySentenceCntrl.GetWindowText(strSQL);
strcpy(pSqlstr,(LPCTSTR)strSQL);
try
{
HRESULT hr;
pRecordset.CreateInstance(__uuidof(Recordset));
pRecordset->Open(pSqlstr,
_variant_t((IDispatch*)pADOConn),
adOpenStatic,
adLockOptimistic,
adCmdFile);
CComVariant vField;
CComVariant vName;
FieldsPtr pFields;
FieldPtr pField;
CString strStationID;
CString strResult;
_variant_t vColumn;
strResult="選中結果如下:\r\n";
while(!pRecordset->adoEOF)
{
hr = pRecordset->get_Fields(&pFields);
long lCount;
short j;
DataTypeEnum DataType;
pFields->get_Count(&lCount);
for(j=0;j<lCount;j++)
{
vColumn = (short)j;
hr = pFields->get_Item(vColumn, &pField) ;
pField->get_Type(&DataType);
switch(DataType)
{
case adTinyInt:
case adSmallInt:
case adInteger:
case adBigInt:
case adUnsignedTinyInt:
case adUnsignedSmallInt:
case adUnsignedInt :
case adUnsignedBigInt:
hr = pField->get_Value (&vName) ;
long nTemp;
nTemp = vName.bVal;
strStationID.Format("%d",nTemp);
break;
case adBSTR:
case adChar :
case adVarChar:
case adLongVarChar :
case adWChar :
case adVarWChar :
case adLongVarWChar :
hr = pField->get_Value (&vName) ;
strStationID=vName.bstrVal;
// strStationID.Format("%14s",strStationID);
break;
default:
strStationID="未考慮此類型數據的處理!";
break;
}
// vField = "CategoryName";
// hr = pFields->get_Item(vField, &pField) ;
strResult += strStationID;
strResult += "\t\t";
}
strResult += "\r\n";
pRecordset->MoveNext();
}
m_QueryResultCntrl.SetWindowText(strResult);
pRecordset->Close();
pRecordset = NULL;
}
catch(...)
{
if(pRecordset->State != adStateClosed)
{
pRecordset->Close();
}
pRecordset = NULL;
AfxMessageBox("查詢出錯,請檢查查詢語句的正確性");
return;
}
}
void CADOTestDlg::OnChangeEditQuerySentence()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
if(bDBConnected)
{
if(m_QuerySentenceCntrl.GetWindowTextLength()>0)
{
m_QueryCntrl.EnableWindow(TRUE);
}
else
{
m_QueryCntrl.EnableWindow(FALSE);
}
m_QueryResultCntrl.SetWindowText("");
}
else
{
AfxMessageBox("數據庫未連接,請連接數據庫!");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -