초보인데요..이거랑 비슷한 프로그램을 만들려는데
책 한권이랑 같이 보고 있는데 80프로 이상이 이해가 안되네요
감이 잘 오지않네요...왜 갑자기 insert가 나오는지..제가 알기로는
이프로그램은 검색해서 realgrid상에서 수정하거나 삭제하는 프로그램으로
알고있는데 수정이면 update만 있어도 안되나요? 흐름이나 주석좀 달아주세요 여러분..
unit UGridTest;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, StdCtrls, URGrids, URMGrid;
type
TfrmMain = class(TForm)
grdMain: TRealGrid;
btnSelect: TButton;
btnPost: TButton;
btnDelete: TButton;
qryMain: TQuery;
dbMain: TDatabase;
procedure btnSelectClick(Sender: TObject);
procedure grdMainDrawMarker(Sender: TObject; ARow: Integer;
var Text: String; var BCol, FCol: TColor; var FStyle: TFontStyles;
var ImgIndex: Integer; var Value: Boolean);
procedure btnPostClick(Sender: TObject);
procedure btnDeleteClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure grdMainExit(Sender: TObject);
private
procedure SelectData;
procedure PostData;
procedure DeleteData;
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
const
// Grid Columns Index
GR_EMPNO = 0;
GR_LNAME = 1;
GR_FNAME = 2;
// Query
SQL_SELECT_DATA = 'SELECT * FROM employee ';
SQL_INSERT_DATA = 'INSERT INTO employee '+#13#10+
' (EmpNo, LastName, FirstName) '+#13#10+
' VALUES '+#13#10+
' (:EmpNo, :LastName, :FirstName) ';
SQL_UPDATE_DATA = 'UPDATE employee SET LastName = :LastName, '+#13#10+
' FirstName = :FirstName '+#13#10+
' WHERE EmpNo = :EmpNo ';
SQL_DELETE_DATA = 'DELETE FROM employee '+#13#10+
' WHERE EmpNo = :EmpNo ';
implementation
{$R *.DFM}
procedure TfrmMain.SelectData;
begin
with qryMain, grdMain do
begin
Clear; // RealGrid.Clear;
SQL.Text := SQL_SELECT_DATA;
Open;
if not IsEmpty then
while not EOF do
begin
AddRow; // 그리드에 빈 로우를 추가
// TRealGrid.Cells[0, TRealGrid.RowCount -1].AsFloat 의 축약
Cells[GR_EMPNO, RowCount -1].AsFloat := FieldByName('EmpNo' ).AsFloat;
Cells[GR_LNAME, RowCount -1].AsString := FieldByName('LastName' ).AsString;
Cells[GR_FNAME, RowCount -1].AsString := FieldByName('FirstName').AsString;
Next;
end;
end;
end;
procedure TfrmMain.PostData; //일괄저장 방법
var
nRow: Integer;
begin
with qryMain, grdMain do
begin
// dbMain.StartTransaction;
// RDBMS 사용시 주석을 지워 주세요, 트랜잭션 처리 방법
try
for nRow := 0 to RowCount -1 do
begin
if RowState[nRow] = wrUpdated then
SQL.Text := SQL_UPDATE_DATA
else if RowState[nRow] = wrInserted then
SQL.Text := SQL_INSERT_DATA;
if (RowState[nRow] = wrUpdated) or (RowState[nRow] = wrInserted) then
begin
ParamByName('EmpNo' ).AsFloat := Cells[GR_EMPNO, nRow].AsFloat;
ParamByName('LastName' ).AsString := Cells[GR_LNAME, nRow].AsString;
ParamByName('FirstName').AsString := Cells[GR_FNAME, nRow].AsString;
ExecSQL;
end;
end;
//dbMain.Commit;
for nRow := 0 to RowCount -1 do
RowState[nRow] := wrNormal;
Refresh;
except
on E: Exception do
begin
//dbMain.Rollback;
ShowMessage(E.Message + ' [저장오류]');
end;
end;
end;
end;
procedure TfrmMain.DeleteData;
begin
if Application.MessageBox('정말삭제할래?','삭제',MB_YESNO) = IDYES then
with qryMain, grdMain do
begin
//dbMain.StartTransaction;
try
SQL.Text := SQL_DELETE_DATA;
ParamByName('EmpNo').AsFloat := Cells[GR_EMPNO, Row].AsFloat;
ExecSQL;
//dbMain.Commit;
DeleteRow(Row); // 또는 SelectData;
except
on E: Exception do
begin
//dbMain.Rollback;
ShowMessage(E.Message + ' [삭제오류]');
end;
end;
end;
end;
// Event
procedure TfrmMain.FormCreate(Sender: TObject);
begin
with grdMain do
Options := Options + [wgoEditing, wgoInserting];
end;
procedure TfrmMain.btnSelectClick(Sender: TObject);
begin
SelectData;
end;
procedure TfrmMain.btnPostClick(Sender: TObject);
begin
PostData;
end;
procedure TfrmMain.btnDeleteClick(Sender: TObject);
begin
DeleteData;
end;
procedure TfrmMain.grdMainExit(Sender: TObject);
begin
with grdMain do
Post;
end;
procedure TfrmMain.grdMainDrawMarker(Sender: TObject; ARow: Integer;
var Text: String; var BCol, FCol: TColor; var FStyle: TFontStyles;
var ImgIndex: Integer; var Value: Boolean);
begin
case grdMain.RowState[ARow] of
wrUpdated : BCol := clYellow;
wrInserted: BCol := clGreen;
wrDeleted : BCol := clRed;
else ; // wrNormal -- 이 줄은 생략해도 된다.
end;
end;
end.
그렇다고 몽땅 달수도 없고 모르시는부분만 추려서
분할해서 질문해보시면 많은 분들이 답해주실거 같습니다.