Server.AttachDatabase Method (String, StringCollection, AttachOptions)

Attaches an existing database that is made up of one or more files to the instance of SQL Server that has the specified name and using the specified data files and attachment options. 

Namespace:  Microsoft.SqlServer.Management.Smo
Assembly:  Microsoft.SqlServer.Smo (in Microsoft.SqlServer.Smo.dll)


Public Sub AttachDatabase ( _
    name As String, _
    files As StringCollection, _
    attachOptions As AttachOptions _
Dim instance As Server 
Dim name As String 
Dim files As StringCollection 
Dim attachOptions As AttachOptions

instance.AttachDatabase(name, files, attachOptions)
public void AttachDatabase(
    string name,
    StringCollection files,
    AttachOptions attachOptions
void AttachDatabase(
    String^ name, 
    StringCollection^ files, 
    AttachOptions attachOptions
member AttachDatabase : 
        name:string * 
        files:StringCollection * 
        attachOptions:AttachOptions -> unit
public function AttachDatabase(
    name : String, 
    files : StringCollection, 
    attachOptions : AttachOptions


  • name
    Type: System.String
    A String value that specifies the name of the database to be attached.


The data and transaction log files of a database can be detached and then reattached to the same or another instance of SQL Server. Detaching and attaching a database is useful if you want to change the database to a different instance of SQL Server on the same computer, or if you want to move the database.

See Also


Server Class

AttachDatabase Overload

Microsoft.SqlServer.Management.Smo Namespace

Other Resources

Calling Methods

Managing Servers