永发信息网

Delphi的一个操作数据库问题

答案:2  悬赏:50  手机版
解决时间 2021-07-25 19:17
  • 提问者网友:浮克旳回音
  • 2021-07-24 20:53

unit bddg1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, DB, DBTables, StdCtrls, Grids, DBGrids, ExtCtrls;

type
Tbddg1_Form = class(TForm)
GroupBox1: TGroupBox;
xm_LEdit: TLabeledEdit;
DBGrid1: TDBGrid;
Button1: TButton;
Button2: TButton;
PopupMenu1: TPopupMenu;
Query1: TQuery;
DataSource1: TDataSource;
N1: TMenuItem;
procedure Button1Click(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
bddg1_Form: Tbddg1_Form;

implementation
uses main;
{$R *.dfm}

procedure Tbddg1_Form.Button1Click(Sender: TObject);
var xm,sqlstr:string;
begin
xm:=Trim(xm_LEdit.Text);
if Length(xm)<1 then
begin
sqlstr:='select a.*,b.ZYMC as ZYMC,c.XBMC as XBMC from xsb a';
sqlstr:=sqlstr+'INNER JOIN zydm b ON a.ZYDM=b.ZYDM';
sqlstr:=sqlstr+'INNER JOIN xbdm c ON a.XBDM=c.XBDM';
end
else
begin
xm:='%'+xm+'%';
sqlstr:='select a.*,b.ZYMC as ZYMC,c.XBMC as XBMC from xsb a';
sqlstr:=sqlstr+'INNER JOIN zydm b ON a.ZYDM=b.ZYDM';
sqlstr:=sqlstr+'INNER JOIN xbdm c ON a.XBDM=b.XBDM';
sqlstr:=sqlstr+'where (a.XM like '''+xm+''')';
end;
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add(sqlstr);
Query1.Open;
if Query1.RecordCount>0 then
N1.Enabled:=true
else
N1.Enabled:=False;
end;

procedure Tbddg1_Form.N1Click(Sender: TObject);
var xh,sqlstr:string;
begin
if Query1.FieldByName('ZT').AsInteger=0 then
begin
xh:=Query1.FieldByName('xh').AsString;
sqlstr:='update xsb set ZT=1 where (XH='''+xh+''')';
with TQuery.Create(nil) do
begin
try
SessionName:=Main_Form.Database1.SessionName;
DataBaseName:=Main_Form.Database1.DatabaseName;
sql.Clear;
sql.Add(sqlstr);
ExecSQL;
finally
Free;
end;
end;
end
else
begin
ShowMessage('该同学已经报道!');
exit;
end;

end;

procedure Tbddg1_Form.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
if gdSelected in State then Exit;
case Query1.FieldByName('ZT').AsInteger of
0:(Sender as TDBGrid).Canvas.Brush.Color:=clWindow;
1:(Sender as TDBGrid).Canvas.Brush.Color:=clYellow;
2:(Sender as TDBGrid).Canvas.Brush.Color:=clBlue;
3:(Sender as TDBGrid).Canvas.Brush.Color:=clRed;
end;
DbGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;

procedure Tbddg1_Form.FormCreate(Sender: TObject);
begin
Query1.SessionName:=Main_Form.Database1.SessionName;
Query1.DatabaseName:=Main_Form.Database1.DatabaseName;
end;

procedure Tbddg1_Form.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Main_Form.N12.Enabled:=True;
Action:=caFree;
end;

end.

最佳答案
  • 五星知识达人网友:掌灯师
  • 2021-07-24 22:31
把你的代码给我,我帮你调
全部回答
  • 1楼网友:雾月
  • 2021-07-24 23:16

BDE?

怎么不用ADO?

用ADO好了。另外,你贴这么长的代码出来,没几个愿意看完的了。

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯