⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adodbdlg.cpp

📁 基于COM的ADO数据库技术深入编程
💻 CPP
字号:
// AdodbDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Adodb.h"
#include "AdodbDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#import "c:\program files\common files\system\ado\msado15.dll" \
	no_namespace,rename ("EOF", "adoEOF"),named_guids


/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CAdodbDlg dialog

CAdodbDlg::CAdodbDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAdodbDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAdodbDlg)
		// NOTE: the ClassWizard will add member initialization here
	m_OpenType=0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAdodbDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAdodbDlg)
	DDX_Control(pDX, IDC_LIST1, m_list);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAdodbDlg, CDialog)
	//{{AFX_MSG_MAP(CAdodbDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOPEN, OnOpen)
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAdodbDlg message handlers

BOOL CAdodbDlg::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 CAdodbDlg::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 CAdodbDlg::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 CAdodbDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CAdodbDlg::OnOpen() 
{  
	if(m_OpenType==0){AfxMessageBox("请选择打开方式");return;}
	if(m_OpenType==1)
	{this->OpenByTpye1(); 
	}
	else
	{this->OpenByType2(); 
	}
}

void CAdodbDlg::OpenByTpye1()//智能指针方式
{
::CoInitialize(NULL); 
   m_list.ResetContent();
   //数据源
   _bstr_t varSource="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb";	 
   //数据源连接智能接口指针
   _ConnectionPtr	m_pConnection(_uuidof(Connection));
   //建立连接
   m_pConnection->Open(varSource,"","",adModeUnknown);
   _RecordsetPtr m_pSet(_uuidof(Recordset));//数据集智能指针
try	{ 
	          m_pSet->Open("SELECT * FROM student",//SQL 语句
		                    m_pConnection.GetInterfacePtr(),//数据源连接接口	 
		                    adOpenDynamic,//动态打开
							adLockOptimistic,//锁定方式
							adCmdText);//以文本命令方式打开
	}
	catch(_com_error *e)//错误处理
	{
		AfxMessageBox(e->ErrorMessage());
	}
  _variant_t	var;
	CString str="";
  long fldc=m_pSet->GetFields()->GetCount(); 
  long i=0;
    try    //获取所有的记录,并以字符形式显示
   {
	 m_pSet->MoveFirst();   
	 while(!m_pSet->adoEOF)
	   {str="";
		 for(i=0;i<fldc;i++)
		{    var=m_pSet->GetCollect((long)i);
			{  
		    var.ChangeType(VT_BSTR);
		    str+=var.bstrVal;
		    str+=" ";
		   }
		  } 
		 m_list.AddString(str);
		 m_pSet->MoveNext();
	   }
	}catch(_com_error *e)
	{AfxMessageBox(e->ErrorMessage());
	 delete e;
	}
m_pSet->MoveFirst(); 
::CoUninitialize(); //取消COM 初始化
}

void CAdodbDlg::OpenByType2()//实例化对象方式
{
::CoInitialize(NULL); 
_Recordset *m_pSet=NULL;//数据集接口指针
//数据源
_variant_t varSource="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb";	 
_variant_t varSql="Select * from student";//SQL语句
//实例化对象
HRESULT hr=::CoCreateInstance(CLSID_Recordset,NULL,CLSCTX_INPROC_SERVER,IID__Recordset,(void**)&m_pSet);
if(FAILED(hr))
{AfxMessageBox("CocreateInstance Failed!");
}
else{
	m_pSet->AddRef();//引用计数 
m_list.ResetContent();
try{ m_pSet->Open(varSql,//SQL 语句
		          varSource,//数据源连接接口	 
		          adOpenDynamic,//动态打开
				  adLockOptimistic,//锁定方式
				  adCmdText);//以文本命令方式打开
	}
 catch(_com_error *e)//错误处理
	{
  	AfxMessageBox(e->ErrorMessage());
	}
  _variant_t var;
	 CString str=""; 
  long fldc; 
   m_pSet->GetFields()->get_Count(&fldc) ; 
  long i=0;
    try    //获取所有的记录,并以字符形式显示
   {
	 m_pSet->MoveFirst();  
	 short bE;
	 m_pSet->get_adoEOF(&bE); 
	 while(!bE)
	   {str="";
		 for(i=0;i<fldc;i++)
		{  _variant_t index=i;
			 m_pSet->get_Collect(index,&var);//获取字段记录
			{ 
		    var.ChangeType(VT_BSTR);//转变成字符形式
		    str+=var.bstrVal;
		    str+=" ";
		   }
		  } 
		 m_list.AddString(str);
		 m_pSet->MoveNext();
         m_pSet->get_adoEOF(&bE); 
	   }
	}catch(_com_error *e)
	{AfxMessageBox(e->ErrorMessage());
	 delete e;
	}
  m_pSet->MoveFirst(); 
}
  m_pSet->Release(); //释放接口指针 
::CoUninitialize(); //取消COM 初始化

}

void CAdodbDlg::OnRadio1() 
{
 m_OpenType=1;	
}

void CAdodbDlg::OnRadio2() 
{
	m_OpenType=2;	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -