Server.AttachDatabase Method (String, StringCollection)

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

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

Syntax

'Declaration
Public Sub AttachDatabase ( _
    name As String, _
    files As StringCollection _
)
'Usage
Dim instance As Server
Dim name As String
Dim files As StringCollection

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

Parameters

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

Remarks

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.

Examples

Visual Basic

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
Dim owner As String
Dim logstr as String
Dim datastr as String
owner = srv.Databases("AdventureWorks2008R2").Owner

'Detach the AdventureWorks2008R2 database.
srv.DetachDatabase("AdventureWorks2008R2", False, False)

'Display information about the detached database.
Dim d As DataTable
datastr = "C:\Program Files\Microsoft SQL Server"
datastr = datastr + "\MSSQL10_50\MSSQL\Data\AdventureWorks2008R2_Data.mdf"
logstr = "C:\Program Files\Microsoft SQL Server"
logstr = logstr + "\MSSQL10_50\MSSQL\Data\AdventureWorks2008R2_Log.ldf"
d = srv.DetachedDatabaseInfo(datastr)
Dim r As DataRow
Dim c As DataColumn
For Each r In d.Rows
    Console.WriteLine("==========================")
    For Each c In r.Table.Columns
        Console.WriteLine(c.ColumnName + " = " + r[c].ToString)
    Next
Next

'Check whether the file is a detached primary file.
Console.WriteLine(srv.IsDetachedPrimaryFile(datastr))

'Attach the database
Dim sc As StringCollection
sc = New StringCollection
sc.Add(datastr)
sc.Add(logstr)
srv.AttachDatabase("AdventureWorks2008R2", sc, owner, AttachOptions.None)

PowerShell

$srv = new-object Microsoft.SqlServer.Management.Smo.Server("(local)")
$db = New-Object Microsoft.SqlServer.Management.Smo.Database
$db = $srv.Databases.Item("AdventureWorks2008R2")
$owner = $db.Owner
$srv.DetachDatabase("AdventureWorks2008R2", $FALSE, $FALSE)
$datastr = "C:\Program Files\Microsoft SQL Server"
$datastr = $datastr + "\MSSQL10_50\MSSQL\Data\AdventureWorks2008R2_Data.mdf"
$logstr = "C:\Program Files\Microsoft SQL Server"
$logstr = $logstr + "\MSSQL10_50\MSSQL\Data\AdventureWorks2008R2_Log.ldf"
$d = $srv.DetachedDatabaseInfo($datastr)
foreach ($r in $d.Rows)
{
   Write-Host "=========================="
   Foreach ($c in $d.Columns)
   {
      Write-Host $c.ColumnName "=" $r[$c].ToString()
   }
}
Write-Host $srv.IsDetachedPrimaryFile($datastr)
$sc = new-object Systems.Collections.Specialized.StringCollection
$sc.Add($datastr)
$sc.Add($logstr)
$srv.AttachDatabase("AdventureWorks2008R2", $sc, $owner, [Microsoft.SqlServer.Management.Smo.AttachOptions]::None)