Bagikan melalui


SqlConnectionStringBuilder.AttachDBFilename Properti

Definisi

Mendapatkan atau mengatur string yang berisi nama file data utama. Ini termasuk nama jalur lengkap database yang dapat dilampirkan.

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

Nilai Properti

Nilai AttachDBFilename properti , atau String.Empty jika tidak ada nilai yang disediakan.

Pengecualian

Untuk mengatur nilai ke null, gunakan Value.

Contoh

Contoh berikut membuat instans baru SqlConnectionStringBuilder , dan mengatur AttachDBFilename properti untuk menentukan nama file data terlampir.

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

Keterangan

Properti ini sesuai dengan kunci "AttachDBFilename", "extended properties", dan "initial file name" dalam string koneksi.

AttachDBFilename hanya didukung untuk file data utama dengan ekstensi .mdf.

Kesalahan akan dihasilkan jika file log ada di direktori yang sama dengan file data dan kata kunci 'database' digunakan saat melampirkan file data utama. Dalam hal ini, hapus file log. Setelah database dilampirkan, file log baru akan dibuat secara otomatis berdasarkan jalur fisik.

Berlaku untuk

Lihat juga