如何判断输入的内容是否与数据库相应字段匹配?(100分)

  • 主题发起人 主题发起人 水晶星球
  • 开始时间 开始时间

水晶星球

Unregistered / Unconfirmed
GUEST, unregistred user!
最近编了一个小型DBF文件数据库建立以及维护系统,经友人试用后发现输入的内容与
数据库相应字段可能不匹配,现将原代码贴出,征求大家意见使之完善化。
原代码如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, DBTables, ExtCtrls, ComCtrls, Grids, DBGrids, Buttons;

type
TForm1 = class(TForm)
PageControl1: TPageControl;
StatusBar1: TStatusBar;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
TabSheet3: TTabSheet;
Table1: TTable;
DataSource1: TDataSource;
GroupBox1: TGroupBox;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
GroupBox2: TGroupBox;
DBGrid1: TDBGrid;
BitBtn6: TBitBtn;
StringGrid1: TStringGrid;
Label3: TLabel;
Label4: TLabel;
Edit3: TEdit;
Label5: TLabel;
ComboBox1: TComboBox;
Label6: TLabel;
Edit4: TEdit;
BitBtn7: TBitBtn;
BitBtn8: TBitBtn;
GroupBox3: TGroupBox;
BitBtn3: TBitBtn;
Edit5: TEdit;
Label7: TLabel;
Edit6: TEdit;
GroupBox4: TGroupBox;
GroupBox6: TGroupBox;
DBGrid2: TDBGrid;
BitBtn4: TBitBtn;
BitBtn5: TBitBtn;
StringGrid2: TStringGrid;
GroupBox5: TGroupBox;
GroupBox7: TGroupBox;
BitBtn9: TBitBtn;
StringGrid3: TStringGrid;
ComboBox2: TComboBox;
Label8: TLabel;
Edit7: TEdit;
Label9: TLabel;
Label10: TLabel;
Label11: TLabel;
procedure FormCreate(Sender: TObject);
procedure BitBtn7Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure BitBtn8Click(Sender: TObject);
procedure BitBtn6Click(Sender: TObject);
procedure TabSheet1Show(Sender: TObject);
procedure TabSheet3Show(Sender: TObject);
procedure TabSheet2Show(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure BitBtn4Click(Sender: TObject);
procedure BitBtn5Click(Sender: TObject);
procedure BitBtn9Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
n:integer;
implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
n:=1;
with stringgrid1 do
begin
cells[0,0]:='序号/说明';
cells[1,0]:='字段名';
cells[2,0]:='字段类型';
cells[3,0]:='字段长度';
end;
with stringgrid2 do
begin
cells[0,0]:='序号/字段名';
options:=[gotabs,gorowmoving,goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goRangeSelect,goediting];
end;
with stringgrid3 do
begin
cells[0,0]:='序号/字段名';
end;
pagecontrol1.ActivePageIndex:=0;
statusbar1.SimpleText :='系统启动成功!';
statusbar1.Refresh ;
end;

procedure TForm1.BitBtn7Click(Sender: TObject);
begin
with stringgrid1 do
begin
cells[0,n]:=inttostr(n);
cells[1,n]:=edit3.Text ;
cells[2,n]:=combobox1.text;
if cells[2,n]='ftinteger' then
cells[3,n]:='0'
else
cells[3,n]:=edit4.Text;
rowcount:=rowcount+1;
end;
n:=n+1;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
i:integer;
begin
with table1 do
begin
active:=false;
databasename:=edit2.Text ;
tabletype:=ttdbase;
tablename:=edit1.text;
with fielddefs do
begin
clear;
for i:=1 to n-1 do
begin
if stringgrid1.Cells[2,i]='ftstring' then
begin
add(stringgrid1.Cells[1,i],ftstring,strtoint(stringgrid1.Cells[3,i]),true);
end;
if stringgrid1.Cells[2,i]='ftinteger' then
begin
add(stringgrid1.Cells[1,i],ftinteger,0,true);
end;
end;
end;
createtable;
statusbar1.SimpleText :=edit1.Text +'建立成功!';
end;
end;


procedure TForm1.BitBtn2Click(Sender: TObject);
begin
if fileexists(edit1.text) then
begin
with table1 do
begin
active:=false;
tablename:=edit1.Text ;
active:=true;
end;
statusbar1.SimpleText :=edit1.Text +'打开成功!';
end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
try
table1.close;
finally
end;
end;

procedure TForm1.BitBtn8Click(Sender: TObject);
begin
with stringgrid1 do
begin
cells[1,row]:=edit3.Text ;
cells[2,row]:=combobox1.text;
cells[3,row]:=edit4.Text;
end;
end;

procedure TForm1.BitBtn6Click(Sender: TObject);
begin
if application.messagebox('确定要删除选定的表吗?','你确定吗?',MB_okcancel)=idok then
begin
table1.Active :=false;
table1.TableName:=edit1.Text;
table1.DeleteTable;
statusbar1.SimpleText :=edit1.Text +'已经删除成功!';
end;
end;

procedure TForm1.TabSheet1Show(Sender: TObject);
begin
statusbar1.SimpleText :='系统管理模式!';
end;

procedure TForm1.TabSheet3Show(Sender: TObject);
begin
statusbar1.SimpleText :='系统查询模式!';
end;

procedure TForm1.TabSheet2Show(Sender: TObject);
begin
statusbar1.SimpleText :='系统输入模式!';

end;

procedure TForm1.BitBtn3Click(Sender: TObject);
var
n:integer;
begin
if fileexists(edit5.text) then
begin
with table1 do
begin
active:=false;
databasename:=edit6.Text ;
tablename:=edit5.Text ;
active:=true;
end;
statusbar1.SimpleText :=edit5.Text+'打开成功!';
with stringgrid2 do
begin
colcount:=table1.FieldCount+1;
for n:=0 to table1.FieldCount-1 do
begin
cells[n+1,0]:=table1.Fields[n].fieldName;
end;
end;
with stringgrid3 do
begin
colcount:=table1.FieldCount+1;
for n:=0 to table1.FieldCount-1 do
begin
cells[n+1,0]:=table1.Fields[n].fieldName;
combobox2.Items.Add (table1.Fields[n].fieldName);
end;
end;
end;
end;

procedure TForm1.BitBtn4Click(Sender: TObject);
var
i:integer;
begin
with table1 do
begin
append;
for i:=1 to fieldcount do
begin
if FieldDefs.Find(stringgrid2.Cells[i,0]).DataType=ftinteger then fieldvalues[stringgrid2.Cells[i,0]]:=strtoint(stringgrid2.Cells[i,stringgrid1.row]);
if FieldDefs.Find(stringgrid2.Cells[i,0]).DataType=ftstring then fieldvalues[stringgrid2.Cells[i,0]]:=stringgrid2.Cells[i,stringgrid1.row];
end;
post;
end;
with stringgrid2 do
begin
RowCount :=stringgrid2.RowCount+1;
Cells [0,row]:=inttostr(row);
end;
statusbar1.SimpleText :='当前记录已经添加';
end;

procedure TForm1.BitBtn5Click(Sender: TObject);
var
i:integer;
begin
i:=0;
with table1 do
begin
try
begin
disablecontrols;
edit;
while i<(stringgrid2.RowCount -stringgrid2.row) do
begin
findprior;
i:=i+1;
end;
for i:=1 to fieldcount do
begin
fieldvalues[stringgrid2.Cells[i,0]]:=stringgrid2.Cells[i,stringgrid1.row];
end;
post;
end;
finally
EnableControls;
end;
end;
statusbar1.SimpleText :='第'+inttostr(stringgrid2.Row)+'行记录已经修改';
end;

procedure TForm1.BitBtn9Click(Sender: TObject);
var
i:integer;
begin
with table1 do
begin
if Locate(combobox2.Text,edit7.Text,[loPartialKey]) then
begin
with stringgrid3 do
begin
cells[0,row]:=inttostr(row);
for i:=1 to fieldcount do cells[i,row]:=fieldbyname(cells[i,0]).asstring;
rowcount:=rowcount+1;
row:=row+1;
end;
end;
end;
end;

end.
水平不高,不敢用什么复杂控件,见笑了[:)]
 
使用lookup字段,用户选择
 
请注意[8D]:
我关心的不是如何识别数据库的字段类型,而是作为文本输入的内容如何区分为
字符,数字,日期等不同类型并作为相应字段内容输入到数据库中。
 
请注意[8D]:
我关心的不是如何识别数据库的字段类型,而是作为文本输入的内容如何区分为
字符,数字,日期等不同类型并作为相应字段内容输入到数据库
 
try
strtoint(edit.text)
except
try
StrToDate(edit.text)
except

end
end
 
最简单的办法,莫过于用dbedit等db控件
 
后退
顶部