TestAdoDlg.cpp
上传用户:weisheen
上传日期:2022-07-09
资源大小:19390k
文件大小:7k
源码类别:
ActiveX/DCOM/ATL
开发平台:
Visual C++
- // TestAdoDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "TestAdo.h"
- #include "TestAdoDlg.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()
- /////////////////////////////////////////////////////////////////////////////
- // CTestAdoDlg dialog
- CTestAdoDlg::CTestAdoDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CTestAdoDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CTestAdoDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CTestAdoDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CTestAdoDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CTestAdoDlg, CDialog)
- //{{AFX_MSG_MAP(CTestAdoDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
- ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
- ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
- ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTestAdoDlg message handlers
- BOOL CTestAdoDlg::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 CTestAdoDlg::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 CTestAdoDlg::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 CTestAdoDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- //#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
- void CTestAdoDlg::OnButton1()
- {
- ::CoInitialize(NULL);
- _ConnectionPtr myconn; //连接智能指针
- myconn.CreateInstance(__uuidof(Connection));//生成实例
- _bstr_t sql("Provider=OraOLEDB.Oracle.1;User Id=scott;Data Source=tarena;");
- HRESULT hr=myconn->Open(sql,"scott","tiger",-1);
- //定义一个sql语句对象;
- _CommandPtr mysql1; //命令智能指针
- mysql1.CreateInstance(__uuidof(Command));//生成实例
- _bstr_t sql1("select * from ocyBooks");
- mysql1->CommandText=sql1;//指定命令文本
- mysql1->CommandType=adCmdText;//指定命令类型
- //以下是新增的代码:
- //制定SQL所查询的数据库;
- mysql1->ActiveConnection=myconn;
- //查询并返回存放在表中的结果集
- _RecordsetPtr mydata=mysql1->Execute(NULL,NULL,adCmdText);//数据操作
- while(!mydata->adoEOF)//列表指针
- {
- _variant_t myid=mydata->GetCollect("BOOKISNB");//获取数据表对应属性名的数据
- _variant_t myname=mydata->GetCollect("BOOKNAME");//获取数据表对应属性名的数据
- _variant_t myauthor=mydata->GetCollect("BOOKAuthor");//获取数据表对应属性名的数据
- CString strName((LPCTSTR)(_bstr_t)myname);//(_bstr_t)为带空字节头的UNicode码
- CString strAuthor((LPCTSTR)(_bstr_t)myauthor);//(LPCTSTR)为不带空字节头的UNicode码
- CString str;
- str.Format("%d;%s;%s",(long)myid,strName,strAuthor);
- MessageBox(str);
- mydata->MoveNext();
- }
- mydata->Close();
- myconn->Close();
- }
- #include <atlbase.h>
- void CTestAdoDlg::OnButton2()
- {
- // TODO: Add your control notification handler code here
- ::CoInitialize(NULL);
- CComPtr<_Connection> mytools;
- mytools.CoCreateInstance(__uuidof(Connection));
- _bstr_t sql("Provider=OraOLEDB.Oracle.1;User Id=scott;Data Source=tarena;");
- HRESULT hr=mytools->Open(sql,"scott","tiger",-1);//建立数据源连接
- mytools->Close();
- }
- void CTestAdoDlg::OnButton3()
- {
- // TODO: Add your control notification handler code here
- ::CoInitialize(NULL);
- _Connection* mytools=NULL;
- CoCreateInstance(__uuidof(Connection),
- NULL,
- CLSCTX_INPROC_SERVER,
- __uuidof(_Connection),
- (void **)&mytools);
- _bstr_t sql("Provider=OraOLEDB.Oracle.1;User Id=scott;Data Source=tarena;");
- HRESULT hr=mytools->Open(sql,"scott","tiger",-1);//建立数据源连接
- mytools->Close();
- }
- void CTestAdoDlg::OnButton4()
- {
- // TODO: Add your control notification handler code here
- ::CoInitialize(NULL);
- //_bstr_t sql1("insert into ocyBooks (BookISNB, BookName, BookAuthor) values (123457,'COM','WANGWU')");
- //_bstr_t sql1("delete from ocyBooks");
- _bstr_t sql1("update ocyBooks set BookName='C++' where BookName='COM'");
- _ConnectionPtr myconn;//连接智能指针
- myconn.CreateInstance(__uuidof(Connection));//生成实例
- _bstr_t sql("Provider=OraOLEDB.Oracle.1;User Id=scott;Data Source=tarena;");
- HRESULT hr=myconn->Open(sql,"scott","tiger",-1);//建立数据源连接
- _CommandPtr cmdsql1;//命令智能指针
- cmdsql1.CreateInstance(__uuidof(Command));//生成实例
- cmdsql1->CommandType=adCmdText;//指定命令类型
- cmdsql1->CommandText=sql1;//指定命令文本
- //制定SQL所查询的数据库;
- cmdsql1->ActiveConnection=myconn;
- VARIANT iResult;
- iResult.vt=VT_I4;//数字类型
- cmdsql1->Execute(&iResult,NULL,adCmdText);//功能和Recordset的一样
- }