如何使用asp.net将数据库文件附加到sql server management studio
问题描述:
我有一个数据库文件,在我的项目中必须要求通过代码将此数据库附加到sql server auto.请帮助我,非常感谢.
i have a database file, in my project must requiment attach this database to sql server auto by code.Help me please and thanks so much.
答
嗨
如果我的理解是正确的,则需要使用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.