Tutorials to .com

Tutorials to .com » Database » Sqlserver » How to obtain all the SQLServer server LAN

How to obtain all the SQLServer server LAN

Print View , by: iSee ,Total views: 17 ,Word Count: 1596 ,Date: Mon, 24 Aug 2009 Time: 10:07 PM

How to obtain the LAN to all SQL Server servers
PAN id = ArticleContent1_ArticleContent1_lblContent>

How to obtain the LAN to all SQL Server servers

Literature reference sources: Delphi-depth exploration of

I've always wanted in my application on the SQL Server to obtain more detailed information. Until recently, the use of SQLDMO (SQL Distributed Management Objects) was able to realize this idea. SQLDMO provides a very powerful feature, we can use the program to achieve almost any SQL Server has a function. In this article I will show you how to get all the SQL Servers in the LAN server, how to connect, how to get all of the database server.

SQLDMO right, as from the SQL Server 2000 provides a dynamic link library SQLDMO.dll. The dll itself is a COM right image, first of all you need from the type library reference Microsoft SQLDMO Object Library (Version 8.0). delphi will be automatically generated for you SQLDMO_TLB.PAS files, including all the COM objects interfaces.

Here we need to note that due to the introduction of SQLDMO TDatabase and TApplication and several other Delphi default class name and class name conflicts own, so they can change into a _TypeName form. Or other name, I am here to change T_Application, T_Database so.

Our next step in our program, the introduction of unit file SQLDMO_TLB.PAS. The application module name is SqlServers

interface program runs as follows:

Server list is all of the SQL SERVER LAN server, select the server, enter a user name and password, drop-down list of databases, the program will list all the databases in this server.

Program source code is as follows:

unit SqlServers;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

StdCtrls, Buttons, ComCtrls, SQLDMO_TLB; / / Note that the introduction of this document do not forget to

type

TdmoObject = record

SQL_DMO: _SQLServer;

lConnected: boolean;

end;

type

TFormServersList = class (TForm)

Label1: TLabel;

Label2: TLabel;

CB_ServerNames: TComboBox;

CB_DataNames: TComboBox;

Label3: TLabel;

Label4: TLabel;

Ed_Login: TEdit;

Ed_Pwd: TEdit;

BitBtn1: TBitBtn;

BitBtn2: TBitBtn;

procedure FormCreate (Sender: TObject);

procedure FormCloseQuery (Sender: TObject; var CanClose: Boolean);

procedure FormClose (Sender: TObject; var Action: TCloseAction);

procedure FormShow (Sender: TObject);

procedure BitBtn2Click (Sender: TObject);

procedure CB_DataNamesDropDown (Sender: TObject);

private

server_Names: TStringList;

/ / Object collection

PdmoObject: array of TdmoObject;

/ / Get all of the remote server

Function GetAllServers (ServerList: TStringList): Boolean;

(Private declarations)

public

(Public declarations)

end;

var

FormServersList: TFormServersList;

implementation

($ R *. DFM)

(TForm1)

Function TFormServersList.GetAllServers (ServerList: TStringList): Boolean;

var

sApp: _Application;

sName: NameList;

iPos: integer;

begin

Result: = True;

try

sApp: = CoApplication_.Create; / / create an object without the release, delphi he would release

sName: = sApp.ListAvailableSQLServers;

except

Result: = False;

Exit;

end;

if sName.Count> 0 then / / The reason why iPos from a starting position because of a null value that is 0

for iPos: = 1 to sName.Count - 1 do

begin

CB_ServerNames.Items.Add (sName.Item (iPos));

ServerList.Add (sName.Item (iPos));

end;

end;

procedure TFormServersList.FormCreate (Sender: TObject);

var

lcv: integer;

begin

server_Names: = TStringList.Create;

if not GetAllServers (server_Names) then

begin

Application.MessageBox (can not get a list of servers, the client may be missing DLL library functions, error, MB_OK);

exit;

end;

for lcv: = 0 to server_Names.Count - 1 do

begin

SetLength (PdmoObject, lcv + 1);

with PdmoObject [lcv] do

begin

SQL_DMO: = CoSQLServer.Create;

SQL_DMO.Name: = Trim (server_Names [lcv]);

/ / Login security attributes, NT Authentication

SQL_DMO.LoginSecure: = false;

/ / Set a connection timeout

SQL_DMO.LoginTimeout: = 3;

/ / Automatically re-login, if the first failure

SQL_DMO.AutoReconnect: = true;

SQL_DMO.ApplicationName: = server_Names [lcv];

lConnected: = false;

end;

end;

end;

procedure TFormServersList.FormCloseQuery (Sender: TObject; var CanClose: Boolean);

begin

server_Names.Free;

end;

procedure TFormServersList.FormClose (Sender: TObject; var Action: TCloseAction);

begin

Action: = CaFree;

end;

procedure TFormServersList.FormShow (Sender: TObject);

begin

if CB_ServerNames.Items.Count> 0 then / / enumerate all the server name

CB_ServerNames.Text: = CB_ServerNames.Items.Strings [0];

end;

procedure TFormServersList.BitBtn2Click (Sender: TObject);

begin

Close;

end;

procedure TFormServersList.CB_DataNamesDropDown (Sender: TObject);

var

icount, Server_B: integer;

begin

CB_DataNames.Clear;

Screen.Cursor: = CrHourGlass;

Server_B: = CB_ServerNames.Items.IndexOf (CB_ServerNames.Text);

with PdmoObject [Server_B]. SQL_DMO do

begin

if not PdmoObject [Server_B]. lConnected then

try

Connect (Name, Trim (Ed_Login.Text), Trim (Ed_Pwd.Text));

except

Screen.Cursor: = CrDefault;

Application.MessageBox (Please check the user name or password is correct, the connection fails, MB_OK);

Exit;

end;

if not VerifyConnection (SQLDMOConn_ReconnectIfDead) then

begin

ShowMessage (in trying to connect to SQL SERVER 2000 Error + # 10 # 13 +

Sure whether the increase in the dynamic link library SQLDMO.DLL);

exit;

end else

PdmoObject [Server_B]. LConnected: = True;

Databases.Refresh (true);

for icount: = 1 to Databases.Count do

CB_DataNames.Items.Add (Databases.Item (icount, null). Name);

end;

Screen.Cursor: = CrDefault;

end

end.


Microsoft SQL Server Tutorial Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.