procedure TForm1.OpenO1Click(Sender: TObject);//File->open..
begin
if OpenDialog1.Execute then
Edit1.Text:=OpenDialog1.FileName;
end;
procedure TForm1.BBTNOpenClick(Sender: TObject);//打开数据库
var szPath:string; //用来接收输入的路径
begin
ListBox1.Clear;
ADOConnection1.Close;
ADOQuery1.Close;
ADOQuery1.Active:=False;
//LTableList_ItemIndex:=0;
if Trim(ComboBox1.Text)<>'' then
begin
DataSource1.DataSet:=ADOQuery1;
ADOConnection1.LoginPrompt:=false;
try
if Trim(ComboBox1.Text)='OLEDB.4.0 *.mdb' then
begin
ADOConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+Edit1.Text+';Persist Security Info=False;Jet OLEDB:Database Password='+EdtPwd.Text+'';
end else
if Trim(ComboBox1.Text)='SQL Server' then //要求IP,数据库 登陆用户名和密码
begin
szPath:='(Local)';
if not InputQuery('SQL Server','请输入你要连接到的SQL服务器名或IP 本地可用 (Local) 或 127.0.0.1 ',szPath) then exit;
Edit1.text:=Trim(szPath);
szPath:='pubs';
if not InputQuery('数据库选择','请输入你要打开的数据库名,如:pubs ',szPath) then exit;
ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password='+EdtPwd.Text+';Persist Security Info=True;User ID='+EdtUser.Text+';Initial Catalog='+szPath+';Data Source='+Edit1.text+'';
end;
/
ADOConnection1.Open;
ADOConnection1.GetTableNames(Listbox1.Items);
StatusBar1.SimpleText:='连接数据库成功!';
except
application.MessageBox('连接数据库失败!','错误',0);
Edit1.Color:=clRed;
end;
end;
end;
procedure TForm1.BBTNExecSqlClick(Sender: TObject);
begin
if EdtSql.Text<>'' then
try
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.Connection:=ADOConnection1;
ADOQuery1.SQL.Add(Pchar(trim(EdtSql.text)));
if LeftStr(trim(EdtSql.text),6)<>'select' then
ADOQuery1.ExecSQL
else ADOQuery1.Open;
ListBox1.Items.Clear;
ADOConnection1.GetTableNames(Listbox1.Items);
DataSource1.DataSet:=ADOQuery1;
DBGrid1.DataSource:=DataSource1;
except
showmessage('SQL命令语法有误,没有成功执行!');
end;
end;
procedure TForm1.ListBox1Click(Sender: TObject);
begin
if Listbox1.ItemIndex <> -1 then
begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.Connection:=ADOConnection1;
ADOQuery1.SQL.Add(Pchar('Select * from '+Listbox1.Items[Listbox1.ItemIndex]+';'));
try
ADOQuery1.Open;
except
application.MessageBox('数据表有问题,无法正常打开!','提示',0);
end;
DataSource1.DataSet:=ADOQuery1;
DBGrid1.DataSource:=DataSource1;
end;
end;
procedure TForm1.AboutA1Click(Sender: TObject);
begin
Form2.ShowModal;
end;
end.