COCIExampleDlg.cpp
上传用户:qiye66685
上传日期:2013-03-10
资源大小:49k
文件大小:16k
- // COCIExampleDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "COCIExample.h"
- #include "COCIExampleDlg.h"
- #include "ConnectDlg.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()
- /////////////////////////////////////////////////////////////////////////////
- // CCOCIExampleDlg dialog
- CCOCIExampleDlg::CCOCIExampleDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CCOCIExampleDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CCOCIExampleDlg)
- 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_MAINFRAME);
- }
- CCOCIExampleDlg::~CCOCIExampleDlg()
- {
- OCISessionEnd(svchp,errhp,authp,(ub4)0);
- OCIServerDetach(srvhp,errhp,OCI_DEFAULT);
- 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);
- }
- void CCOCIExampleDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CCOCIExampleDlg)
- 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_EDIT1, m_strConFVal);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CCOCIExampleDlg, CDialog)
- //{{AFX_MSG_MAP(CCOCIExampleDlg)
- 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_UPTATE, OnButUpdate)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CCOCIExampleDlg message handlers
- BOOL CCOCIExampleDlg::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 CCOCIExampleDlg::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 CCOCIExampleDlg::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 CCOCIExampleDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CCOCIExampleDlg::OnButConnectdb()
- {
- // TODO: Add your control notification handler code here
- ErrorProc(errhp,OCIEnvInit(& envhp,OCI_DEFAULT,0,0));
- 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);
- OCIHandleAlloc((dvoid*)envhp,(dvoid**)& dschp,OCI_HTYPE_DESCRIBE,0,0);
- OCIHandleAlloc((dvoid*)envhp,(dvoid**)& stmthp,OCI_HTYPE_STMT,0,0);
- CConnectDlg ConnDlg;
- ConnDlg.m_strDBName="vwserver";
- ConnDlg.m_strUserName="";
- ConnDlg.m_strUserID="";
- if(ConnDlg.DoModal()==IDCANCEL)
- {
- MessageBox("DLG has been out!");
- return;
- }
-
- UpdateData(true);
- sword status;
- status=OCIServerAttach(srvhp,errhp,(unsigned char*)(LPCTSTR)ConnDlg.m_strDBName,
- (sb4)strlen(ConnDlg.m_strDBName),OCI_DEFAULT);
- if(status!=0)
- {
- MessageBox("Database has been found!");
- }
-
- ErrorProc(errhp,OCIAttrSet((dvoid*)svchp,(ub4) OCI_HTYPE_SVCCTX,
- (dvoid*)srvhp,(ub4)0,OCI_ATTR_SERVER,errhp));
- 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);//这句话连不上!
- ErrorProc(errhp,OCISessionBegin(svchp,errhp,authp,OCI_CRED_RDBMS,OCI_DEFAULT));
-
- if(status==OCI_ERROR)
- {
- MessageBox("Database has not been connected!");
- return;
- }
-
- status=OCIAttrSet(svchp,OCI_HTYPE_SVCCTX,(dvoid*)authp,0,OCI_ATTR_SESSION,errhp);
- if(status!=0) return;
- CString strSQL;
- text textSQL[1024];
- strSQL="SELECT TABLE_NAME FROM USER_TABLES";
- wsprintf((char*)textSQL,"%s",strSQL);
- status=OCIStmtPrepare(stmthp,errhp,textSQL,
- strlen((char*)textSQL),OCI_NTV_SYNTAX,OCI_DEFAULT);
-
- ErrorProc(errhp,OCIStmtExecute(svchp,stmthp,errhp,(ub4)0,0,
- NULL,NULL,OCI_DEFAULT));
- // ub4 col_num;//还没有使用.
- 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];
- ErrorProc(errhp,OCIDefineByPos(stmthp,&defhp[0],
- errhp,1,(ub1*)colbuf[0],collen[0]+1,SQLT_STR,
- &ind[0],0,0,OCI_DEFAULT)); //defhp中的参数修改过!
- while((OCIStmtFetch(stmthp,errhp,1,OCI_FETCH_NEXT,
- OCI_DEFAULT))!=OCI_NO_DATA)
- {
- m_listTablename.AddString((char*) colbuf[0]);
- }
- delete colbuf[0];
-
- }
- void CCOCIExampleDlg::ErrorProc(dvoid *err, sword status)
- {
- CString str;
- sb4 errcode;
- text errbuf[512];
- if(status==OCI_ERROR)
- {
- OCIErrorGet((dvoid*)errhp,(ub4)1,NULL,&errcode,errbuf,
- (ub4)sizeof(errbuf),OCI_HTYPE_ERROR);
- str.Format("error:%dn error information:%sn",errcode,errbuf);
- AfxMessageBox(str);
- }
- }
- void CCOCIExampleDlg::OnBTableselectok()
- {
- // TODO: Add your control notification handler code here
- TableName="";
- int item=m_listTablename.GetCurSel();
- m_listTablename.GetText(item,TableName);
- if(TableName=="")return;
- text textSQL[1024];
- sword status;
- status=wsprintf((char *)textSQL,"SELECT * FROM %s",TableName);
- if(OCI_SUCCESS!=OCIStmtPrepare(stmthp,errhp,textSQL,strlen((char *)textSQL),
- OCI_NTV_SYNTAX,OCI_DEFAULT))
- {
- ErrorProc(errhp,status);
- return;
- }
- if(OCI_SUCCESS!=OCIStmtExecute(svchp,stmthp,errhp,(ub4)0,0,NULL,NULL,OCI_DEFAULT))
- {
- MessageBox("SQL语句没有成功执行!");
- ErrorProc(errhp,status);
- return;
- }
- ub4 col_num;
-
- ErrorProc(errhp,OCIAttrGet(stmthp,OCI_HTYPE_STMT,&col_num,0,OCI_ATTR_PARAM_COUNT,errhp));
- ColumnNumbers=(int)col_num;
- text *namep;
- ub4 sizep;
- text tempText[100];
- for(int i=0;i<(int)col_num;i++)
- {
- ErrorProc(errhp,OCIParamGet(stmthp,OCI_HTYPE_STMT,errhp,(void**)&colhp,ub4(i+1)));
- ErrorProc(errhp,OCIAttrGet(colhp,OCI_DTYPE_PARAM,&collen[i],0,OCI_ATTR_DATA_SIZE,errhp));
- ErrorProc(errhp,OCIAttrGet(colhp,OCI_DTYPE_PARAM,&coltype[i],0,OCI_ATTR_DATA_TYPE,errhp));
- if(coltype[i]==SQLT_DAT) collen[i]=30;
- colbuf[i]=(text*)new text[(int)collen[i]+1];
-
- ErrorProc(errhp,OCIAttrGet(colhp,OCI_DTYPE_PARAM,(dvoid*)&namep,(ub4*)&sizep,OCI_ATTR_NAME,errhp));
- strncpy((char*)tempText,(char*)namep,(size_t)sizep);
- tempText[sizep]=' ';
- ColName[i].Format("%s",tempText);
-
- }
- for(i=0;i<(int)col_num;i++)
- {
- if(OCI_SUCCESS!=OCIDefineByPos(stmthp,&defhp[i],errhp,i+1,(ub1*)colbuf[i],
- collen[i]+1,SQLT_STR,&ind[i],0,0,OCI_DEFAULT))
- {
- ErrorProc(errhp,status);
- return;
- }
- }
- int row=0;
-
- while(OCI_SUCCESS==OCIStmtFetch(stmthp,errhp,1,OCI_FETCH_NEXT,OCI_DEFAULT))
- {
- for(i=0;i<(int)col_num;i++)
- {
- ColVal[row][i]=colbuf[i];
- }
- row++;
- }
-
- for(i=0;i<(int)col_num;i++)//problem has been solved!
- delete colbuf[i];
-
- m_listCtrl.ModifyStyle(NULL,LVS_REPORT);
- m_listCtrl.SetExtendedStyle(LVS_EX_FLATSB |LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
- m_listCtrl.SetFocus();
-
- int ColNumber=m_listCtrl.GetHeaderCtrl()->GetItemCount();
- for(i=0;i<ColNumber;i++)
- m_listCtrl.DeleteColumn(i);//change the original.(0->i)(my idea is right!)
- for(i=0;i<(int)col_num;i++)
- m_listCtrl.InsertColumn(i,ColName[i],LVCFMT_LEFT,60,0);
- m_listCtrl.DeleteAllItems();
- for(i=0;i<row;i++)
- {
- m_listCtrl.InsertItem(LVIF_TEXT|LVIF_STATE,i,ColVal[i][0],
- (i%2)==0?LVIS_SELECTED:0,LVIS_SELECTED,0,0);
- for(int j=1;j<(int)col_num;j++)
- m_listCtrl.SetItemText(i,j,ColVal[i][j]);
- } //自己把参数写错了,以后遍尝的时候要特别小心!
- for(i=0;i<(int)ColumnNumbers;i++)
- m_comboCtrl.AddString(ColName[i]);
-
- for(i=0;i<(int)col_num;i++)
- m_comConFNameCtrl.AddString(ColName[i]);
-
- }
- void CCOCIExampleDlg::OnButQuery()
- {
- // TODO: Add your control notification handler code here
- UpdateData(TRUE);
- CString FieldName=m_strFieldName;
- CString strCondition=m_strCondition;
- CString strFieldVal=m_strFieldVal;
-
- sword status;
- text textSQL[1024];
- wsprintf((char*)textSQL,"SELECT *FROM %s WHERE %s %s :CONDITION",
- TableName,FieldName,strCondition);
- if(status=OCIStmtPrepare(stmthp,errhp,textSQL,
- strlen((char*)textSQL),OCI_NTV_SYNTAX,OCI_DEFAULT))
- {
- ErrorProc(errhp,status);
- return;
- }
-
- text *textConVal;
- sb4 len;
- int strlength=strFieldVal.GetLength();
- textConVal=new text[strlength+1];
- wsprintf((char*)textConVal,"%s",strFieldVal);
- textConVal[strlength]=' ';
- len=strlen((const char*)textConVal)+1;
- if(status=OCIBindByName(stmthp,&bidhp[0],errhp,(text*) ":CONDITION",-1,
- (ub1 *)textConVal,len,SQLT_STR,0,0,0,0,0,OCI_DEFAULT))
- {
- ErrorProc(errhp,status);
- return;
- }
- if(status=OCIStmtExecute(svchp,stmthp,errhp,(ub4)0,0,NULL,NULL,OCI_DEFAULT))
- {
- ErrorProc(errhp,status);
- return;
- }
- ub4 col_num;
- ErrorProc(errhp,OCIAttrGet(stmthp,OCI_HTYPE_STMT,&col_num,0,OCI_ATTR_PARAM_COUNT,errhp));
- for(int i=0;i<(int)col_num;i++)
- {
- ErrorProc(errhp,OCIParamGet(stmthp,OCI_HTYPE_STMT,errhp,
- (void **)&colhp,ub4(i+1)));
- ErrorProc(errhp,OCIAttrGet(colhp,OCI_DTYPE_PARAM,&collen[i],0,
- OCI_ATTR_DATA_SIZE,errhp));
- ErrorProc(errhp,OCIAttrGet(colhp,OCI_DTYPE_PARAM,&coltype[i],
- 0,OCI_ATTR_DATA_TYPE,errhp));
- if(coltype[i]==SQLT_DAT)
- collen[i]=30;
- colbuf[i]=(text*)new text [(int)collen[i]+1];
- }
- for(i=0;i<(int)col_num;i++)
- {
- if(status=OCIDefineByPos(stmthp,&defhp[i],
- errhp,i+1,(ub1*)colbuf[i],collen[i]+1,SQLT_STR,&ind[i],0,0,OCI_DEFAULT))
- {
- ErrorProc(errhp,status);
- return;
- }
- }
-
- int row=0;
- while((OCIStmtFetch(stmthp,errhp,1,OCI_FETCH_NEXT,OCI_DEFAULT))!=OCI_NO_DATA)
- {
- for(i=0;i<(int)col_num;i++)
- {
- ColVal[row][i]="";
- if(ind[i]==-1)
- ColVal[row][i]="";
- else
- ColVal[row][i]=colbuf[i];
- }
- row=row+1;
- }
- for(i=0;i<(int)col_num;i++)
- delete colbuf[i];
-
- m_listCtrl.DeleteAllItems();
- for(i=0;i<row;i++)
- {
- m_listCtrl.InsertItem(LVIF_TEXT|LVIF_STATE,i,ColVal[i][0],
- (i%2)==0?LVIS_SELECTED:0,LVIS_SELECTED,0,0);
- for(int j=1;j<(int)col_num;j++)
- m_listCtrl.SetItemText(i,j,ColVal[i][j]);
- }
- delete textConVal;
- }
- void CCOCIExampleDlg::OnButDelete()
- {
- // TODO: Add your control notification handler code here
- UpdateData(TRUE);
- sword status;
- text textSQL[1024];
- wsprintf((char*)textSQL,"DELETE FROM %s WHERE %s %s :DelFieldVal",TableName,
- m_strFieldName,m_strCondition);
- if(status=OCIStmtPrepare(stmthp,errhp,textSQL,strlen((char*)textSQL), //没有执行!
- OCI_NTV_SYNTAX,OCI_DEFAULT))
- {
- ErrorProc(errhp,status);
- return;
- }
- text textColVal[20];
- sb4 len;
- int strlength=m_strFieldVal.GetLength();
- wsprintf((char*)textColVal,"%s",m_strFieldVal);
- textColVal[strlength]=' ';
- len=strlen((const char *)textColVal)+1;
- if(status=OCIBindByName(stmthp,&bidhp[0],errhp,(text *) ":DelFieldVal",
- -1,(ub1 *)&textColVal,len,SQLT_STR,0,(ub2*)0,(ub2*)0,(ub4)0,(ub4*)0,OCI_DEFAULT))
- {
- ErrorProc(errhp,status);
- return;
- }
- if(status=OCIStmtExecute(svchp,stmthp,errhp,(ub4)1,0,NULL,NULL,OCI_DEFAULT))
- {
- ErrorProc(errhp,status);
- return;
- }
- OnBTableselectok();
-
- }
- void CCOCIExampleDlg::OnButUpdate()
- {
- // TODO: Add your control notification handler code here
- UpdateData(TRUE);
- sword status;
- text textSQL[1024];
- wsprintf((char*)textSQL,"UPDATE %s SET %s =:UpDFieldVal WHERE %s =:KeyFieldVal",
- TableName,m_strFieldName,m_strConFName);
- if(status=OCIStmtPrepare(stmthp,errhp,textSQL,strlen((char *)textSQL),
- OCI_NTV_SYNTAX,OCI_DEFAULT))
- {
- ErrorProc(errhp,status);
- return;
- }
- text textColVal[20];
- sb4 len;
- int strlength=m_strFieldVal.GetLength();
- wsprintf((char*)textColVal,"%s",m_strFieldVal);
- textColVal[strlength]=' ';
- len=strlen((const char *)textColVal)+1;
-
- if(status=OCIBindByName(stmthp,&bidhp[0],errhp,(text*)":UpDFieldVal",-1,
- (ub1*)&textColVal,len,SQLT_STR,0,0,0,0,0,OCI_DEFAULT))
- {
- ErrorProc(errhp,status);
- return;
- }
- text textConColVal[20];
- strlength=m_strConFName.GetLength();
- wsprintf((char*)textConColVal,"%s",m_strConFVal);
- textConColVal[strlength]=' ';
- len=strlen((const char*)textConColVal)+1;
- if(status=OCIBindByName(stmthp,&bidhp[1],errhp,(text*)":KeyFieldVal",-1,
- (ub1*)&textConColVal,len,SQLT_STR,0,0,0,0,0,OCI_DEFAULT))
- {
- ErrorProc(errhp,status);
- return;
- }
- if(status=OCIStmtExecute(svchp,stmthp,errhp,(ub4)1,0,NULL,NULL,OCI_DEFAULT))
- {
- ErrorProc(errhp,status);
- return;
- }
-
- status=OCITransCommit(svchp,errhp,(ub4)0);
- OnBTableselectok();
-
- }