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

📄 adodlg.cpp

📁 利用了ADO技术实现了对数据库的基本操作啊
💻 CPP
字号:
// ADODlg.cpp : implementation file
//

#include "stdafx.h"
#include "ADO.h"
#include "ADODlg.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()

/////////////////////////////////////////////////////////////////////////////
// CADODlg dialog

CADODlg::CADODlg(CWnd* pParent /*=NULL*/)
	: CDialog(CADODlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CADODlg)
		// 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 CADODlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CADODlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CADODlg, CDialog)
	//{{AFX_MSG_MAP(CADODlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDB_CREATE, OnCreate)
	ON_BN_CLICKED(IDB_DELETE, OnDelete)
	ON_BN_CLICKED(IDB_QUERY, OnQuery)
	ON_BN_CLICKED(IDB_INSERT, OnInsert)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CADODlg message handlers

BOOL CADODlg::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
	
	HRESULT  hr ;
	hr = m_pCommand.CreateInstance("ADODB.Command");
	if(FAILED(hr))
	{
		::AfxMessageBox("m_pCommamnd.CreateInstance failed!");
		return FALSE;
	}

	try
	{
		hr = m_pConnection.CreateInstance("ADODB.Connection");
		if(SUCCEEDED(hr))
		{
			//连接Access2000的数据库
			//ConnectionTimeOut用来设置连接的超时时间,需要在Open之前调用
			m_pConnection->ConnectionTimeout = 10;
	//		hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\DB\\test.mdb","","",adModeUnknown);
            if(FAILED(hr))
			{
				::AfxMessageBox("Open failed!");
			}
			//连接SQL2000的数据库
			//直接连接没有利用DSN数据源
			m_pConnection->Open("driver={SQL Server};Server=127.0.0.1;DATABASE=MyDataBase;UID=;PWD=","","",adModeUnknown);
            //利用DSN数据源	
//	        m_pConnection->Open("Data Source=MYDB;UID=sa;PWD=;","","",adModeUnknown);
		}
	}
	catch(_com_error e)
	{
		CString errorMessage;
		errorMessage.Format("%s",e.ErrorMessage());
		AfxMessageBox(errorMessage);
		//	State属性指明当前Connection对象的状态,
		//	0表示关闭,1表示已经打开,我们可以通过读取这个属性来作相应的处理
		if(m_pConnection->State)
			m_pConnection->Close();
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CADODlg::OnCreate() 
{	
	//利用_ConnectionPtr执行SQL命令
	//如果DB中已经存在了表了,再重复进行Create就会出现一个Bug
   /*
	_variant_t RecordsAffected;
	CString strSql;
	strSql = "Create table Student(Name char(10),Age int)";
	this->m_pConnection->Execute(_bstr_t(strSql),&RecordsAffected,adCmdText);
	*/
}

void CADODlg::OnDelete() 
{	
}

void CADODlg::OnQuery() 
{	
	//利用_ConnectionPtr执行SQL命令
	/*
	_variant_t RecordsAffected;
	CString strSql;
	strSql = "select count(*) from Student";
	m_pRecordset = this->m_pConnection->Execute(_bstr_t(strSql),&RecordsAffected,adCmdText);
    _variant_t vindex = long(0);
	_variant_t vCount = this->m_pRecordset->GetCollect(vindex);
	long lindex = _variant_t (vCount);
	strSql.Format("Num:%d",lindex);
	MessageBox(strSql);
	*/
	//利用_CommandPtr 执行SQL命令
/*
	m_pCommand->ActiveConnection = this->m_pConnection;
	m_pCommand->CommandText = "Select count(*) from Student";
	m_pRecordset = this->m_pCommand->Execute(NULL,NULL,adCmdText);
	 _variant_t vindex = long(0);
	_variant_t vCount = this->m_pRecordset->GetCollect(vindex);
	CString strResult;
	long lindex = _variant_t (vCount);
	strResult.Format("Num:%d",lindex);
	MessageBox(strResult);
*/
}

void CADODlg::OnInsert() 
{
	//利用_ConnectionPtr执行SQL命令
	/*
	_variant_t RecordsAffected;
	CString strSql;
	strSql = "Insert into Student values('AB',21)";
	this->m_pConnection->Execute(_bstr_t(strSql),&RecordsAffected,adCmdText);
	strSql = "Insert into Student values('AC',22)";
	this->m_pConnection->Execute(_bstr_t(strSql),&RecordsAffected,adCmdText);
	*/
}

⌨️ 快捷键说明

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