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

📄 chapq2_1view.cpp

📁 完整的数据库课程设计
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// chapq2_1View.cpp : implementation of the CChapq2_1View class
//

#include "stdafx.h"
#include "chapq2_1.h"

#include "chapq2_1Doc.h"
#include "chapq2_1View.h"
#include "column.h"
#include "columns.h"
#include "Public.h"

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

/////////////////////////////////////////////////////////////////////////////
// CChapq2_1View

IMPLEMENT_DYNCREATE(CChapq2_1View, CFormView)

BEGIN_MESSAGE_MAP(CChapq2_1View, CFormView)
	//{{AFX_MSG_MAP(CChapq2_1View)
	ON_BN_CLICKED(IDC_SOURCE, OnSource)
	ON_BN_CLICKED(IDC_CONNCET, OnConncet)
	ON_BN_CLICKED(IDC_EXECUTE, OnExecute)
	ON_BN_CLICKED(IDC_DISCONNECT, OnDisconnect)
	ON_BN_CLICKED(IDC_QUIT, OnQuit)
	ON_LBN_SELCHANGE(IDC_LIST, OnSelchangeList)
	ON_BN_CLICKED(IDC_SCHOOL, OnSchool)
	ON_BN_CLICKED(IDC_GRADUATION, OnGraduation)
	ON_BN_CLICKED(IDC_SUSPENSION, OnSuspension)
	ON_BN_CLICKED(IDC_CHANGING, OnChanging)
	ON_BN_CLICKED(IDC_FIND, OnFind)
	ON_BN_CLICKED(IDC_FIND_CLEAR, OnFindClear)
	ON_BN_CLICKED(IDC_STUDENT, OnStudent)
	ON_BN_CLICKED(IDC_COURSE, OnCourse)
	ON_BN_CLICKED(IDC_SC, OnSc)
	ON_BN_CLICKED(IDC_INSERT, OnInsert)
	ON_BN_CLICKED(IDC_INSERT_CLEAR, OnInsertClear)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	ON_BN_CLICKED(IDC_DELETE_CLEAR, OnDeleteClear)
	ON_BN_CLICKED(IDC_LAW, OnLaw)
	ON_BN_CLICKED(IDC_LAW_CLEAR, OnLawClear)
	ON_BN_CLICKED(IDC_BACKUP, OnBackup)
	ON_BN_CLICKED(IDD_STUDENT, OnStudent)
	ON_BN_CLICKED(IDD_COURSE, OnCourse)
	ON_BN_CLICKED(IDD_SC, OnSc)
	ON_BN_CLICKED(IDC_RESTORE, OnRestore)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChapq2_1View construction/destruction

CChapq2_1View::CChapq2_1View()
	: CFormView(CChapq2_1View::IDD)
{
	//{{AFX_DATA_INIT(CChapq2_1View)
	m_strConnection = _T("");
	m_strSQL = _T("");
	m_numcha = _T("");
	m_namecha = _T("");
	m_numdel = _T("");
	m_namedel = _T("");
	m_cnodel = _T("");
	m_strTableName=_T(""); //林
	isDesc=FALSE;          //林
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CChapq2_1View::~CChapq2_1View()
{
}

void CChapq2_1View::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChapq2_1View)
	DDX_Control(pDX, IDC_LIST, m_ListBox);
	DDX_Control(pDX, IDC_DATAGRID, m_DataGrid);
	DDX_Text(pDX, IDC_CONNECTION_STRING, m_strConnection);
	DDX_Text(pDX, IDC_EXECUTE_STRING, m_strSQL);
	DDX_Text(pDX, IDC_SNO_QUIRY, m_numcha);
	DDX_Text(pDX, IDC_SNAME_QUIRY, m_namecha);
	DDX_Text(pDX, IDC_SNO_DELETE, m_numdel);
	DDX_Text(pDX, IDC_SNAME_DELETE, m_namedel);
	DDX_Text(pDX, IDC_CNO_DELETE, m_cnodel);
	//}}AFX_DATA_MAP
}

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

	return CFormView::PreCreateWindow(cs);
}

void CChapq2_1View::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	CPublic *app = (CPublic *)AfxGetApp();
		if(	app->IS==FALSE)
	{
		Judge();
	}
}

/////////////////////////////////////////////////////////////////////////////
// CChapq2_1View printing

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

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

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

void CChapq2_1View::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CChapq2_1View diagnostics

#ifdef _DEBUG
void CChapq2_1View::AssertValid() const
{
	CFormView::AssertValid();
}

void CChapq2_1View::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CChapq2_1View message handlers

void CChapq2_1View::GenerateError(HRESULT hr, PWSTR pwszDescription) //林
{
    CString m_strError;
	m_strError.Format("Run-time error'%d(%X)'",hr,hr);
	m_strError+=pwszDescription;
	AfxMessageBox(m_strError);
}

void CChapq2_1View::OnSource()  //林 按钮“数据源”
{
	// TODO: Add your control notification handler code here
	HRESULT hr;
	IDataSourceLocatorPtr m_dlPrompt=NULL;
	_ConnectionPtr m_Conn=NULL;
	::CoInitialize(NULL);
	CLoginDialog dlg1;
	hr=m_dlPrompt.CreateInstance(__uuidof(DataLinks));
	m_Conn=m_dlPrompt->PromptNew();
	if(m_Conn!=NULL)
	{
		m_strConnection.Format("%s",(char*)m_Conn->ConnectionString);
		UpdateData(FALSE);
    }
}

void CChapq2_1View::getTables()  //林 得到数据库中的数据表
{
    _bstr_t tablesName;
	CString kooky;
	m_ListBox.ResetContent();
	UpdateData(TRUE);
	try
	{
	   m_Recordset.CreateInstance(__uuidof(Recordset));
	   m_Recordset=m_Connection->OpenSchema(adSchemaTables,vtMissing,vtMissing);
	   while(!m_Recordset->adoEOF)
	   {
		  tablesName=m_Recordset->GetCollect("TABLE_NAME");
		  kooky=(char*)tablesName;
		  if(kooky.Left(4)!="MSys")
              m_ListBox.AddString(kooky);
		  m_Recordset->MoveNext();
	   }
	}
	catch(_com_error &e)
	{
		GenerateError(e.Error(),e.Description());
	}
	catch(...){}
	UpdateData(FALSE);
    m_Recordset=NULL;
}

void CChapq2_1View::OnConncet()   //林  按钮“连接”
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
	try
	{
		m_Connection.CreateInstance(__uuidof(Connection));
        m_Connection->Open(_bstr_t(m_strConnection.GetBuffer(0)),"","",-1);
	}
	catch(_com_error &e)
	{
		GenerateError(e.Error(),e.Description());
	}
	catch(...){}
	UpdateData(FALSE);
	getTables();
}

void CChapq2_1View::OnExecute()  //林  按钮“操作”
{
	// TODO: Add your control notification handler code here
	try
	{
		m_Recordset.CreateInstance(__uuidof(Recordset));
		UpdateData(TRUE);
		m_Connection->CursorLocation=adUseClient;
        m_Recordset->Open(m_strSQL.GetBuffer(0),m_Connection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
	}
	catch(_com_error &e)
	{
		GenerateError(e.Error(),e.Description());
	}
	catch(...){}
	m_DataGrid.SetRefDataSource((LPUNKNOWN)m_Recordset);
    m_DataGrid.Refresh();
    UpdateData(FALSE);
    m_Recordset=NULL;
}

void CChapq2_1View::OnDisconnect()  //林   按钮“断开”
{
	// TODO: Add your control notification handler code here
	try
	{
		m_Connection->Close();
	}
    catch(_com_error &e)
	{
		GenerateError(e.Error(),e.Description());
	}
	catch(...){}
	m_ListBox.ResetContent();
	m_DataGrid.SetRefDataSource(NULL);
	m_DataGrid.Refresh();
	m_strSQL="";
    UpdateData(FALSE);
}

void CChapq2_1View::OnQuit()   //林   按钮“退出”
{
	// TODO: Add your control notification handler code here
	try
	{
		if(m_Recordset!=NULL)
			m_Recordset->Close();
		if(m_Connection!=NULL)
			m_Connection->Close();
	}
	catch(...){}
	PostMessage(WM_QUIT);
}

void CChapq2_1View::OnSelchangeList()  //林 选中列表框中的数据表
{
	// TODO: Add your control notification handler code here
	int index=m_ListBox.GetCurSel();
	m_ListBox.GetText(index,m_strTableName);
	m_strSQL="select * from ["+m_strTableName+"]";
	UpdateData(FALSE);
	OnExecute();
}

BEGIN_EVENTSINK_MAP(CChapq2_1View, CFormView)
    //{{AFX_EVENTSINK_MAP(CChapq2_1View)
	ON_EVENT(CChapq2_1View, IDC_DATAGRID, 216 /* HeadClick */, OnHeadClickDatagrid, VTS_I2)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CChapq2_1View::OnHeadClickDatagrid(short ColIndex) //林 点击排序
{
	// TODO: Add your control notification handler code here
	CColumns cols=m_DataGrid.GetColumns();  //先取得列集
	VARIANT index;
	index.intVal=ColIndex;
	index.vt=VT_I4;
	CColumn col=cols.GetItem(index); //再取得列集中的列,由VARIANT index变量指出列的索引号
	m_strTableName=col.GetCaption();
	if(m_Connection!=NULL)
	{
		if(m_strTableName.GetLength()!=0)
		{
			int nOrignalLength=m_strSQL.GetLength();
			CString strTmp="order by ["+m_strTableName+"]";

⌨️ 快捷键说明

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