且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用asp.net将数据库文件附加到sql server management studio

更新时间:2023-02-03 10:34:47


如果我的理解是正确的,则需要使用C#将数据库备份文件附加到sql服务器.能否请您检查下面的代码.这可能对您有帮助.我无法编译代码.

Hi
If my understanding is correct, you need to attach a database backup file to the sql server using C#. Can you please check the below code. This may help you. I couldn''t compile the code.

using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

  string ConnectionString = "Your connections string";
            SqlConnection conSql = new SqlConnection(ConnectionString);
            ServerConnection con = new ServerConnection(conSql);
            Server server = new Server(con);
            System.Collections.Specialized.StringCollection sCollection = new    System.Collections.Specialized.StringCollection();
            sCollection.Add("filePath");
            server.AttachDatabase("newdatabasename", sCollection);



To get the namespace, please refer the following dlls.
1. C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.ConnectionInfo.dll

2. C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.ConnectionInfoExtended.dll

3. C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.Management.Utility.dll

4. C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.Management.UtilityEnum.dll

5.C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.Smo.dll

6.C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.SmoExtended.dll


NB:
This all dlls may not need. Actually i didn't check all. And also there may be better mechanisms to do this job.