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”关键字,则将生成错误。 在这种情况下,请移除日志文件。 附加数据库后,将基于物理路径自动生成一个新日志文件。

适用于

另请参阅