vC 与数据库操作——向ctrllist控件中插入数据的精华

类别:编程语言 点击:0 评论:0 推荐:

                       VC中ctrllist的使用与数据库链接

清浪冲天 马敬发 QQ: 283989349   E-mail: [email protected]

有问题时,我们可以通过http://www.hustc.mpc.cn 这个论坛,

或 通过QQ 共同学习和讨论


 

// DIALOG_TRAINCOURSE.cpp : implementation file
//

#include "stdafx.h"
#include "train.h"
#include "DIALOG_TRAINCOURSE.h"
#include "CourseInfo.h"
#include "DIALOG_APPISE.h"
#include "CourseSet.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDIALOG_TRAINCOURSE dialog


CDIALOG_TRAINCOURSE::CDIALOG_TRAINCOURSE(CWnd* pParent /*=NULL*/)
 : CDialog(CDIALOG_TRAINCOURSE::IDD, pParent)
{
 //{{AFX_DATA_INIT(CDIALOG_TRAINCOURSE)
  // NOTE: the ClassWizard will add member initialization here
 //}}AFX_DATA_INIT
}


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


BEGIN_MESSAGE_MAP(CDIALOG_TRAINCOURSE, CDialog)
 //{{AFX_MSG_MAP(CDIALOG_TRAINCOURSE)
 ON_BN_CLICKED(IDC_BUTTONADD, OnButtonadd)
 ON_BN_CLICKED(IDC_BUTTONDEL, OnButtondel)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDIALOG_TRAINCOURSE message handlers

void CDIALOG_TRAINCOURSE::OnButtonadd()
{
 // TODO: Add your control notification handler code here
 m_database.Close();
 CCourseInfo m_courseinfo;
 m_courseinfo.m_database.Open(_T("train"));
 m_courseinfo.DoModal();
 RefreshData();
}

void CDIALOG_TRAINCOURSE::OnOK()
{
 // TODO: Add extra validation here
 CDIALOG_APPISE m_apprisemanager;
 int i=m_content.GetSelectionMark();
 CString strSQL;
 if(i==-1){
  MessageBox("please choose a course first!");
 }
 else{
  m_apprisemanager.course_id=atoi(m_content.GetItemText(i,0));
  m_database.Close();
  m_apprisemanager.m_database.Open(_T("train"));
  m_apprisemanager.DoModal();
 }

}

BOOL CDIALOG_TRAINCOURSE::OnInitDialog()
{
 CDialog::OnInitDialog();
 
 // TODO: Add extra initialization here
 m_content.InsertColumn(0,"课程号");
 m_content.InsertColumn(1,"课程类别");
 m_content.InsertColumn(2,"课程中文名称");
 m_content.InsertColumn(3,"课程英文名称");
 m_content.InsertColumn(4,"课程描述");
 m_content.InsertColumn(5,"课程学时");
 m_content.InsertColumn(6,"教材");
 m_content.InsertColumn(7,"等效课程");
 m_content.InsertColumn(8,"预修课程");
 m_content.InsertColumn(9,"初训/复训标志");
 m_content.InsertColumn(10,"开课部门");
 RECT rect;
 m_content.GetWindowRect(&rect);
 int wid = rect.right - rect.left;
 m_content.SetColumnWidth(0,wid/11);
 m_content.SetColumnWidth(1,wid/11);
 m_content.SetColumnWidth(2,wid/11);
 m_content.SetColumnWidth(3,wid/11);
 m_content.SetColumnWidth(4,wid/11);
 m_content.SetColumnWidth(5,wid/11);
 m_content.SetColumnWidth(6,wid/11);
 m_content.SetColumnWidth(7,wid/11);
 m_content.SetColumnWidth(8,wid/11);
 m_content.SetColumnWidth(9,wid/11);
 m_content.SetColumnWidth(10,wid/11);
 m_content.SetExtendedStyle(LVS_EX_FULLROWSELECT);
 RefreshData(); 
 
 return TRUE;  // return TRUE unless you set the focus to a control
               // EXCEPTION: OCX Property Pages should return FALSE
}

void CDIALOG_TRAINCOURSE::RefreshData()
{
 if(!m_database.IsOpen()){
  m_database.Open(_T("train"));
 }
 m_content.DeleteAllItems();
 CCourseSet m_recordset(&m_database);
 CString strSQL;
 strSQL.Format("select * from COURSE");
 m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL);
 CDBVariant varValue;
 if(m_recordset.GetRecordCount()!=0) m_recordset.MoveFirst();
 char buf[20];
 int i=0;
 while(!m_recordset.IsEOF())
 {
  int temp=0;
  m_recordset.GetFieldValue(temp,varValue);
  sprintf(buf,"%d",varValue.m_iVal); m_content.InsertItem(i,buf);
  m_recordset.GetFieldValue(1,varValue);
  m_content.SetItemText(i,1,varValue.m_pstring->GetBuffer(1));
  m_recordset.GetFieldValue(2,varValue);
  m_content.SetItemText(i,2,varValue.m_pstring->GetBuffer(1));
  m_recordset.GetFieldValue(3,varValue);
  m_content.SetItemText(i,3,varValue.m_pstring->GetBuffer(1));
  m_recordset.GetFieldValue(4,varValue);
  m_content.SetItemText(i,4,varValue.m_pstring->GetBuffer(1));
  m_recordset.GetFieldValue(5,varValue);
  sprintf(buf,"%d",varValue.m_iVal); m_content.SetItemText(i,5,buf);
  m_recordset.GetFieldValue(6,varValue);
  m_content.SetItemText(i,6,varValue.m_pstring->GetBuffer(1));
  m_recordset.GetFieldValue(7,varValue);
  sprintf(buf,"%d",varValue.m_iVal); m_content.SetItemText(i,7,buf);
  m_recordset.GetFieldValue(8,varValue);
  sprintf(buf,"%d",varValue.m_iVal); m_content.SetItemText(i,8,buf);
  m_recordset.GetFieldValue(9,varValue);
  sprintf(buf,"%d",varValue.m_chVal); m_content.SetItemText(i,9,buf);
  m_recordset.GetFieldValue(10,varValue);
  sprintf(buf,"%d",varValue.m_iVal); m_content.SetItemText(i,10,buf);
  m_recordset.MoveNext();
  i++;
 }
}

void CDIALOG_TRAINCOURSE::OnButtondel()
{
 // TODO: Add your control notification handler code here
 int i=m_content.GetSelectionMark();
 CString strSQL;
 if(i==-1){
  MessageBox("please choose a record first!");
 }
 else{
  int keyid=atoi(m_content.GetItemText(i,0));
  strSQL.Format("delete from COURSE where COURSE_ID=%d",keyid);
  m_database.ExecuteSQL(strSQL);
  strSQL.Format("delete from COURSE_EVALUATION where COURSE_ID=%d",keyid);
  m_database.ExecuteSQL(strSQL);
  m_database.Close();
  RefreshData();
 } 
}

void CDIALOG_TRAINCOURSE::OnCancel()
{
 // TODO: Add extra cleanup here
 m_database.Close();
 CDialog::OnCancel();
}

本文地址:http://com.8s8s.com/it/it27830.htm