连接数据库程序调测 ( 积分: 50 )

  • 主题发起人 主题发起人 farday
  • 开始时间 开始时间
F

farday

Unregistered / Unconfirmed
GUEST, unregistred user!
以下那里写错能,能指正吗?有什么写的不好的,也请指出,谢谢!
还有怎么用让DBGrid1显示序号呢?网上的文章多,但没找到一个满意的



unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants,IniFiles, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls, Grids, DBGrids;
const
IniFileName = 'config.ini';

type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
Label1: TLabel;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
procedure FormCreate(Sender: TObject);



private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
server,database,sa,password,SqlStr:string;
Startdb_flag:boolean;
function ReadConfigFile: Boolean;
function InitDatabase(aADOConnect:TADOConnection;database,sa, password,server: string): Boolean;
procedure db1();
procedure Connectdb();

implementation


{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin

If (Not ReadConfigFile) Then
Begin
Showmessage('配置文件读取错误,程序将关闭!');
Exit;
End;
Connectdb();
end;

function ReadConfigFile: Boolean;
Var
IniHandle:TIniFile;
Begin
Result:=False;
//************************************************************************************************
If FileExists(ExtractFilePath(Application.ExeName)+IniFileName) Then
Begin
IniHandle:=TIniFile.Create(ExtractFilePath(Application.ExeName)+IniFileName);
With IniHandle Do
Begin
server :=ReadString('DataBase','server','');
database :=ReadString('DataBase','database','');
sa :=ReadString('DataBase','sa','');
password :=ReadString('DataBase','password','');
Result:=True;
End;
End
End;

procedure Connectdb();
begin
Startdb_flag := true;
Try
if not InitDatabase(form1.ADOConnection1,database, sa, password,server) then
begin
Startdb_flag := False;
form1.Label1.Caption:='连接主数据库不成功:'+database;
exit;
end
Except
exit
End;
end;

function InitDatabase(aADOConnect:TADOConnection;database,sa, password,server: string): Boolean;
begin
try
with aADOConnect do
begin
connected:=false;
ConnectionString := 'Provider=SQLOLEDB.1;Persist Security Info=False;User ID='+sa+';Password='+password+';Initial Catalog='+database+';Data Source='+server+';Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=LYP';
Connected := True;
Result := True;
form1.Label1.Caption:='连接主数据库成功:'+database; //'DB连接成功:'+ Amachine_name+'; //+Amachine_name+''+TNSNAME+ATNSName';
end;
except
form1.Label1.Caption:='异常';
end;
end;

procedure db1();
begin
Connectdb();
form1.ADOQuery1.Close;
form1.ADOQuery1.SQL.Clear;
SqlStr:='SELECT * from bill';
form1.ADOQuery1.SQL.add(SqlStr);
form1.ADOQuery1.Open;
form1.ADOQuery1.Active:=false;
form1.ADOQuery1.Active:=true;

end;
end.
 
后退
顶部