共用方式為


SqlConnectionStringBuilder.AttachDBFilename 屬性

定義

取得或設定包含主要資料檔名稱的字串。 這包括可附加之資料庫的完整路徑名稱。

public:
 property System::String ^ AttachDBFilename { System::String ^ get(); void set(System::String ^ value); };
public string AttachDBFilename { get; set; }
member this.AttachDBFilename : string with get, set
Public Property AttachDBFilename As String

屬性值

AttachDBFilename 屬性的值,如果未提供任何值,則為 String.Empty

例外狀況

若要將值設定為 null,請使用 Value

範例

下列範例會建立新的 SqlConnectionStringBuilder 實例,並設定 AttachDBFilename 屬性以指定附加數據檔的名稱。

using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        try
        {
            string connectString =
                "Server=(local);" +
                "Integrated Security=true";
            SqlConnectionStringBuilder builder =
                new SqlConnectionStringBuilder(connectString);
            Console.WriteLine("Original: " + builder.ConnectionString);
            Console.WriteLine("AttachDBFileName={0}", builder.AttachDBFilename);

            builder.AttachDBFilename = @"C:\MyDatabase.mdf";
            Console.WriteLine("Modified: " + builder.ConnectionString);

            using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
            {
                connection.Open();
                // Now use the open connection.
                Console.WriteLine("Database = " + connection.Database);
            }
            Console.WriteLine("Press any key to finish.");
            Console.ReadLine();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}
Imports System.Data.SqlClient

Module Module1
    Sub Main()
        Try
            Dim connectString As String = _
             "Server=(local);" & _
             "Integrated Security=True"
            Dim builder As New SqlConnectionStringBuilder(connectString)
            Console.WriteLine("Original: " & builder.ConnectionString)
            Console.WriteLine("AttachDBFileName={0}", _
             builder.AttachDBFilename)
            builder.AttachDBFilename = "C:\MyDatabase.mdf"
            Console.WriteLine("Modified: " & builder.ConnectionString)

            Using connection As New SqlConnection(builder.ConnectionString)
                connection.Open()
                ' Now use the open connection.
                Console.WriteLine("Database = " & connection.Database)
            End Using

            Console.WriteLine("Press any key to finish.")
            Console.ReadLine()

        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub
End Module

備註

這個屬性會對應至連接字串中的 "AttachDBFilename"、"extended properties" 和 "initial file name" 索引鍵。

AttachDBFilename 僅支援具有.mdf擴展名的主要數據檔。

如果在與資料檔相同的目錄中有記錄檔存在,則會產生錯誤,而且在附加主要資料檔時會使用 'database' 關鍵字。 在此情形下,請移除此記錄檔。 一旦附加資料庫之後,就會根據實體路徑自動產生新的記錄檔。

適用於

另請參閱