// TryODBC.h header file
class CTryODBCDlg : public CDialog
{
ODBCConnection m_Connection;
public:
CTryODBCDlg(CWnd* pParent = NULL);
enum { IDD = IDD_TRYODBC_DIALOG };
CListCtrl m_List;
int m_IntegerField;
float m_FloatField;
CString m_CharField;
CTime m_TimeField;
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
virtual BOOL OnInitDialog();
afx_msg void OnTryInsert();
afx_msg void OnTrySelect();
afx_msg void OnTrySelect2();
afx_msg void OnTryDelete();
afx_msg void OnTryUpdate();
afx_msg void OnConnect();
afx_msg void OnDisconnect();
afx_msg void OnTryUpdate2();
DECLARE_MESSAGE_MAP()
private:
void
AddToList(int ID, int IntegerField, float FloatField,
LPCSTR CharField,CTime TimeField);
void EnableButtons(BOOL On);
};
// TryODBC.cpp implementation file
//........
// Connect to database
void CTryODBCDlg::OnConnect()
{
if (!m_Connection.Connect("DSN=TRYODBC;UID=USER;"))
AfxMessageBox
("Cannot connect to ODBC data source TRYODBC");
}
// Disconnect from database
void CTryODBCDlg::OnDisconnect()
{
m_Connection.Disconnect();
}
// Insert a record in the table
void CTryODBCDlg::OnTryInsert()
{
// Get data from controls
if (!UpdateData(TRUE)) return;
ODBCStatement Stmt(&m_Connection,"OnTryInsert");
TIMESTAMP_STRUCT TimeStamp;
CTime_to_TIMESTAMP_STRUCT(CTime::GetCurrentTime(),
TimeStamp);
Stmt.BindParameter(1,&m_IntegerField);
Stmt.BindParameter(2,&m_FloatField);
Stmt.BindParameter(3,m_CharField);
Stmt.BindParameter(4,&TimeStamp);
Stmt.ExecDirect("INSERT INTO [TryODBC] "
"([IntegerField],[FloatField],[CharField],[TimeField]) "
"VALUES (?,?,?,?)");
}
// Delete all records from the table
void CTryODBCDlg::OnTryDelete()
{
// Empty list control
m_List.DeleteAllItems();
ODBCStatement Stmt(&m_Connection);
Stmt.ExecDirect("DELETE FROM [TryODBC]");
}
// Update records increasing by 1 the IntegerField
void CTryODBCDlg::OnTryUpdate()
{
ODBCStatement Stmt(&m_Connection);
Stmt.ExecDirect("UPDATE [TryODBC] "
"SET [IntegerField]=[IntegerField]+1;");
}
// Increment IntegerField if it is less than in dialog box
void CTryODBCDlg::OnTryUpdate2()
{
// Get data from controls
UpdateData(TRUE);
ODBCStatement Stmt(&m_Connection);
Stmt.BindParameter(1,&m_IntegerField);
Stmt.ExecDirect("UPDATE [TryODBC] "
"SET [IntegerField]=[IntegerField]+1 "
"WHERE [IntegerField]<?;");
}
// Select records using BindCol
void CTryODBCDlg::OnTrySelect()
{
// Empty list control
m_List.DeleteAllItems();
ODBCStatement Stmt(&m_Connection,"OnTrySelect");
int ID;
int IntegerField;
char CharField[256];
float FloatField;
TIMESTAMP_STRUCT TimeStamp;
CTime TimeField;
Stmt.BindCol(1,&ID);
Stmt.BindCol(2,&IntegerField);
Stmt.BindCol(3,&FloatField);
Stmt.BindCol(4,CharField,sizeof(CharField));
Stmt.BindCol(5,&TimeStamp);
if (Stmt.ExecDirect("SELECT [ID], [IntegerField],
"[FloatField], [CharField], [TimeField] FROM [TryODBC]"))
while (Stmt.Fetch())
{
TIMESTAMP_STRUCT_to_CTime(TimeStamp,TimeField);
AddToList(ID, IntegerField, FloatField, CharField,
TimeField);
}
}
// Select records using GetData
void CTryODBCDlg::OnTrySelect2()
{
// Empty list control
m_List.DeleteAllItems();
ODBCStatement Stmt(&m_Connection,"OnTrySelect2");
if (Stmt.ExecDirect("SELECT [ID], [IntegerField],"
"[FloatField], [CharField], [TimeField] FROM [TryODBC]"))
while (Stmt.Fetch())
{
int ID;
int IntegerField;
float FloatField;
CString CharField;
CTime TimeField;
Stmt.GetData(1,&ID);
Stmt.GetData(2,&IntegerField);
Stmt.GetData(3,&FloatField);
Stmt.GetData(4,&CharField);
Stmt.GetData(5,&TimeField);
AddToList(ID, IntegerField, FloatField, CharField,
TimeField);
}
}