如何遍历网络上所有的SQL Server 服务器名列表,以及怎样在取得的Server服务器名上取得所有的数据库列表(100分)

  • 主题发起人 主题发起人 CashChin
  • 开始时间 开始时间
C

CashChin

Unregistered / Unconfirmed
GUEST, unregistred user!
如何遍历网络上所有的SQL Server 服务器名列表,以及怎样在取得的Server服务器名上取得所有的数据库列表
 
怎样在取得的Server服务器名上取得所有的数据库列表:
use master
select name from sysdatabases
 
获得所有的服务器
var
oSQLServerDMOApp :variant
begin
oSQLServerDMOApp :=createoleobject('SQLDMO.Application')

使用oSQLServerDMOApp.ListAvailableSQLServers的count方法获得服务器数量
再用item方法获得名称
别忘了先导入sqldmo
 
hpboy:能否给出具体的SQLDMO导入的源代码?
 
能不用sqldmo?
 
上述的方法可能不很有效,间接一点,可以看网络上的计算机有哪些
打开了1433端口就可以了,一般SQL服务器均打开的是1433
 
{*******************************************************}
{ }
{ 数据库的操作 }
{ 作者: }
{ }
{ 日期:2002-09-10 }
{ 引用了SQLDMO_TLB单元,只有安装sql server才能引入 }
{ 如果不delphi引入则须把dcu文件加入到工程中即可 }
{ }
{*******************************************************}
unit Umain;

interface

uses
Windows, Messages, SysUtils, Variants,SQLDMO_TLB,Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TFrmDataManager = class(TForm)
PnlTitle: TPanel;
ImgTitle: TImage;
LblTitle1: TLabel;
LblTitle2: TLabel;
ImgServer: TImage;
CmbxServer: TComboBox;
LblServer: TLabel;
BvlCon: TBevel;
LblConnection: TLabel;
RadioBtnWin: TRadioButton;
RadioBtnSql: TRadioButton;
LblUser: TLabel;
LblPassWord: TLabel;
EdtUser: TEdit;
EdtPassWord: TEdit;
LblDatabase: TLabel;
BvlData: TBevel;
LblDatabaseLst: TLabel;
CmbxDatabase: TComboBox;
BvlOperate: TBevel;
BtnConnection: TButton;
BtnBackUp: TButton;
BtnRestore: TButton;
BtnExit: TButton;
SaveDlg: TSaveDialog;
OpenDlg: TOpenDialog;
procedure BtnExitClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure RadioBtnWinClick(Sender: TObject);
procedure RadioBtnSqlClick(Sender: TObject);
procedure BtnConnectionClick(Sender: TObject);
procedure CmbxDatabaseDropDown(Sender: TObject);
procedure CmbxDatabaseCloseUp(Sender: TObject);
procedure BtnBackUpClick(Sender: TObject);
procedure BtnRestoreClick(Sender: TObject);
private
{ Private declarations }
procedure SetBtnState(value:Boolean);
procedure SetEdtState;
public
{ Public declarations }
Connected:boolean;
end;

var
FrmDataManager: TFrmDataManager;
App:_application;
Backup :_Backup;
Restore :_Restore;

implementation

uses reg;

{$R *.dfm}

procedure TFrmDataManager.BtnExitClick(Sender: TObject);
begin
Close;
end;

procedure TFrmDataManager.SetBtnState(value: Boolean);
begin
BtnBackUp.Enabled:=Value;
BtnRestore.Enabled:=Value;
end;

procedure TFrmDataManager.SetEdtState;
begin
EdtUser.Enabled:=False;
EdtPassWord.Enabled:=False;
EdtUser.Color:=clBtnFace;
EdtPassWord.Color:=clBtnFace;
if RadioBtnSql.Checked then
begin
EdtUser.Enabled:=True;
EdtPassWord.Enabled:=True;
EdtUser.Color:=clWhite;
EdtPassWord.Color:=clWhite;
end;
end;

procedure TFrmDataManager.FormCreate(Sender: TObject);
var
i:integer;
nl:namelist;
Server:_sqlserver;
begin
Connected:=False;
BtnConnection.Enabled:=False;
SetBtnState(Connected);
App:=coapplication.Create;
Server:=cosqlserver.Create ;
nl:=App.ListAvailableSQLServers;
CmbxServer.Clear;
for i:=0 to nl.Count-1 do
begin
CmbxServer.Items.Add(nl.Item(i+1) )
end;
CmbxServer.ItemIndex:=0;
end;

procedure TFrmDataManager.RadioBtnWinClick(Sender: TObject);
begin
SetEdtState;
end;

procedure TFrmDataManager.RadioBtnSqlClick(Sender: TObject);
begin
SetEdtState;
end;

procedure TFrmDataManager.BtnConnectionClick(Sender: TObject);
var
ConStr:string;
begin
Connected:=False;

if RadioBtnWin.Checked then
begin
ConStr:=BaseConStr+'Integrated Security=SSPI;Persist Security Info=False;'+
'Initial Catalog='+CmbxDatabase.Text +';Data Source='+
CmbxServer.Text;
end
else
begin
ConStr:=BaseConStr+'Password='+EdtPassWord.Text
+';Persist Security Info=True;User ID='+EdtUser.Text
+';Initial Catalog='+CmbxDatabase.Text
+';Data Source='+CmbxServer.Text;
end;

Connected:=True;
{$IFNDEF BizUnit}
SetRegString(HKEY_LOCAL_MACHINE, MyRegSection, MyRegKey_Connection, ConStr);
{$ENDIF}

SetBtnState(Connected);

end;

procedure TFrmDataManager.CmbxDatabaseDropDown(Sender: TObject);
var
i:integer;
Server:_sqlserver;
Dtbase:_database;
begin
BtnConnection.Enabled:=False;
Server:=cosqlserver.Create ;
Dtbase:=codatabase.Create;
CmbxDatabase.Clear;
Server.LoginTimeout:=-1;
Screen.Cursor := crHourGlass;

try
if RadioBtnWin.Checked then
begin
With Server do
begin
LoginSecure:=true;
AutoReConnect:= False;
Connect(CmbxServer.Text,null,null);
end;
end
else
begin
With Server do
begin
LoginSecure := False;
AutoReConnect := False;
Connect(CmbxServer.Text,EdtUser.Text, EdtPassWord.Text);
end;
end;
CmbxDatabase.Clear;
for i:=1 to Server.Databases.Count do
begin
CmbxDatabase.Items.Add(Server.Databases.Item(i,'').Name);
end;

except
Application.MessageBox('连接数据库失败!', PChar(Self.Caption), MB_OK or MB_ICONSTOP);
EdtUser.SetFocus;
end;
Screen.Cursor := crDefault;
end;

procedure TFrmDataManager.CmbxDatabaseCloseUp(Sender: TObject);
begin
if CmbxDatabase.ItemIndex>=0 then BtnConnection.Enabled:=True;
end;

procedure TFrmDataManager.BtnBackUpClick(Sender: TObject);
var
Server:_sqlserver;
Backup:_Backup;
begin
Server:=cosqlserver.Create ;
Server.LoginTimeout:=-1;
if SaveDlg.Execute then
begin
try
Screen.Cursor := crHourGlass;
if RadioBtnWin.Checked then
begin
With Server do
begin
LoginSecure:=true;
AutoReConnect:= False;
Connect(CmbxServer.Text,null,null);
end;
end
else
begin
With Server do
begin
LoginSecure := False;
AutoReConnect := False;
Connect(CmbxServer.Text,EdtUser.Text, EdtPassWord.Text);

end;
end;


Backup :=coBackup.Create ;
Backup.action:=SQLDMOBackup_DATABASE;
Backup.Database:=CmbxDatabase.Text;
Backup.Files := SaveDlg.FileName;

Backup.SQLBackup(Server);
Screen.Cursor := crDefault;
Application.MessageBox(PChar('数据库 '+CmbxDatabase.Text +' 备份成功'), PChar(Self.Caption), MB_OK or MB_ICONINFORMATION);
except
Screen.Cursor := crDefault;
Application.MessageBox(PChar('数据库 '+CmbxDatabase.Text+' 备份失败'), PChar(Self.Caption), MB_OK or MB_ICONSTOP);
end;

Screen.Cursor := crDefault;
end;
end;

procedure TFrmDataManager.BtnRestoreClick(Sender: TObject);
var
Server:_sqlserver;
Restore:_Restore;
begin
Server:=cosqlserver.Create ;
Server.LoginTimeout:=-1;
if OpenDlg.Execute then
begin
try
Screen.Cursor := crHourGlass;
if RadioBtnWin.Checked then
begin
With Server do
begin
LoginSecure:=true;
AutoReConnect:= False;
Connect(CmbxServer.Text,null,null);
end;
end
else
begin
With Server do
begin
LoginSecure := False;
AutoReConnect := False;
Connect(CmbxServer.Text,EdtUser.Text, EdtPassWord.Text);
end;
end;

Restore :=coRestore.create;
Restore.Replacedatabase:=true;
Restore.action:=SQLDMORESTORE_DATABASE;
Restore.Database:=CmbxDatabase.Text;
Restore.Files := OpenDlg.FileName;
Restore.SQLRestore(Server);
Screen.Cursor := crDefault;
Application.MessageBox(PChar('数据库 '+CmbxDatabase.Text +' 恢复成功'), PChar(Self.Caption), MB_OK or MB_ICONINFORMATION);
except
Screen.Cursor := crDefault;
Application.MessageBox(PChar('数据库 '+CmbxDatabase.Text+' 恢复失败'), PChar(Self.Caption), MB_OK or MB_ICONSTOP);
end;

Screen.Cursor := crDefault;
end;
end;

end.
 
怎样获得SQLDMO_TLB文件
 
SQLDMO 对像来自 SQL Server 2000 提供的动态连接库 SQLDMO.dll,安装sql2000就会带或者到CD中找出来注册一下`~然后~~你要在IMPORT TYPE library 里找到了Microsoft SQLDMO Object Library (Version 8.0). SQLDMO.DLL,然后create unit生成pas
在代码中引用就可以了,文件中包括了所有 COM 对象的接口。
要注意,由于引入的SQLDMO “TDatabase”和 “TApplication”和其它几个缺省类名与 Delphi 自带的类名冲突,所以自己可以修改成 _TypeName 的形式。如果发生

[Error] SQLDMO_TLB.pas(12625): Identifier redeclared: 'ID'
[Fatal Error] Unit1.pas(8): Could not compile used unit 'SQLDMO_TLB.pas'
可以注释掉```或改ID
 
怎么样才能加入.DCU呢。
 
但是在没有安装sql2000的机子上,就不行了》。。会出现找不到模块这个错误!!
 

Similar threads

后退
顶部