?? odltestdlg.cpp
字號:
// ODLTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ODLTest.h"
#include "ODLTestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CODLTestDlg dialog
CODLTestDlg::CODLTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CODLTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CODLTestDlg)
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 CODLTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CODLTestDlg)
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(CODLTestDlg, CDialog)
//{{AFX_MSG_MAP(CODLTestDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_COMMAND(ID_DATABASE_CONNECT, OnDatabaseConnect)
ON_COMMAND(ID_DATABASE_DISCONNECT, OnDatabaseDisconnect)
ON_COMMAND(ID_DELETE_CUSTOMERS, OnDeleteCustomers)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CODLTestDlg message handlers
BOOL CODLTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
}
// 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 CODLTestDlg::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();
}
}
//************************************************
HCURSOR CODLTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//************************************************
void CODLTestDlg::OnButton1()
{
UpdateData();
if(m_CustomerName.IsEmpty())
{
AfxMessageBox("Enter customer name first.");
return;
}
else
m_CustList.AddString(m_CustomerName);
m_CustomerName = "";
UpdateData(FALSE);
}
//************************************************
void CODLTestDlg::OnOK()
{
UpdateData();
if(!m_Cnn.IsOpen()) // Check if database is opened
{
AfxMessageBox("You are not connected to the database yet.");
return;
}
if(m_SalesManName.IsEmpty())
{
AfxMessageBox("Salesman Name must be entered.");
return;
}
int CustCount = m_CustList.GetCount();
if(CustCount == 0)
{
AfxMessageBox("At least one customer must be entered.");
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("Record added successfully.");
}
//************************************************
// Connect to database
void CODLTestDlg::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.");
}
}
//************************************************
// Disconnect from database
void CODLTestDlg::OnDatabaseDisconnect()
{
m_Cnn.Close();
}
//************************************************
void CODLTestDlg::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();
}
}
//************************************************
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -