📄 main.cpp
字号:
// Direct Oracle Access - Department grid demo
// Allround Automations
// support@allroundautomations.nl
// http://www.allroundautomations.nl
//
// This application demonstrates:
// - Connecting to a database
// - Selecting and updating data using variables
// - Displaying queried data in a grid
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
#include "Main.h"
//---------------------------------------------------------------------------
#pragma link "Grids"
#pragma link "Oracle"
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ConnectBtnClick(TObject *Sender)
{
// Log on or off
if (MainSession->Connected) MainSession->LogOff(); else MainLogon->Execute();
// The button shows if we're connected
ConnectBtn->Down = MainSession->Connected;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::QueryBtnClick(TObject *Sender)
{
int Row;
// Setup titles
DeptGrid->Cells[0][0] = "Number";
DeptGrid->Cells[1][0] = "Name";
DeptGrid->Cells[2][0] = "Location";
Row = 1;
// Execute the query
// SelectQuery->SQL = select * from dept
// order by deptno
try
{
SelectQuery->Execute();
// Fill the grid, mark each row unchanged
while (!SelectQuery->Eof)
{
DeptGrid->Cells[0][Row] = SelectQuery->Field("DEPTNO");
DeptGrid->Cells[1][Row] = SelectQuery->Field("DNAME");
DeptGrid->Cells[2][Row] = SelectQuery->Field("LOC");
DeptGrid->Cells[3][Row] = "UNCHANGED";
SelectQuery->Next();
Row++;
}
}
catch (EOracleError &E)
{
if (E.ErrorCode == 942)
Application->MessageBox("You must install the demo tables first", "Error", IDOK);
else
Application->ShowException(&E);
}
// Adjust the grid
DeptGrid->RowCount = Row + 1;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::DeptGridSetEditText(TObject *Sender, int ACol,
int ARow, const AnsiString Value)
{
// Mark row as changed
DeptGrid->Cells[3][ARow] = "CHANGED";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::CommitBtnClick(TObject *Sender)
{
int Row;
for (Row = 1; Row < DeptGrid->RowCount; Row++)
{
// Execute an update statement for each changed row
// UpdateQuery->SQL = update dept
// set dname = :dname, loc = :loc
// where deptno = :deptno
if (DeptGrid->Cells[3][Row] == "CHANGED")
try
{
// Set the query variables to the new values
UpdateQuery->SetVariable("DEPTNO", DeptGrid->Cells[0][Row]);
UpdateQuery->SetVariable("DNAME", DeptGrid->Cells[1][Row]);
UpdateQuery->SetVariable("LOC", DeptGrid->Cells[2][Row]);
// Update it
UpdateQuery->Execute();
// Mark row unchanged
DeptGrid->Cells[3][Row] = "UNCHANGED";
}
catch (EOracleError &E)
{
Application->ShowException(&E);
}
}
// Commit all updates
MainSession->Commit();
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -