📄 myoledbsampledlg.cpp
字号:
// MyOLEDBSampleDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MyOLEDBSample.h"
#include "MyOLEDBSampleDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CMyOLEDBSampleDlg dialog
CMyOLEDBSampleDlg::CMyOLEDBSampleDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyOLEDBSampleDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyOLEDBSampleDlg)
m_SalesManName = _T("");
m_CustomerName = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyOLEDBSampleDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyOLEDBSampleDlg)
DDX_Control(pDX, IDC_LIST1, m_CustList);
DDX_Text(pDX, IDC_EDIT1, m_SalesManName);
DDV_MaxChars(pDX, m_SalesManName, 15);
DDX_Text(pDX, IDC_EDIT2, m_CustomerName);
DDV_MaxChars(pDX, m_CustomerName, 15);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyOLEDBSampleDlg, CDialog)
//{{AFX_MSG_MAP(CMyOLEDBSampleDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(ID_DATABASE_CONNECT, OnDatabaseConnect)
ON_COMMAND(ID_DATABASE_DISCONNECT, OnDatabaseDisconnect)
ON_COMMAND(ID_DELETE_CUSTOMERS, OnDeleteCustomers)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyOLEDBSampleDlg message handlers
BOOL CMyOLEDBSampleDlg::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 CMyOLEDBSampleDlg::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 CMyOLEDBSampleDlg::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 CMyOLEDBSampleDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyOLEDBSampleDlg::OnDatabaseConnect()
{
try
{
if(!m_Cnn.IsOpen()) // Check if database is opened
m_Cnn.Open("TESTDB", "Admin", "");
AfxMessageBox("Database connection successful");
}
catch(CSypODLException e)
{
e.DisplayError();
AfxMessageBox("Solution : You have to create an ODBC DSN called 'TESTDB' to the \
database that is provided with this application for test purpose.");
}
}
void CMyOLEDBSampleDlg::OnDatabaseDisconnect()
{
m_Cnn.Close();
}
void CMyOLEDBSampleDlg::OnDeleteCustomers()
{
if(!m_Cnn.IsOpen()) // Check if database is opened
{
AfxMessageBox("You are not connected to the database yet.");
return;
}
int Result = ::MessageBox(NULL, "Delete all customers?", "SURE!", MB_YESNO | MB_ICONSTOP);
if(Result == IDNO)
return;
try
{
m_Cnn.Execute("DELETE FROM Customer"); // Execute method is used for action queries.
AfxMessageBox("All customers are deleted.");
}
catch(CSypODLException e)
{
e.DisplayError();
}
}
void CMyOLEDBSampleDlg::OnButton1()
{
UpdateData();
if(m_CustomerName.IsEmpty())
{
AfxMessageBox("Enter customer name first.");
return;
}
else
m_CustList.AddString(m_CustomerName);
m_CustomerName = "";
UpdateData(FALSE);
}
void CMyOLEDBSampleDlg::OnOK()
{
UpdateData();
if(!m_Cnn.IsOpen()) // Check if database is opened
{
AfxMessageBox("不能连接数据库.");
return;
}
if(m_SalesManName.IsEmpty())
{
AfxMessageBox("必须输入销售人员的姓名.");
return;
}
int CustCount = m_CustList.GetCount();
if(CustCount == 0)
{
AfxMessageBox("输入顾客的姓名.");
return;
}
// Create recordset for parent table
CSypODLRecordBase<CAccessor<CSalesManAccessor> > mRsParent;
// Create recordset for child table
CSypODLRecordBase<CAccessor<CCustomerAccessor> > mRsChild;
try
{
long SID;
mRsParent.Open("SalesMan", &m_Cnn, 1); // Open parent table recordset
if(mRsParent.IsEOF()) // Check if there is any record
SID = 1;
else
{
mRsParent.MoveLast(); // Move to the last record
SID = mRsParent.GetAccessor()->m_SalesManID + 1; // Create new SalesmanID
}
m_Cnn.StartTransaction(); // Start transaction
mRsChild.Open("Customer", &m_Cnn, 1); // Open child table recordset
mRsParent.GetAccessor()->ClearRecord(); // Field buffer must be cleared
mRsParent.GetAccessor()->m_SalesManID = SID; // Add SID to field buffer
lstrcpy(mRsParent.GetAccessor()->m_SalesManName, m_SalesManName); // Add salesman name
mRsParent.Insert(); // Insert row in the table
for(int i=0; i<CustCount; i++)
{
CString mCustName;
m_CustList.GetText(i, mCustName);
mRsChild.GetAccessor()->ClearRecord(); // Field buffer must be cleared
mRsChild.GetAccessor()->m_CustomerID_status = DBSTATUS_S_IGNORE;
lstrcpy(mRsChild.GetAccessor()->m_CustomerName, mCustName);
mRsChild.GetAccessor()->m_Address_status = DBSTATUS_S_ISNULL; // Insert NULL
mRsChild.GetAccessor()->m_SalesManID = SID;
mRsChild.Insert(); // Insert row in the child table
}
m_Cnn.Commit(); // Commit transaction.
}
catch(CSypODLException e)
{
m_Cnn.Abort(); // Rollback transaction in case of error
e.DisplayError();
}
AfxMessageBox("记录添加成功.");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -