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

📄 myodbcview.cpp

📁 一个简单的数据库增加和删减的程序,完成对ACCESS数据库中数据添加和删除.
💻 CPP
字号:
// MyODBCView.cpp : implementation of the CMyODBCView class
//

#include "stdafx.h"
#include "MyODBC.h"

#include "MyODBCSet.h"
#include "MyODBCDoc.h"
#include "MyODBCView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyODBCView

IMPLEMENT_DYNCREATE(CMyODBCView, CRecordView)

BEGIN_MESSAGE_MAP(CMyODBCView, CRecordView)
	//{{AFX_MSG_MAP(CMyODBCView)
	ON_COMMAND(ID_RECORD_FIRST, OnRecordFirst)
	ON_COMMAND(ID_RECORD_LAST, OnRecordLast)
	ON_COMMAND(ID_RECORD_NEXT, OnRecordNext)
	ON_COMMAND(ID_RECORD_PREV, OnRecordPrev)
	ON_BN_CLICKED(IDC_RECORD_ADD, OnRecordAdd)
	ON_BN_CLICKED(IDC_RECORD_DEL, OnRecordDel)
	ON_BN_CLICKED(IDC_RECORD_EDIT, OnRecordEdit)
	ON_BN_CLICKED(IDC_RECORD_PLAY, OnRecordPlay)
	ON_BN_CLICKED(IDC_RECORD_QUERY, OnRecordQuery)
	ON_WM_DESTROY()
	ON_WM_HSCROLL()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyODBCView construction/destruction

CMyODBCView::CMyODBCView()
	: CRecordView(CMyODBCView::IDD)
{
	//{{AFX_DATA_INIT(CMyODBCView)
	m_pSet = NULL;
	m_ID = 0;
	m_name = _T("");
	m_position = _T("");
	m_singer = _T("");
	m_writer = _T("");
	m_sql = _T("");
	m_dight = _T("");
	//}}AFX_DATA_INIT


	m_set = NULL;
	m_DSOK = TRUE;
	m_Query = "Select * from Table1";

}

CMyODBCView::~CMyODBCView()
{
	if(m_pSet != NULL) delete m_set;
}

void CMyODBCView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyODBCView)
	DDX_Control(pDX, IDC_PROGRESS, m_Progress);
	DDX_Control(pDX, IDC_SLIDER, m_Slider);
	DDX_Text(pDX, IDC_EDIT_ID, m_ID);
	DDX_Text(pDX, IDC_EDIT_NAME, m_name);
	DDX_Text(pDX, IDC_EDIT_POSITION, m_position);
	DDX_Text(pDX, IDC_EDIT_SINGER, m_singer);
	DDX_Text(pDX, IDC_EDIT_WRITER, m_writer);
	DDX_Text(pDX, IDC_EDIT_SQL, m_sql);
	DDX_Text(pDX, IDC_DIGHT, m_dight);
	//}}AFX_DATA_MAP
}

BOOL CMyODBCView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CRecordView::PreCreateWindow(cs);
}

void CMyODBCView::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_myODBCSet;
	CRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	//创建并打开数据集
	try
	{
		m_pSet = new CMyODBCSet();
		m_pSet->Open();
		MyUpdateData();
	}
	catch(CDBException * pe)
	{
		AfxMessageBox(pe->m_strError);
		pe->Delete();
	}

	m_Progress.SetRange(0,100);
	m_Progress.SetPos(0);
	m_Progress.ShowWindow(SW_HIDE);

	m_Slider.SetRange(0,20);
	m_Slider.ShowWindow(SW_HIDE);


}

/////////////////////////////////////////////////////////////////////////////
// CMyODBCView printing

BOOL CMyODBCView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMyODBCView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMyODBCView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMyODBCView diagnostics

#ifdef _DEBUG
void CMyODBCView::AssertValid() const
{
	CRecordView::AssertValid();
}

void CMyODBCView::Dump(CDumpContext& dc) const
{
	CRecordView::Dump(dc);
}

CMyODBCDoc* CMyODBCView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyODBCDoc)));
	return (CMyODBCDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMyODBCView database support
CRecordset* CMyODBCView::OnGetRecordset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// CMyODBCView message handlers

void CMyODBCView::OnRecordFirst() 
{
	m_pSet->MoveFirst();
	MyUpdateData();
	
}

void CMyODBCView::OnRecordLast() 
{
	m_pSet->MoveLast();
	MyUpdateData();
	
}

void CMyODBCView::OnRecordNext() 
{
	m_pSet->MoveNext();
	MyUpdateData();
	
}

void CMyODBCView::OnRecordPrev() 
{
	m_pSet->MovePrev();
	MyUpdateData();
	
}

void CMyODBCView::OnRecordAdd() 
{
	try
	{
		m_pSet->AddNew();
		UpdateData(true);
		//m_pSet->m_ID = m_ID;
		m_pSet->m_name = m_name;
		m_pSet->m_singer = m_singer;
		m_pSet->m_writer = m_writer;
		m_pSet->m_position = m_position;
		m_pSet->Update();
		m_pSet->Requery();

		m_pSet->MoveLast();
		MyUpdateData();
	}
	catch(CDBException * pe)
	{
		AfxMessageBox(pe->m_strError);
		pe->Delete();
	}
	
}

void CMyODBCView::OnRecordDel() 
{
	try
	{
		m_pSet->Delete();
		m_pSet->Requery();
		MyUpdateData();
	}
	catch(CDBException * pe)
	{
		AfxMessageBox(pe->m_strError);
		pe->Delete();
	}
	
}

void CMyODBCView::OnRecordEdit() 
{
	try
	{
		m_pSet->Edit();
		UpdateData(true);
		m_pSet->m_ID = m_ID;
		m_pSet->m_name = m_name;
		m_pSet->m_singer = m_singer;
		m_pSet->m_writer = m_writer;
		m_pSet->m_position = m_position;
		m_pSet->Update();
	}
	catch(CDBException * pe)
	{
		AfxMessageBox(pe->m_strError);
		pe->Delete();
	}
	
}

void CMyODBCView::MyUpdateData()
{
	m_name = m_pSet->m_name;
	m_ID = m_pSet->m_ID;
	m_singer = m_pSet->m_singer;
	m_writer = m_pSet->m_writer;
	m_position = m_pSet->m_position;
	UpdateData(false);
}

void CMyODBCView::OnRecordPlay() 
{
		CString strFileName = m_position;
		char szFileName[_MAX_PATH];
		GetShortPathName((LPCSTR)strFileName,szFileName,_MAX_PATH);
		char szExt[8];
		_splitpath(( const char * )szFileName,NULL,NULL,NULL,szExt);

		MCIERROR mciError = 0;
		if( !stricmp(szExt,".wav"))
			mciError = Open(szFileName,"waveaudio"," ",m_hWnd);
		else if( !stricmp(szExt,".mid"))
			mciError = Open(szFileName,"sequencer"," ",m_hWnd);
		else if( !stricmp(szExt,".avi"))
			mciError = Open(szFileName,"avivideo","overlapped",m_hWnd);


		m_Progress.ShowWindow(SW_SHOW);
		m_Slider.ShowWindow(SW_SHOW);
		Play();
		SetTimer(100,500,NULL);


/*	
*/
	
}

void CMyODBCView::OnRecordQuery() 
{
	UpdateData(true);
	CString m_Statement = m_sql;
	if( m_Statement == ""){AfxMessageBox("没有SQL语句!");return;}

	if(m_pSet->IsOpen())
	{
		m_pSet->Close();	
	}
	CString OldStr;
	OldStr = m_Query;

	TRY
	{	m_Statement.MakeUpper();
		if(m_Statement.Find("SELECT") == -1)
		{
			m_pSet->m_pDatabase->ExecuteSQL(m_Statement);
		}
		else
		{
			m_Query = m_Statement;
		}

		if(!m_pSet->IsOpen())
		{
			m_pSet->Open(CRecordset::dynaset,m_Query);
		}
	}

	CATCH_ALL(e)
	{
		TCHAR Err[255];
		e->GetErrorMessage(Err,255);
		AfxMessageBox(Err);
		if(!m_pSet->IsOpen())
		{
			m_pSet->Open(CRecordset::dynaset,OldStr);
			m_Query = OldStr;
			m_Statement = OldStr;
		}
	}
	
	END_CATCH_ALL

		
//	ReadDisplayFields();


	if(m_pSet->IsEOF())
	{
		AfxMessageBox("当前视图没有记录!");
		return;
	}
	m_pSet->MoveFirst();


//	ReadDisplayRecord();


	UpdateData(false);
	
}

void CMyODBCView::OnDestroy() 
{
	CRecordView::OnDestroy();
	
/*	if(m_DSOK == FALSE)
	{
		return;
	}

	if(m_Db.IsOpen())
	{
		m_Db.Close();
	}
*/
	if(m_pSet->IsOpen())
	{
		m_pSet->Close();
	}
	
}

void CMyODBCView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	CString strMode = GetMode();
	if(! strMode.CompareNoCase("playing"))
		Stop();
	int nSPos = m_Slider.GetPos();
	long lLength = GetLength();
	long lMPos = (long)((double)nSPos*lLength/20);
	char szStartPos[256];
	lMPos = max(GetStartPosition(szStartPos),lMPos);
	lMPos = min(lLength,lMPos);
	Seek(lMPos);
	if(! strMode.CompareNoCase("playing"))
		Play();
	
	CRecordView::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CMyODBCView::OnTimer(UINT nIDEvent) 
{
	CString strMode= GetMode();
	if(! strMode.CompareNoCase("stopped"))
		{
			GetDlgItem(IDC_RECORD_PLAY)->ShowWindow(true);
		}
		else if(! strMode.CompareNoCase("not ready"))
		{
			GetDlgItem(IDC_RECORD_PLAY)->ShowWindow(false);
		}
		char szPos[256];
		m_Slider.SetPos((int)(20*GetPosition(szPos)/GetLength()));
		m_Progress.SetPos((int)(100*GetPosition(szPos)/GetLength()));
		m_dight = itoa((100*GetPosition(szPos)/GetLength()),szPos,10);
		m_dight +="%";
		UpdateData(false);
			
	CRecordView::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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