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 instance 만들고 연결된 데이터 파일의 이름을 지정하기 위해 속성을 설정합니다AttachDBFilename.

using Microsoft.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);
        }
    }
}

설명

이 속성은 연결 문자열 내의 "AttachDBFilename", "extended properties" 및 "initial file name" 키에 해당합니다.

AttachDBFilename 는 .mdf 확장자를 사용하는 기본 데이터 파일에 대해서만 지원됩니다.

연결 문자열에 AttachDBFileName 키 값을 지정하면 데이터베이스가 연결되고 연결의 기본 데이터베이스가 됩니다.

이 키를 지정하지 않고 데이터베이스가 이전에 연결된 경우 데이터베이스가 다시 연결되지 않습니다. 이전에 연결된 데이터베이스는 연결의 기본 데이터베이스로 사용됩니다.

이 키를 AttachDBFileName 키와 함께 지정하면 이 키의 값이 별칭으로 사용됩니다. 그러나 다른 연결된 데이터베이스에서 이름이 이미 사용되는 경우 연결이 실패합니다.

경로는 DataDirectory 대체 문자열을 사용하여 절대 또는 상대 경로일 수 있습니다. DataDirectory를 사용하는 경우 데이터베이스 파일은 대체 문자열이 가리키는 디렉터리의 하위 디렉터리 내에 있어야 합니다. 참고: 원격 서버, HTTP 및 UNC 경로 이름은 지원되지 않습니다.

데이터베이스 이름은 다음과 같이 키워드(keyword) 'database'(또는 해당 별칭 중 하나)로 지정해야 합니다.

"AttachDbFileName=|DataDirectory|\data\YourDB.mdf;integrated security=true;database=YourDatabase"

로그 파일이 데이터 파일과 같은 디렉터리에 있고 주 데이터 파일을 연결할 때 'database' 키워드가 사용되면 오류가 생성됩니다. 이 경우에는 로그 파일을 제거해야 합니다. 데이터베이스가 연결되면 새 로그 파일이 실제 경로에 따라 자동으로 생성됩니다.

적용 대상

추가 정보