?? newdata.pas
字號:
{##########################################
旁注入侵專用程序 3.0升級版
----------------------------------------
模塊:數據庫瀏覽 - 新建數據表
描述:該單元主要用于新建數據表
作者:2005.4.2日晚 明小子
##########################################}
unit NewData;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Spin, ComCtrls;
type
TNewDataForm = class(TForm)
Label1: TLabel;
EdTableName: TEdit;
ChkAuto: TCheckBox;
Button2: TButton;
Button3: TButton;
SpinEdit1: TSpinEdit;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
CbType: TComboBox;
ChkSet: TCheckBox;
EdFieldName: TEdit;
Button1: TButton;
procedure SpinEdit1Change(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure CbTypeKeyPress(Sender: TObject; var Key: Char);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
NewDataForm: TNewDataForm;
Num: integer = 2;
ED: array of TEdit;
LB, LB2: array of TLabel;
CBOX: array of TComboBox;
Chk: array of TCheckBox;
implementation
uses MainUnit;
{$R *.dfm}
procedure TNewDataForm.SpinEdit1Change(Sender: TObject);
begin
SetLength(ED, SpinEdit1.Value + 5);
SetLength(LB, SpinEdit1.Value + 5);
SetLength(LB2, SpinEdit1.Value + 5);
SetLength(CBOX, SpinEdit1.Value + 5);
SetLength(CHK, SpinEdit1.Value + 5);
if Num = SpinEdit1.Value then
begin
ED[SpinEdit1.Value + 1] := TEdit.Create(self);
ED[SpinEdit1.Value + 1].Parent := NewDataForm;
ED[SpinEdit1.Value + 1].Width := 135;
ED[SpinEdit1.Value + 1].Height := 20;
ED[SpinEdit1.Value + 1].Top := 21 + (SpinEdit1.Value * 24);
ED[SpinEdit1.Value + 1].Left := 88;
LB[SpinEdit1.Value + 1] := TLabel.Create(self);
LB[SpinEdit1.Value + 1].Parent := NewDataForm;
LB[SpinEdit1.Value + 1].Caption := '列' + inttostr(SpinEdit1.Value) + ':字段名';
LB[SpinEdit1.Value + 1].Width := 78;
LB[SpinEdit1.Value + 1].Height := 12;
LB[SpinEdit1.Value + 1].Top := 26 + (SpinEdit1.Value * 24);
LB[SpinEdit1.Value + 1].Left := 8;
LB2[SpinEdit1.Value + 1] := TLabel.Create(self);
LB2[SpinEdit1.Value + 1].Parent := NewDataForm;
LB2[SpinEdit1.Value + 1].Caption := '字段類型';
LB2[SpinEdit1.Value + 1].Width := 52;
LB2[SpinEdit1.Value + 1].Height := 12;
LB2[SpinEdit1.Value + 1].Top := 26 + (SpinEdit1.Value * 24);
LB2[SpinEdit1.Value + 1].Left := 232;
CBOX[SpinEdit1.Value + 1] := TComboBox.Create(self);
CBOX[SpinEdit1.Value + 1].Parent := NewDataForm;
CBox[SpinEdit1.Value + 1].Items.Add('文本');
CBox[SpinEdit1.Value + 1].Items.Add('備注');
CBox[SpinEdit1.Value + 1].Items.Add('數字');
CBox[SpinEdit1.Value + 1].Items.Add('貨幣');
CBox[SpinEdit1.Value + 1].Items.Add('日期/時間');
CBox[SpinEdit1.Value + 1].ItemIndex := 0;
CBOX[SpinEdit1.Value + 1].Width := 81;
CBOX[SpinEdit1.Value + 1].Height := 20;
CBOX[SpinEdit1.Value + 1].Top := 21 + (SpinEdit1.Value * 24);
CBOX[SpinEdit1.Value + 1].Left := 288;
CBOX[SpinEdit1.Value + 1].SelStart := 10;
Chk[SpinEdit1.Value + 1] := TCheckBox.Create(self);
Chk[SpinEdit1.Value + 1].Parent := NewDataForm;
Chk[SpinEdit1.Value + 1].Caption := '必填字段';
Chk[SpinEdit1.Value + 1].Width := 73;
Chk[SpinEdit1.Value + 1].Height := 17;
Chk[SpinEdit1.Value + 1].Top := 23 + (SpinEdit1.Value * 24);
Chk[SpinEdit1.Value + 1].Left := 376;
ED[SpinEdit1.Value + 1].SetFocus;
NewDataForm.Height := 118 + SpinEdit1.Value * 25;
Button1.Top := NewDataForm.Height - 70;
Button2.Top := NewDataForm.Height - 70;
Button3.Top := NewDataForm.Height - 70;
Inc(Num);
end
else
begin
ED[Num].Free;
LB[Num].Free;
LB2[Num].Free;
CBOX[Num].Free;
Chk[Num].Free;
Dec(Num);
NewDataForm.Height := 118 + SpinEdit1.Value * 25;
Button1.Top := NewDataForm.Height - 70;
Button2.Top := NewDataForm.Height - 70;
Button3.Top := NewDataForm.Height - 70;
end;
end;
procedure TNewDataForm.Button2Click(Sender: TObject);
var
TypeStr: string;
i: integer;
Tables: TStrings;
TN: TTreeNode;
begin
if Trim(EdTableName.Text) = '' then
begin
application.MessageBox('請先設置表名!', '提示', 64);
Exit;
end;
if Trim(EdFieldName.Text) = '' then
begin
application.MessageBox('請先設置字段名!', '提示', 64);
Exit;
end;
for i := 0 to MainForm.TableTree.Items.Count - 1 do
if Trim(EdTableName.Text) = MainForm.TableTree.Items[i].Text then
begin
application.MessageBox('你設置的表名與該數據庫的另一對象同名,請重新設置表名!', '提示', 48);
Exit;
end;
try
MainForm.ADOQuery1.Close;
if (CbType.Itemindex = 0) or (CbType.Text = '文本') then TypeStr := 'String';
if CbType.Itemindex = 1 then TypeStr := 'memo';
if CbType.Itemindex = 2 then TypeStr := 'long';
if CbType.Itemindex = 3 then TypeStr := 'currency';
if CbType.Itemindex = 4 then TypeStr := 'time';
if ChkAuto.Checked then
begin
if ChkSet.Checked then
MainForm.ADOQuery1.SQL.Text := 'create table ' + EdTableName.Text + '(ID int IDENTITY (1, 1) NOT NULL, ' + EdFieldName.Text + ' ' + TypeStr + ' not null)'
else
MainForm.ADOQuery1.SQL.Text := 'create table ' + EdTableName.Text + '(ID int IDENTITY (1, 1) NOT NULL, ' + EdFieldName.Text + ' ' + TypeStr + ')';
end
else
begin
if ChkSet.Checked then
MainForm.ADOQuery1.SQL.Text := 'create table ' + EdTableName.Text + '(' + EdFieldName.Text + ' ' + TypeStr + ' not null)'
else
MainForm.ADOQuery1.SQL.Text := 'create table ' + EdTableName.Text + '(' + EdFieldName.Text + ' ' + TypeStr + ')'
end;
MainForm.ADOQuery1.ExecSQL;
if SpinEdit1.Value > 1 then
begin
for i := 3 to SpinEdit1.Value + 1 do
begin
MainForm.ADOQuery1.Close;
if (CBOX[i].Itemindex = 0) or (CBOX[i].Text = '文本') then TypeStr := 'String';
if CBOX[i].Itemindex = 1 then TypeStr := 'memo';
if CBOX[i].Itemindex = 2 then TypeStr := 'long';
if CBOX[i].Itemindex = 3 then TypeStr := 'currency';
if CBOX[i].Itemindex = 4 then TypeStr := 'time';
if Chk[i].Checked then
MainForm.ADOQuery1.SQL.Text := 'alter table ' + EdTableName.Text + ' add column ' + ED[i].Text + ' ' + TypeStr + ' not null'
else
MainForm.ADOQuery1.SQL.Text := 'alter table ' + EdTableName.Text + ' add column ' + ED[i].Text + ' ' + TypeStr;
MainForm.ADOQuery1.ExecSQL;
end;
end;
{ * * * * * * * 創建完畢后,自動打開數據表 * * * * * * }
try
MainForm.TableTree.Items.Clear;
MainForm.FieldsTree.Items.Clear;
except
end;
try
Tables := TStringList.Create;
MainForm.ADOCon.GetTableNames(Tables);
for i := 0 to Tables.Count - 1 do
MainForm.TableTree.Items.AddChild(TN, Tables.Strings[i]);
Tables.Free;
MainForm.OpenDataBool := True; {表示數據已已經被打開了}
MainForm.Pane1.Caption := '共有:' + inttostr(MainForm.TableTree.Items.Count) + '個數據表';
except
end;
application.MessageBox('新數據表已成功創建!', '提示', 64);
Close;
except
application.MessageBox('創建新數據表失敗,表名或字段名設置有誤!' + #13 +
'提示:表名或字段名不可以設置為諸如:' + #13 + 'User、Table、Money、From、Select等之類的SQL關鍵字!', '發生錯誤', 48);
end;
end;
procedure TNewDataForm.CbTypeKeyPress(Sender: TObject; var Key: Char);
begin
key := #0;
end;
procedure TNewDataForm.Button3Click(Sender: TObject);
begin
close;
end;
procedure TNewDataForm.Button1Click(Sender: TObject); {初始化新建數據表}
var
i, j: integer;
begin
try
for j := 1 to 2 do
begin
for i := 2 to SpinEdit1.Value + 1 do
begin
ED[i].Free;
LB[i].Free;
LB2[i].Free;
CBOX[i].Free;
Chk[i].Free;
end;
Num := 2;
SpinEdit1.Value := 1;
CbType.ItemIndex := 0;
ChkAuto.Checked := False;
ChkSet.Checked := False;
EdTableName.Clear;
EdFieldName.Clear;
Button2.Top := 75;
Button1.Top := 75;
Button3.Top := 75;
NewDataForm.Height := 150;
end;
except
end;
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -