首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

怎么在客户端让用户选择SQL Server服务器的文件夹

2012-03-11 
如何在客户端让用户选择SQL Server服务器的文件夹?就像企业管理器中备份/还原数据库一样,选择备份设备位置

如何在客户端让用户选择SQL Server服务器的文件夹?
就像企业管理器中备份/还原数据库一样,选择备份设备位置,操作者可以选择sql   server服务器里的文件夹。

[解决办法]
If you have Enterprise manager (SQL Server tools) installed on the machine, you may activate any Enterprise Manager function or menu from within your appliction using SQLNS and SQLDMO.

The good thing is it is easy to make backup as well as restore since it works exactly the same way as using the Enterprise manager. The downside is it will only work if the tools are installed on the client machine.

1) Choose project, Import typelibrary.
2) Look for "Microsoft SQLNamespace Object Library " in the list.
3) Choose install or create unit depending on if you want it as a component or just access using TLB unit as I have.
4) Include SQLNS.pas in uses of your unit.
5) Now do the same 4 steps with "Microsoft SQLDMO Object library " (SQLDMO.pas)

First operation is then to connect to the SQL server...

uses
SQLNS, SQLDMO;

var
FNameSpace:SQLNamespace; // This one needs to be global or part of class as it needs to be accessed by other functions.

var
ConString:OLEVariant;
Hnd:Integer;
FServerName, FPass, FUser: String;

// strings are to be assigned with the password, username and servername to then make part of connection string...

ConString:= 'Server= '+FServerName+ ';UID= '+FUser+ ';PWD= '+FPass+ ';Trusted_Connection=No ';

// The connect operation also requires a window handle. As I had this call within a component I used the one below. If you have a form then Hnd:=Form.Handle instead.

Hnd:=(Owner as TWinControl).Handle;

// Now make the connection...

FNameSpace:= CoSQLNamespace.Create;
FNameSpace.Initialize(FAppName,SQLNSRootType_Server,ConString,Hnd);

// Include the function below to navigate easy in the namespace...

function GetDBNameSpaceObject(
const aDBName: String): SQLNamespaceObject;
var
hServer,hDatabases,hDatabase:Integer;
begin
Result:=nil;
hServer:=FNameSpace.GetRootItem;
hDatabases:=FNameSpace.GetFirstChildItem(hServer,SQLNSOBJECTTYPE_DATABASES, ' ');
hDatabase:=FNameSpace.GetFirstChildItem(hDatabases,SQLNSOBJECTTYPE_DATABASE,aDBName);
Result:=FNameSpace.GetSQLNameSpaceObject(hDatabase);
end;

// Now you may use this function to activate the "database backup menu " for a particular database on the server...

procedure ShowBackupDB(const aDBName: String);
var
DB:SQLNamespaceObject;
Hnd:Integer;
begin
Hnd:=(Owner as TWinControl).Handle;
DB:=GetDBNameSpaceObject(aDBName);
DB.ExecuteCommandByID(SQLNS_CmdID_DATABASE_BACKUP,Hnd,0);
DB:=nil;
end;

// To show "restore menu ", use this procedure...

procedure ShowRestoreDB(const aDBName: String);
var
DB:SQLNamespaceObject;
Hnd:Integer;
begin
Hnd:=(Owner as TWinControl).Handle;
DB:=GetDBNameSpaceObject(aDBName);
DB.ExecuteCommandByID(SQLNS_CmdID_DATABASE_RESTORE,Hnd,0);
DB:=nil;
end;

// To make a backup without showing any interface, use this procedure...

{
RestoreType=SQLDMORestore_Database,
SQLDMORestore_Files,
SQLDMORestore_Log
DeviceName=Valid name of a backup-device
}

procedure RecordBackup(const aDBName: string;
BackupType: SQLDMO_BACKUP_TYPE; const DeviceName: string;
Initialize: Boolean);
var
ThisBackup:_Backup;
begin
try
ThisBackup:=coBackup.Create;


except
Messagedlg( 'coBackup.Create failed, the object might not be installed on this machine ',mtError,[mbOK],0);
end;
ThisBackup.Initialize:=Initialize;
ThisBackup.Database:=aDBName;
ThisBackup.Devices:= '[ '+DeviceName+ '] ';
ThisBackup.SQLBackup(FServer);
ThisBackup:=nil;
end;

// To restore without interface, use this procedure...

procedure TNSConnection.RestoreBackup(const aDBName: string;
RestoreType: SQLDMO_RESTORE_TYPE; const DeviceName: string);
var
ThisRestore:_Restore;
begin
try
ThisRestore:=coRestore.Create;
except
Messagedlg( 'coRestore.Create failed, the object might not be installed on this machine ',mtError,[mbOK],0);
end;
ThisRestore.Database:=aDBName;
ThisRestore.Devices:= '[ '+DeviceName+ '] ';
ThisRestore.Action:=RestoreType;
ThisRestore.SQLRestore(FServer);
end;
[解决办法]
建议如下:
1\首先读取SQL SERVER数据库中所有数据名称
2\把数据库名称读入列表框中;
3\然后利用SQL语句来对相应的数据库操作(如备份/还原等)

具体SQL怎么样写,SQL SERVER帮助里面写的比较详细的.
[解决办法]
SQL DMO组件获取服务器列表

热点排行