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

📄 200432590201dlg.cpp

📁 采用VC++的语言将数据库技术和ADO技术结合
💻 CPP
字号:
// 200432590201Dlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CMy200432590201Dlg dialog

CMy200432590201Dlg::CMy200432590201Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy200432590201Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMy200432590201Dlg)
	m_strTablename = _T("");
	m_lstuID = 0;
	m_strstuname = _T("");
	m_lage = 0;
	m_lclass = 0;
	m_sstuID = 0;
	m_scourseID = 0;
	m_sscore = 0;
	m_courseID = 0;
	m_credit = 0;
	m_strcoursename = _T("");
	m_strteacher = _T("");
	m_id = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy200432590201Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy200432590201Dlg)
	DDX_Control(pDX, IDC_LIST1_TABLENAME, m_listTablename);
	DDX_LBString(pDX, IDC_LIST1_TABLENAME, m_strTablename);
	DDX_Text(pDX, IDC_EDIT_STUID, m_lstuID);
	DDX_Text(pDX, IDC_EDIT_STUNAME, m_strstuname);
	DDX_Text(pDX, IDC_EDIT_AGE, m_lage);
	DDX_Text(pDX, IDC_EDIT_CLASS, m_lclass);
	DDX_Text(pDX, IDC_EDIT_SSTUID, m_sstuID);
	DDX_Text(pDX, IDC_EDIT_SCOURSEID, m_scourseID);
	DDX_Text(pDX, IDC_EDIT_SSCORE, m_sscore);
	DDX_Text(pDX, IDC_EDIT_COUID, m_courseID);
	DDX_Text(pDX, IDC_EDIT_CREDIT, m_credit);
	DDX_Text(pDX, IDC_EDIT_NAME2, m_strcoursename);
	DDX_Text(pDX, IDC_EDIT_TEACHER, m_strteacher);
	DDX_Text(pDX, IDC_EDIT1, m_id);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy200432590201Dlg, CDialog)
	//{{AFX_MSG_MAP(CMy200432590201Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_LBN_SELCHANGE(IDC_LIST1_TABLENAME, OnSelchangeList1Tablename)
	ON_BN_CLICKED(IDC_BUTTON1_CONNECTDB, OnButton1Connectdb)
	ON_BN_CLICKED(IDC_B_TABLESELECTOK, OnBTableselectok)
	ON_BN_CLICKED(IDC_BUT_ADDRECORD, OnButAddrecord)
	ON_BN_CLICKED(IDC_BUT2_ADD, OnBut2Add)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON, OnButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy200432590201Dlg message handlers

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

void CMy200432590201Dlg::OnSelchangeList1Tablename() 
{
	// TODO: Add your control notification handler code here
	
}

void CMy200432590201Dlg::OnButton1Connectdb() 
{
	// TODO: Add your control notification handler code here
	//连接数据库
	//实例化m_pConnection
	m_pConnection.CreateInstance("ADODB.Connection");
	CString m_strConnection;
	CString DBName;
	//用打开文件对话框来获取我们要连接的数据库文件名以及数据库所在的路径
	CFileDialog dlg(TRUE,"*.mdb","201.mdb",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Access 文件 ( * .mdb) | *.mdb||",NULL);
	if(dlg.DoModal() == IDCANCEL)
		return;
	else
		DBName = dlg.GetPathName();

		//获取*.mdb的路径与文件名
	delete dlg;
	try
	{
		m_strConnection.Format("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = %s",DBName);
		m_pConnection ->Open((_bstr_t)m_strConnection,"","",adConnectUnspecified);

	}
	catch(_com_error e)//捕捉异常
	{
		AfxMessageBox("数据库连接失败!");
		return;
	}
	//获取表名并且把它加到列表框中
	m_listTablename.ResetContent();
	_RecordsetPtr rsTableName = NULL;
	bstr_t bstrTable;
	bstr_t bstrTableType;
	CString strTable,strTableType;
	try
	{
		rsTableName = m_pConnection ->OpenSchema(adSchemaTables);
		while(!rsTableName ->adoEOF) 
		{
			bstrTable = (_bstr_t)rsTableName ->Fields ->Item["TABLE_NAME"] ->Value;
			bstrTableType = (_bstr_t)rsTableName ->Fields ->Item["TABLE_TYPE"] ->Value;
			strTable = bstrTable.copy();
			strTableType=bstrTableType.copy();
			if(strTableType == "TABLE")
				m_listTablename.AddString(strTable);
			rsTableName ->MoveNext();
		}
	}
	catch(_com_error ce)
	{
		AfxMessageBox("读取数据库表出错");
	}
}

void CMy200432590201Dlg::OnBTableselectok() 
{
	// TODO: Add your control notification handler code here
	if(m_pConnection == NULL)
		return;
	CString TableName;
	int item;
	item = m_listTablename.GetCurSel();
	//获取控件2所选中表名
	m_listTablename.GetText(item,TableName);
	CString SQLstr;
	//创建_RecordsetPtr对象
	_RecordsetPtr m_pRtemp;
	//c初始化m_pRTemp
	m_pRtemp.CreateInstance("ADODB.Recordset");
	//构造SQL语句
	SQLstr.Format("SELECT * FROM %s",TableName);
	//打开记录集
	HRESULT hr = m_pRtemp ->Open((_variant_t)SQLstr,_variant_t((IDispatch *)m_pConnection,true),adOpenDynamic,adLockPessimistic,adCmdText);
	if(SUCCEEDED(hr))
	{
		AfxMessageBox("表打开成功");
	}
	//创建FieldsPtr对象
	FieldsPtr p_fields;
	//创建FieldPtr对象
	FieldPtr p_field;
	p_fields = m_pRtemp ->Fields;
	_variant_t var_index;
	LPCSTR field_name;
	int index=0;
	_bstr_t bstr_field_name;
	//获取字段数目
	int countfields;
	countfields = p_fields->GetCount();
	CString *Column = new CString[countfields];
	CListCtrl *pList = (CListCtrl *)GetDlgItem(IDC_LIST_TABLEDATA);
	VERIFY(pList);
	pList ->DeleteAllItems();
	for(index=0;index < countfields;index++)
	{
		var_index.vt = VT_I4;
		var_index.lVal = index;
		p_field = p_fields ->Item[var_index];
		//获取字段名
		bstr_field_name = p_field ->GetName();
		field_name = (LPCSTR)bstr_field_name;
		Column[index] = field_name;
		//把获取的表所包含字段名加载到列表控件中
		int ColumnWidth = Column[index].GetLength()*15;
		pList ->InsertColumn(index,field_name,LVCFMT_CENTER,ColumnWidth);
	}
	//把表的数据加载到列表控件中
	int i=0;
	_bstr_t vCol;
	pList->SetTextBkColor(RGB(122,200,122));
	pList->SetTextColor(RGB(0,0,200));
	while(!m_pRtemp->adoEOF)
	{
		//i--记录序号
		pList ->InsertItem(i,"i");
		//i--字段序号
		for(int j=0;j<countfields;j++)
		{
			vCol = m_pRtemp->GetCollect((long)j);
			pList->SetItemText(i,j,vCol);
		}
		m_pRtemp ->MoveNext();
		i++;
		
	}

}

void CMy200432590201Dlg::OnButAddrecord() 
{
	// TODO: Add your control notification handler code here
	CString student;
	if(m_pConnection == NULL)
		return;
	UpdateData(TRUE);
	CString SQLstr;
	_variant_t RecordAffected;


	_RecordsetPtr m_pRecordset;
	m_pRecordset.CreateInstance("ADODB.Recordset");
	m_pRecordset->Open("SELECT * FROM student", _variant_t((IDispatch*)m_pConnection,true),
		adOpenDynamic,adLockOptimistic,adCmdText);

	//构造SQL语句
	if(m_strTablename=="student")
	{

	
			CString m_lstuIDstr,m_lagestr,m_lclassstr;
			m_lstuIDstr.Format(_T("%d"),m_lstuID);
			m_lagestr.Format(_T("%d"),m_lage);
			m_lclassstr.Format(_T("%d"),m_lclass);
			if(m_lstuIDstr == _T(""))
			{
				AfxMessageBox("主关键字不能为空!");
				return;
			}
			
			m_pRecordset->AddNew();
			m_pRecordset->PutCollect("stuID",static_cast<_variant_t>(m_lstuIDstr));
			m_pRecordset->PutCollect("stuname",static_cast<_variant_t>(m_strstuname));
			m_pRecordset->PutCollect("age",static_cast<_variant_t>(m_lagestr));
			m_pRecordset->PutCollect("class",static_cast<_variant_t>(m_lclassstr));
			m_pRecordset->Update();
	}

 
	OnBTableselectok();

}



void CMy200432590201Dlg::OnBut2Add() 
{
	// TODO: Add your control notification handler code here
	CString course;
	if(m_pConnection == NULL)
		return;
	UpdateData(TRUE);
	CString SQLstr;
	_variant_t RecordAffected;


	_RecordsetPtr m_pRecordset;
	m_pRecordset.CreateInstance("ADODB.Recordset");
	m_pRecordset->Open("SELECT * FROM course", _variant_t((IDispatch*)m_pConnection,true),
		adOpenDynamic,adLockOptimistic,adCmdText);


	if(m_strTablename=="course")
	{

	
			CString m_lcourseIDstr,m_lcreditstr;
			m_lcourseIDstr.Format(_T("%d"),m_courseID);
			m_lcreditstr.Format(_T("%d"),m_credit);
		
			if(m_lcourseIDstr == _T(""))
			{
				AfxMessageBox("主关键字不能为空!");
				return;
			}
			
			m_pRecordset->AddNew();
			m_pRecordset->PutCollect("courseID",static_cast<_variant_t>(m_lcourseIDstr));
			m_pRecordset->PutCollect("coursename",static_cast<_variant_t>(m_strcoursename));
    		m_pRecordset->PutCollect("teacher",static_cast<_variant_t>(m_strteacher));
			m_pRecordset->PutCollect("credit",static_cast<_variant_t>(m_lcreditstr));
		
			m_pRecordset->Update();
	}




	OnBTableselectok();
}

void CMy200432590201Dlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	CString score;
	if(m_pConnection == NULL)
		return;
	UpdateData(TRUE);
	CString SQLstr;
	_variant_t RecordAffected;


	_RecordsetPtr m_pRecordset;
	m_pRecordset.CreateInstance("ADODB.Recordset");
	m_pRecordset->Open("SELECT * FROM score", _variant_t((IDispatch*)m_pConnection,true),
		adOpenDynamic,adLockOptimistic,adCmdText);


	if(m_strTablename=="score")
	{

	
			CString m_lstuIDstr,m_lcourseIDstr,m_lscorestr;
			m_lstuIDstr.Format(_T("%d"),m_sstuID);
			m_lcourseIDstr.Format(_T("%d"),m_scourseID);
			m_lscorestr.Format(_T("%d"),m_sscore);
			
			
			m_pRecordset->AddNew();
			m_pRecordset->PutCollect("stuID",static_cast<_variant_t>(m_lstuIDstr));
			m_pRecordset->PutCollect("courseID",static_cast<_variant_t>(m_lcourseIDstr));
			m_pRecordset->PutCollect("score",static_cast<_variant_t>(m_lscorestr));
			m_pRecordset->Update();
	}
		OnBTableselectok();
}

void CMy200432590201Dlg::OnButton() 
{
	// TODO: Add your control notification handler code here
	if(m_pConnection == NULL)
		return;
	UpdateData(TRUE);
	CString SQLstr;
	_variant_t RecordAffected;
	
	_RecordsetPtr m_pRecordset;
	CString id;
	id.Format(_T("%d"),m_id);
	if(m_strTablename=="student")
		SQLstr.Format("DELETE FROM %s WHERE stuID=%d",m_strTablename,m_id);
	else if(m_strTablename=="course")
		SQLstr.Format("DELETE FROM %s WHERE courseID=%d",m_strTablename,m_id);
	else if(m_strTablename=="score")
		SQLstr.Format("DELETE FROM %s WHERE stuID=%d",m_strTablename,m_id);
	
	try
	{
		m_pConnection->Execute((_bstr_t)SQLstr,&RecordAffected,adCmdText);
		AfxMessageBox("删除记录成功");
	}
	catch(_com_error ce)
	{
		AfxMessageBox("删除记录出错");
	}
	OnBTableselectok();

}

⌨️ 快捷键说明

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