SqlConnectionStringBuilder.AttachDBFilename Property

Definition

获取或设置包含主数据文件名称的字符串。Gets or sets a string that contains the name of the primary data file. 该字符串包括可附加数据库的完整路径名。This includes the full path name of an attachable database.

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

Property Value

String

AttachDBFilename 属性的值,或者,如果未提供任何值,则为 String.EmptyThe value of the AttachDBFilename property, or String.Empty if no value has been supplied.

Exceptions

若要将值设置为 null,请使用 ValueTo set the value to null, use Value.

Examples

下面的示例创建一个新的 SqlConnectionStringBuilder 实例,并设置 AttachDBFilename 属性以指定附加数据文件的名称。The following example creates a new SqlConnectionStringBuilder instance, and sets the AttachDBFilename property in order to specify the name of an attached data file.

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

Remarks

此属性与连接字符串内的“AttachDBFilename”、“extended properties”和“initial file name”键相对应。This property corresponds to the "AttachDBFilename", "extended properties", and "initial file name" keys within the connection string.

仅支持具有 .mdf 扩展名的主数据文件的 AttachDBFilenameAttachDBFilename is only supported for primary data files with an .mdf extension.

如果日志文件和数据文件存在于同一个目录中,并且在附加主数据文件时使用“database”关键字,则将生成错误。An error will be generated if a log file exists in the same directory as the data file and the 'database' keyword is used when attaching the primary data file. 在这种情况下,请移除日志文件。In this case, remove the log file. 附加数据库后,将基于物理路径自动生成一个新日志文件。Once the database is attached, a new log file will be automatically generated based on the physical path.

Applies to

See also