这是stdafx.h头文件:
// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//#if !defined(AFX_STDAFX_H__54996195_168B_4648_A8FB_240BD5BF4C66__INCLUDED_)
#define AFX_STDAFX_H__54996195_168B_4648_A8FB_240BD5BF4C66__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxdisp.h>        // MFC Automation classes
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT//将msado15.dll动态链接库导入到程序中 通过ADO连接数据库
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF")//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_STDAFX_H__54996195_168B_4648_A8FB_240BD5BF4C66__INCLUDED_)
这是头文件zhuceDlg.h :
#if !defined(AFX_ZHUCEDLG_H__3EE75B9F_917C_4566_B4E5_71C0CDE01A0E__INCLUDED_)
#define AFX_ZHUCEDLG_H__3EE75B9F_917C_4566_B4E5_71C0CDE01A0E__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// zhuceDlg.h : header file
///////////////////////////////////////////////////////////////////////////////
// CzhuceDlg dialogclass CzhuceDlg : public CDialog
{
// Construction
public:
CzhuceDlg(CWnd* pParent = NULL);   // standard constructor// Dialog Data
//{{AFX_DATA(CLoginDlg)
enum { IDD = IDD_DIALOG_zhuce };
CString m_sEdit1;
CString m_sEdit2;
CString m_sEdit3;
//}}AFX_DATApublic:
_ConnectionPtr m_pConnecticon;//声明一个连接对象指针
_RecordsetPtr m_pRecordset;//声明一个命令对象指针BOOL OnInitADOConn();//初始化COM并连接数据库
void ExitConnect();//退出连接数据库// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CzhuceDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
//}}AFX_VIRTUAL// Implementation
protected:// Generated message map functions
//{{AFX_MSG(CzhuceDlg)
afx_msg void OnOK();
virtual BOOL OnInitDialog();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_ZHUCEDLG_H__3EE75B9F_917C_4566_B4E5_71C0CDE01A0E__INCLUDED_)zhuceDlg.cpp文件:
// zhuceDlg.cpp : implementation file
//#include "stdafx.h"
#include "StudentySelectClass.h"
#include "zhuceDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CzhuceDlg dialog
CzhuceDlg::CzhuceDlg(CWnd* pParent /*=NULL*/)
: CDialog(CzhuceDlg::IDD, pParent){
//{{AFX_DATA_INIT(CLoginDlg)
m_sEdit1 = _T("");
m_sEdit2 = _T("");
m_sEdit3 = _T("");
//}}AFX_DATA_INIT
}
void CzhuceDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLoginDlg)DDX_Text(pDX, IDC_EDIT1, m_sEdit1);
DDX_Text(pDX, IDC_EDIT2, m_sEdit2);
DDX_Text(pDX, IDC_EDIT3, m_sEdit3);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CzhuceDlg, CDialog)
//{{AFX_MSG_MAP(CzhuceDlg)
ON_BN_CLICKED(IDOK, OnOK)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CzhuceDlg message handlersBOOL CzhuceDlg::OnInitDialog() 
{
CDialog::OnInitDialog();UpdateData(FALSE);
return TRUE;  // return TRUE unless you set the focus to a control
              // EXCEPTION: OCX Property Pages should return FALSE
}//初始化COM并连接数据库
BOOL CzhuceDlg::OnInitADOConn()
{
//初始化OLE/COM库环境
::CoInitialize(NULL);try
{
//创建连接对象实例
m_pConnecticon.CreateInstance("ADODB.Connection");
m_pConnecticon->Open((_bstr_t)theApp.m_sConnectDbString,"","",adConnectUnspecified);
MessageBox("初始化成功!","信息:",MB_OK);
}
catch(_com_error e)
{
MessageBox("初始化失败!","信息:",MB_OK);
CDialog::OnOK();
return FALSE;}
return TRUE;
}//退出连接数据库
void CzhuceDlg::ExitConnect()
{
//关闭记录集和连接
if(m_pRecordset!=NULL)
{
m_pRecordset->Close();
m_pRecordset = NULL;
}
m_pConnecticon->Close();
m_pConnecticon = NULL;
}
void CzhuceDlg::OnOK() 
{
// TODO: Add extra validation here
UpdateData(TRUE);
m_sEdit1.TrimLeft();
m_sEdit1.TrimRight();
m_sEdit2.TrimLeft();
m_sEdit2.TrimRight();
m_sEdit3.TrimLeft();
m_sEdit3.TrimRight();CString csName;
CString csNo;
CString csPsw;
CString csSex;
     
CString sql1;
csName = m_sEdit1;
    csNo = "车主";
    csPsw = m_sEdit3;
    csSex = "车主"; if (m_sEdit1.IsEmpty())
{
MessageBox("用户名不能为空!","信息:",MB_OK|MB_ICONINFORMATION);
return;
}
if (m_sEdit2.IsEmpty())
{
MessageBox("密码不能为空!","信息:",MB_OK|MB_ICONINFORMATION);
return;
}if(m_sEdit2 != m_sEdit3)
{
AfxMessageBox("两次新密码输入不一致!");
return;
}
CString sql = "DELETE FROM usertable";
try
{
if(m_pConnecticon==NULL)
              if(OnInitADOConn()==FALSE)
  return;
  
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pConnecticon->Execute((_bstr_t)sql,NULL,adCmdText);
}
    catch(_com_error e)
{
e.Description();
return;
}m_pConnecticon->BeginTrans();
try
{
//直接操作SQL语句
sql="insert into usertable (userid,firstname,password,dbas)";
sql1.Format("values ('%s','%s','%s','%s') ",csName,csNo,csPsw,csSex);//插入新数据sql = sql + sql1;
m_pConnecticon->Execute((_bstr_t)sql,NULL,adCmdText);
}
catch(...)
{
m_pConnecticon->RollbackTrans();
MessageBox("注册失败!","信息:",MB_OK); 
return;
}
m_pConnecticon->CommitTrans();
MessageBox("注册成功!","信息:",MB_OK);
CDialog::OnOK();
SQLVisual C++数据库连接