对数据库表及字段的查询添加
if OpenDialog1.Execute then
begin
Edit1.Text := OpenDialog1.FileName;
ADOConnection1.Close;
ADOConnection1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='
+ Edit1.Text + ';Persist Security Info=False';
ADOConnection1.Open;
ADOConnection1.GetTableNames(ComboBox1.Items);
ComboBox1.ItemIndex := 0;
end;
如果你是用ADO的话
ADOConnection1.GetTableNames();获得表的列表
ADOConnection1.GetFieldNames();获得某个表的字段列表
TADOConnection 连接后,
ADOConnection1.DataSetCount 为表的个数
ADOConnection1.DataSets[i].Name表名
如:
ADOConnection1.GetTableNames(ComboBox1.Items, False);
//该函数会把表名列在ComboBox1.Items中.
有表名获取字段名,把字段名列在中
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
if ComboBox1.Text<>'' then
begin
ADOConnection1.GetFieldNames(ComboBox1.Text,listbox.Items);
end;
end;
但是在测试发现字段名默认都自动排序了,如要取消可以 fieldlist.Sorted := False;
“动态添加一个数据库字段”是数据库SQL本身的事,与Delphi关系不大。
ADOCommand1.CommandText := 'alter table 表名 add 字段名1 varchar(12),字段名2 integer,字段3 datetime';
ADOCommand1.Execute;
adoquery1.sql.add('alter table 表名 add 字段名1 varchar(12),字段名2 integer,字段3 datetime');
通过ADO.QUERY连接数据库···然后用SQL语句查询就可以了
获取当前数据库中的所有用户表个数
select count(*) from sysobjects where xtype='U' and category=0
获取当前数据库中的所有用户表表名
select name from sysobjects where xtype='U' and category=0
sysobjects 系统对象表
XTYPExtype char(2) 对象类型。
可以是下列对象类型中的一种:
C = CHECK 约束
D = 默认值或 DEFAULT 约束
F = FOREIGN KEY 约束
L = 日志
FN = 标量函数
IF = 内嵌表函数
P = 存储过程
PK = PRIMARY KEY 约束(类型是 K)
RF = 复制筛选存储过程
S = 系统表
TF = 表函数
TR = 触发器
U = 用户表
UQ = UNIQUE 约束(类型是 K)
V = 视图
X = 扩展存储过程
category int 用于发布、约束和标识。
//在列表框中添加数据集中的所有字段
procedure TForm1.FillFields; var i:Integer; begin //清空列表框 ListBoxField.Items.Clear; for i:=0 to ADOQuery1.FieldCount-1 do begin ListBoxField.Items.Add(ADOQuery1.Fields[i].DisplayName); end; ListBoxField2.Items:=ListBoxField.Items; //确保ListBox中选项被选中 ListBoxField.Selected[0]:=true; ListBoxField2.Selected[0]:=true; end;//获取查询条件
function TForm1.GetSearchOpition: TLocateOptions;
var
SearchOpition:TLocateOptions;
begin
if CheckBox1.Checked then
SearchOpition:=SearchOpition+[loPartialKey];
if CheckBox2.Checked then
SearchOpition:=SearchOpition+[loCaseInsensitive];
Result:=SearchOpition;
end;
procedure TForm1.ButtonSearchClick(Sender: TObject);
begin with ADOQuery1 do begin //开始执行查询
if Locate(ListBoxField.Items[ListBoxField.ItemIndex]+';'+ListBoxField2.Items[ListBoxField2.ItemIndex],VarArrayOf([Edit1.Text,Edit2.Text]),GetSearchOpition)=false then ShowMessage('没有查询到符合条件的记录');
end; end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if gdSelected in State then//如果是当前区域是被选择区域
begin
//设置被选择区域的背景色
TDBGrid(sender).Canvas.Brush.Color := clInfoBk ;
//设置被选择区域的文字颜色
TDBGrid(sender).Canvas.Font.Color:= clFuchsia ;
end
else //如果是当前区域不是被选择区域
begin
//如果是偶数行,则背景色为clSkyBlue
if ADOQuery1.RecNo mod 2 = 0 then
TDBGrid(sender).Canvas.Brush.Color := clSkyBlue
else //如果是奇数行,则背景色为clInactiveCaptionText
TDBGrid(sender).Canvas.Brush.Color := clInactiveCaptionText;
end;
//调用默认的绘图函数
TDBGrid(sender).DefaultDrawColumnCell(Rect,DataCol,Column,State);
with TDBGrid(sender).Canvas do
begin
//设置表格横线的颜色
Pen.Color := clPurple ;
MoveTo(Rect.Left, Rect.Bottom);
//画表格横线
LineTo(Rect.Right, Rect.Bottom);
//设置表格竖线的颜色
Pen.Color := clNavy;
MoveTo(Rect.Right, Rect.Top);
//画表格竖线
LineTo(Rect.Right, Rect.Bottom);
end;
end
|