?? ociexampledlg.cpp
字號:
// OCIExampleDlg.cpp : implementation file
//
#include "stdafx.h"
#include "OCIExample.h"
#include "OCIExampleDlg.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()
/////////////////////////////////////////////////////////////////////////////
// COCIExampleDlg dialog
COCIExampleDlg::COCIExampleDlg(CWnd* pParent /*=NULL*/)
: CDialog(COCIExampleDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(COCIExampleDlg)
m_strTablename = _T("");
m_strFieldName = _T("");
m_strcondition = _T("");
m_strFieldVal = _T("");
m_strConFName = _T("");
m_strConFVal = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
//可以改變程序的圖標
m_hIcon = AfxGetApp()->LoadIcon(IDR_ICON1);
//初始化服務器句柄為空
srvhp=NULL;
//
InsertDlg=NULL;
//
ColumnNumbers=0;
//
IsSizeInsertDlg=0;
}
void COCIExampleDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(COCIExampleDlg)
DDX_Control(pDX, IDC_CO_CONFNAME, m_comConFNameCtrl);
DDX_Control(pDX, IDC_COBFNAME, m_comboCtrl);
DDX_Control(pDX, IDC_LIST_TABLEDATA, m_listCtrl);
DDX_Control(pDX, IDC_LIST_TABLENAME, m_listTablename);
DDX_LBString(pDX, IDC_LIST_TABLENAME, m_strTablename);
DDX_CBString(pDX, IDC_COBFNAME, m_strFieldName);
DDX_CBString(pDX, IDC_COMB_CONDITION, m_strcondition);
DDX_Text(pDX, IDC_EFFIELDVAL, m_strFieldVal);
DDX_CBString(pDX, IDC_CO_CONFNAME, m_strConFName);
DDX_Text(pDX, IDC_E_CONFVAL, m_strConFVal);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(COCIExampleDlg, CDialog)
//{{AFX_MSG_MAP(COCIExampleDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUT_CONNECTDB, OnButConnectdb)
ON_BN_CLICKED(IDC_B_TABLESELECTOK, OnBTableselectok)
ON_BN_CLICKED(IDC_BUT_QUERY, OnButQuery)
ON_BN_CLICKED(IDC_BUT_DELETE, OnButDelete)
ON_BN_CLICKED(IDC_BUT_UPDATE, OnButUpdate)
ON_BN_CLICKED(IDC_BUT_INSERT, OnButInsert)
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COCIExampleDlg message handlers
BOOL COCIExampleDlg::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 COCIExampleDlg::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 COCIExampleDlg::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 COCIExampleDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void COCIExampleDlg::OnButConnectdb()
{
//OCIInitialize((ub4) OCI_DEFAULT,(dvoid *)0,(dvoid * (*)(dvoid *, size_t)) 0,
//(dvoid * (*)(dvoid *, dvoid *, size_t)) 0,(void (*)(dvoid *, dvoid *)) 0 );
/*
OCIInitialize(OCI_OBJECT,(dvoid *)0,
(dvoid * (*)(dvoid *, size_t)) 0,
(dvoid * (*)(dvoid *, dvoid *, size_t)) 0,
(void (*)(dvoid *, dvoid *)) 0 );
*/
//ub4 mode = OCI_SHARED | OCI_THREADED;
//代碼4.1.2
//Initializes the OCI process environment
OCIInitialize (OCI_OBJECT, 0, 0, 0, 0);
//初始化OCI環境
OCIEnvInit(&envhp,OCI_DEFAULT,0,0 );
//代碼4.2.1:
//分配句柄與數據結構
//分配錯誤句柄
OCIHandleAlloc(envhp,(void **)&errhp,OCI_HTYPE_ERROR,0,0);
//分配服務器句柄
OCIHandleAlloc(envhp,(void **)&srvhp,OCI_HTYPE_SERVER,0,0);
//分配服務環境句柄
OCIHandleAlloc(envhp,(void **)&svchp,OCI_HTYPE_SVCCTX,0,0);
//分配會話句柄
OCIHandleAlloc(envhp,(void **)&authp,OCI_HTYPE_SESSION,0,0);
// allocate describe handle
//分配描述句柄
OCIHandleAlloc(envhp,(dvoid **) &dschp,OCI_HTYPE_DESCRIBE,0,0);
//分配語句句柄
ErrorProc(errhp,OCIHandleAlloc((dvoid *) envhp,(dvoid **) &stmthp,OCI_HTYPE_STMT,0,0));
//代碼4.3.1
//產生接收用戶數據的對話框
CConectDlg ConnDlg;
ConnDlg.m_strDBName="imagedb";
ConnDlg.m_strUserName="scott";
ConnDlg.m_strUserID="tiger";
if(ConnDlg.DoModal()==IDCANCEL)
return;
//代碼4.3.2
// 連接數據庫
UpdateData(TRUE);sword status;
status=OCIServerAttach(srvhp,errhp,
(unsigned char *)(LPCTSTR)ConnDlg.m_strDBName,
(sb4)strlen(ConnDlg.m_strDBName),OCI_DEFAULT);
// 設置數據庫的屬性:服務器環境
ErrorProc(errhp,OCIAttrSet((dvoid *) svchp,
(ub4) OCI_HTYPE_SVCCTX,
(dvoid *) srvhp, (ub4) 0,
OCI_ATTR_SERVER, errhp));
//代碼4.3.3,認證用戶并且開始會話
LPCTSTR uid, pwd;
uid=ConnDlg.m_strUserName;
pwd=ConnDlg.m_strUserID;
//認證用戶名稱;
ErrorProc(errhp,OCIAttrSet(authp,
OCI_HTYPE_SESSION, (dvoid *)uid,
(ub4)strlen(uid),OCI_ATTR_USERNAME,errhp));
//認證用戶密碼
ErrorProc(errhp,OCIAttrSet(authp,
OCI_HTYPE_SESSION, (dvoid *)pwd,
(ub4)strlen(pwd), OCI_ATTR_PASSWORD,errhp));
//以申明的用戶名稱與密碼開始會話
status=OCISessionBegin(svchp, errhp,
authp,OCI_CRED_RDBMS,OCI_DEFAULT);
if(status!=0)
return ;
// set the Service context
status=OCIAttrSet(svchp, (ub4)OCI_HTYPE_SVCCTX,
(dvoid *)authp,0,OCI_ATTR_SESSION,errhp);
if(status!=0)
return;
//代碼4.4.2
//處理SQL的第一步,準備SQL
//CString strSQL="SELECT TABLE_NAME FROM USER_TABLES";
text textSQL[1024];
wsprintf((char*)textSQL,"%s","SELECT TABLE_NAME FROM USER_TABLES");
status=OCIStmtPrepare(stmthp,errhp,textSQL,
strlen((char *)textSQL),OCI_NTV_SYNTAX,OCI_DEFAULT);
//代碼4.4.3
//處理SQL的第三步,執行
status=OCIStmtExecute(svchp,stmthp,errhp,0,0,NULL,NULL,OCI_DEFAULT);
//代碼4.4.4
//處理SQL的第四步,描述
ub4 col_num;//存放SELECT語句選中的列數
//ub4 k=1;
//讀取選擇列表中的項數
ErrorProc(errhp,OCIAttrGet(stmthp,OCI_HTYPE_STMT,&col_num,0,
OCI_ATTR_PARAM_COUNT,errhp));
//為選擇項分配參數描述符
ErrorProc(errhp,OCIParamGet(stmthp,OCI_HTYPE_STMT,errhp,(void **)&colhp,
1));
//讀取選擇項的數據長度
ErrorProc(errhp,OCIAttrGet(colhp,OCI_DTYPE_PARAM,&collen[0],0,
OCI_ATTR_DATA_SIZE,errhp));
//讀取選擇項的數據類型
ErrorProc(errhp,OCIAttrGet(colhp,OCI_DTYPE_PARAM,&coltype[0],0,
OCI_ATTR_DATA_TYPE,errhp));
//分配緩沖區
colbuf[0]=(text *)new text[(int)collen[0]+1];
//代碼4.4.5
//處理SQL的第五步,定義變量
status=OCIDefineByPos(stmthp,&defhp[0],errhp,1,(ub1*)colbuf[0],
collen[0]+1,SQLT_STR,&ind[0],0,0,OCI_DEFAULT);
//代碼4.4.6
//處理SQL的第六步,取值
while((OCIStmtFetch(stmthp,errhp,1,OCI_FETCH_NEXT,OCI_DEFAULT))!=OCI_NO_DATA)
{
//CString pass=colbuf[0];
//if(pass.Find('$')==-1)
//把獲取的用戶的基表的名稱加到ListBox控件中
m_listTablename.AddString((char *)colbuf[0]);
}
//刪除所分配的緩沖區
delete colbuf[0];
//使得選擇表的按鈕可用
GetDlgItem(IDC_B_TABLESELECTOK )->EnableWindow(true);
}
BOOL COCIExampleDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
if(srvhp!=NULL)
{
//代碼4.5.1
//結束會話
OCISessionEnd(svchp, errhp, authp, (ub4) 0);
//斷開與數據庫的連接
OCIServerDetach(srvhp, errhp, OCI_DEFAULT);
//釋放OCI句柄
OCIHandleFree((dvoid *) srvhp, OCI_HTYPE_SERVER);
OCIHandleFree((dvoid *) svchp, OCI_HTYPE_SVCCTX);
OCIHandleFree((dvoid *) errhp, OCI_HTYPE_ERROR);
OCIHandleFree((dvoid *) authp, OCI_HTYPE_SESSION);
OCIHandleFree((dvoid *) stmthp, OCI_HTYPE_STMT);
OCIHandleFree((dvoid *) dschp, OCI_HTYPE_DESCRIBE);
}
return CDialog::DestroyWindow();
}
void COCIExampleDlg::ErrorProc(dvoid *err, sword status)
{
text errbuf[512];
sb4 errcode;
CString str;
switch(status)
{
case OCI_SUCCESS:
break;
case OCI_SUCCESS_WITH_INFO:
(void) AfxMessageBox("OCI錯誤代碼:OCI_SUCCESS_WITH_INFO\n");
break;
case OCI_NEED_DATA:
(void) AfxMessageBox("OCI錯誤代碼:OCI_NEED_DATA\n");
break;
case OCI_NO_DATA:
(void) AfxMessageBox("OCI錯誤代碼:OCI_NO_DATA\n");
break;
case OCI_ERROR:
(void)OCIErrorGet((dvoid *)errhp,(ub4)1,NULL,&errcode,
errbuf,(ub4)sizeof(errbuf),OCI_HTYPE_ERROR);
str.Format("錯誤號:%d\n錯誤信息:%s\n",errcode,errbuf);
AfxMessageBox(str);
break;
case OCI_INVALID_HANDLE:
(void) AfxMessageBox("OCI錯誤代碼:OCI_INVALID_HANDLE\n");
break;
case OCI_STILL_EXECUTING:
(void) AfxMessageBox("OCI錯誤代碼:OCI_STILL_EXECUTE\n");
break;
default:
break;
}
}
//代碼4.1.3,OCI錯誤報告函數
/*
ErrorProc(dvoid *err, sword status)
{
CString str;sb4 errcode;text errbuf[512];
if(status==OCI_ERROR)
{
//調用OCI的錯誤獲取函數
OCIErrorGet((dvoid *)errhp,(ub4)1,NULL,&errcode,
errbuf,(ub4)sizeof(errbuf),OCI_HTYPE_ERROR);
str.Format("錯誤號:%d\n錯誤信息:%s\n",errcode,errbuf);
AfxMessageBox(str);
}
}
*/
COCIExampleDlg::~COCIExampleDlg()
{
if(InsertDlg!=NULL)
delete InsertDlg;
}
void COCIExampleDlg::OnBTableselectok()
{
// TODO: Add your control notification handler code here
//代碼5.1,
//獲取控件2所選中的表名
TableName="";
int item=m_listTablename.GetCurSel();
m_listTablename.GetText(item,TableName);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -