mencari sql server yang ada di LAN


Compiler : Delphi

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, DB, ADODB, Grids, DBGrids;

type

TFKoneksiMSSever = class(TForm)

adoList: TADOQuery;

edUser: TEdit;

Label1: TLabel;

Label2: TLabel;

cboDatabase: TComboBox;

optSecurity: TRadioGroup;

Label3: TLabel;

Label4: TLabel;

cboServer: TComboBox;

edPass: TEdit;

procedure FormCreate(Sender: TObject);

procedure cboServerChange(Sender: TObject);

procedure optSecurityClick(Sender: TObject);

private

{ Private declarations }

function GetConn : WideString;

procedure ListServer(vServer: TStrings);

public

{ Public declarations }

end;

var

FKoneksiMSSever: TFKoneksiMSSever;

implementation

{$R *.dfm}

uses ActiveX, ComObj, AdoInt, OleDB;

{ TForm1 }

{ — credit ———————————————————

thanks to

http://www.delphi.about.com – Zarko Gajic

prosedur untuk mendapatkan list server dari Ms SQL Server 2000

—————————————————————– }

procedure TFKoneksiMSSever.ListServer(vServer: TStrings);

var

RSCon: ADORecordsetConstruction;

Rowset: IRowset;

SourcesRowset: ISourcesRowset;

SourcesRecordset: _Recordset;

SourcesName, SourcesType: TField;

function PtCreateADOObject(const ClassID: TGUID): IUnknown;

var

Status: HResult;

FPUControlWord: Word;

begin

asm

FNSTCW FPUControlWord

end;

Status := CoCreateInstance(

CLASS_Recordset,

nil,

CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER,

IUnknown,

Result);

asm

FNCLEX

FLDCW FPUControlWord

end;

OleCheck(Status);

end;

begin

SourcesRecordset := PtCreateADOObject(CLASS_Recordset) as _Recordset;

RSCon := SourcesRecordset as ADORecordsetConstruction;

SourcesRowset := CreateComObject(ProgIDToClassID(‘SQLOLEDB Enumerator’)) as ISourcesRowset;

OleCheck(SourcesRowset.GetSourcesRowset(nil, IRowset, 0, nil, IUnknown(Rowset)));

RSCon.Rowset := RowSet;

with TADODataSet.Create(nil) do

try

Recordset := SourcesRecordset;

SourcesName := FieldByName(‘SOURCES_NAME’); { do not localize }

SourcesType := FieldByName(‘SOURCES_TYPE’); { do not localize }

vServer.BeginUpdate;

try

while not EOF do

begin

if (SourcesType.AsInteger = DBSOURCETYPE_DATASOURCE) and (SourcesName.AsString <> ”) then

vServer.Add(SourcesName.AsString);

Next;

end;

finally

vServer.EndUpdate;

end;

finally

Free;

end;

end;

procedure TFKoneksiMSSever.FormCreate(Sender: TObject);

begin

optSecurityClick(optSecurity);

ListServer(cboServer.Items);

end;

function TFKoneksiMSSever.GetConn: WideString;

begin

// milih koneksi apa pake password atau yang enggak

case optSecurity.ItemIndex of

0 : begin

Result :=

‘Provider=SQLOLEDB.1;Integrated Security=SSPI;’+

‘Persist Security Info=False;Data Source=’+cboServer.Text;

end;

1 : begin

Result :=

‘Provider=SQLOLEDB.1;Password=’+edPass.Text+

‘;Persist Security Info=True;User ID=’+edUser.Text+

‘;Data Source=’+cboServer.Text;

end;

end;

end;

{ — credit ———————————————————

thanks to

http://www.swissdelphicenter.ch – Mostafa Sarbazzadeh

prosedur untuk mendapatkan list dari database SQL Server 2000

—————————————————————– }

procedure TFKoneksiMSSever.cboServerChange(Sender: TObject);

begin

cboDatabase.Clear;

with adoList do

begin

ConnectionString := GetConn;

SQL.Clear;

SQL.Append(‘Exec SP_DATABASES’);

try

Open;

cboDatabase.Clear;

repeat

cboDatabase.Items.Append(Fields[0].AsString);

Next;

until adoList.Eof;

Close;

except

on e:exception do

MessageDlg(e.Message,mtError, [mbOK],0);

end;

end;

end;

procedure TFKoneksiMSSever.optSecurityClick(Sender: TObject);

begin

edUser.Enabled := optSecurity.ItemIndex=1;

edPass.Enabled := optSecurity.ItemIndex=1;

If edUser.Enabled Then edUser.SetFocus;

end;

end.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: