📄 insertdlg.cpp
字号:
/*************************************************************************************
@author Chandar
@version 1.0
Development Environment : Visual C++ 6.0
Name of the File : InsertDlg.cpp
Creation/Modification History :
07-Aug-2001 Created
File Overview
Class Implementation file for InsertDlg dialog box.
This file includes the GUI for InsetDlg dialog box. It handles various events
taking place on dialog components.
The dialog box displays the list of products and languages. When user selects a product
and language, the existing name and description of product is retrieved from database
and displayed in the text boxes.The user can change product name and description in
the selected language which is updated or inserted(if it does not exists) in the
database using stored procedure which takes NVARCHAR2 type variable as the parameters
for name and description
*************************************************************************************/
#include "stdafx.h"
#include "OleMR.h"
#include "InsertDlg.h"
#include <string.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CInsertDlg dialog
CInsertDlg::CInsertDlg(CWnd* pParent /*=NULL*/)
: CDialog(CInsertDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CInsertDlg)
m_tname = _T("");
m_tdescription = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CInsertDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CInsertDlg)
DDX_Control(pDX, IDC_COMBOPRODUCT, m_product);
DDX_Control(pDX, IDC_COMBOLANGUAGE, m_language);
DDX_Text(pDX, IDC_TNAME, m_tname);
DDX_Text(pDX, IDC_TDESCRIPTION, m_tdescription);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CInsertDlg, CDialog)
//{{AFX_MSG_MAP(CInsertDlg)
ON_BN_CLICKED(ID_INSERT, OnInsert)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_CBN_SELCHANGE(IDC_COMBOPRODUCT, OnSelchangeComboproduct)
ON_CBN_SELCHANGE(IDC_COMBOLANGUAGE, OnSelchangeCombolanguage)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CInsertDlg message handlers
/***********************************************************************************
This function includes the code for initializing the dialog box and its components
before displaying the dialog box
Here product names and languages are retrieved from database ands shown in list boxes
************************************************************************************/
BOOL CInsertDlg::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
IRowset* pRowset=NULL;
//SQL statement to get product names and ids
CString sqlString ="Select product_id,product_name from product_information";
//Call function to execute above sql statement
pRowset = obj.ExecuteQuery(sqlString);
//Display product names in listbox
DisplayProductsAndLanguages(pRowset,&m_product);
//SQL statement to get language names and ids
sqlString ="Select language_id,language_name from language";
//Call function to execute above sql statement
pRowset = obj.ExecuteQuery(sqlString);
//Display language names in listbox
DisplayProductsAndLanguages(pRowset,&m_language);
return TRUE; // return TRUE unless you set the focus to a control
}
void CInsertDlg::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();
}
}
/*****************************************************************
This function is used to display the product and language names
retrieved from database in to the list boxes on dialog box
****************************************************************/
void CInsertDlg::DisplayProductsAndLanguages(IRowset *pRowset,CComboBox *p_box)
{
ULONG lNumRowsRetrieved,j,coloffset=0;
HROW hRows[5];
HROW* pRows = &hRows[0];
char* pBuffer;
// Allocate Space for the Row Buffer
coloffset=obj.cbColOffset;
pBuffer = new char[coloffset];
/*****************************************
Retrieve 5 rows from the rowset object
******************************************/
pRowset->GetNextRows(0, // For future use
0, // Number of Rows to Skip
5, // Number of Rows to
// Retrieve
&lNumRowsRetrieved, // Number of Rows
// Returned
&pRows); // The Row Buffer
// Display the retrieved rows
while(lNumRowsRetrieved > 0)
{
// For each row in the set we retrieved, display the
// fields contained in it...
for(j = 0; j < lNumRowsRetrieved; j++)
{
// Clear the buffer
memset(pBuffer, 0,obj.cbColOffset);
/*******************************************************************
Retrieve the actual row data using the GetData method.
/******************************************************************/
// Get the row data values
HRESULT hr= pRowset->GetData(hRows[j], obj.hAccessor, pBuffer);
if(hr==S_OK)
{
//Retrieve the name and id specifying their position in buffer
CString name=&pBuffer[obj.pBindings[1].obValue];
CString pid=&pBuffer[obj.pBindings[0].obValue];
int data= atoi(pid);
int index= p_box->AddString(name);
//Set the id as itemdata for list box
//This will be returned when listbox item is selected
p_box->SetItemData(index,data);
}
}
/*******************************************************************
Release the row handles before retrieving the next set of
row handles.
*******************************************************************/
// Release the Rows Retrieved...
pRowset->ReleaseRows(lNumRowsRetrieved, hRows, NULL, NULL, NULL);
// Get a Set of 5 Rows...
pRowset->GetNextRows(0, // For future use
0, // Number of Rows to
// Skip
5, // Number of Rows to
// Retrieve
&lNumRowsRetrieved, // Number of Rows
// Returned
&pRows); // The Row Buffer
}//end while loop
pRowset=NULL;
//Release rowset pointer
obj.pRowset->Release();
//Uninitialize the database session
obj.UnInitialize();
}
/***************************************************************************
This function handles the message for click event of Insert Button
It calls InsertDescription function of ADODBHandler class to insert data
into database. It also checks if the data already exists.
***************************************************************************/
void CInsertDlg::OnInsert()
{
//Get the data from controls into variables
UpdateData(TRUE);
//Check if user has filled all the required fields
if(m_product.GetCurSel()==CB_ERR || m_language.GetCurSel()==CB_ERR || m_tname=="" || m_tdescription=="")
MessageBox("All Fields Required");
else
{
//Get the product id of product selected by the user
DWORD productid=m_product.GetItemData(m_product.GetCurSel());
//Get the language id of the language selected by user
CString languageid;
languageid.Format("%ld",m_language.GetItemData(m_language.GetCurSel()));
//Call the function to insert data into database
obj.InsertData(productid,languageid,m_tname,m_tdescription);
}
}
/*************************************************************************
This function handles the message for selection change event of products
combobox. It retrieves the existing name and description of selected product
from database in selected language and displays them in text boxes.
*************************************************************************/
void CInsertDlg::OnSelchangeComboproduct()
{
if(m_product.GetCurSel()!=CB_ERR && m_language.GetCurSel()!=CB_ERR)
{
//Clear the text boxes
m_tname="";
m_tdescription="";
//Get the product id of product selected by the user
DWORD productid=m_product.GetItemData(m_product.GetCurSel());
//Get the language id of the language selected by user
CString languageid;
languageid.Format("%ld",m_language.GetItemData(m_language.GetCurSel()));
//SQL statement to get product name and description in selected language
CString sqlString;
sqlString.Format("Select translated_name,translated_description from "
"product_descriptions where product_id= %d and language_id='%s'",productid,languageid);
IRowset* pRowset=NULL;
//Call function to execute above SQL query
pRowset = obj.ExecuteQuery(sqlString);
ULONG lNumRowsRetrieved,coloffset=0;
HROW hRows[1];
HROW* pRows = &hRows[0];
char* pBuffer;
// Allocate Space for the Row Buffer
coloffset=obj.cbColOffset;
pBuffer = new char[coloffset];
/*****************************************
Retrieve row from the rowset object
******************************************/
pRowset->GetNextRows(0, // For future use
0, // Number of Rows to Skip
1, // Number of Rows to
// retrieve
&lNumRowsRetrieved, // Number of Rows
// returned
&pRows); // The Row Buffer
// display the fields contained in retrieved row
if(lNumRowsRetrieved>0)
{
// Clear the buffer
memset(pBuffer, 0,obj.cbColOffset);
/******************************************************
Retrieve the actual row data using the GetData method.
/******************************************************/
// Get the row data values
HRESULT hr= pRowset->GetData(hRows[0], obj.hAccessor, pBuffer);
if(hr==S_OK)
{
//Retrieve the name and description specifying their position in buffer
CString name=&pBuffer[obj.pBindings[0].obValue];
CString desc=&pBuffer[obj.pBindings[1].obValue];
//Put the data in text box variables
m_tname=name;
m_tdescription=desc;
}
}
//Update text boxes to show the data
UpdateData(false);
}
}
/*************************************************************************
This function handles the message for selection change event of languages
combobox. It retrieves the existing name and description of selected product
from database in selected language and displays them in text boxes.
*************************************************************************/
void CInsertDlg::OnSelchangeCombolanguage()
{
OnSelchangeComboproduct();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -