Bagikan melalui


SqlDataAdapter Konstruktor

Definisi

Menginisialisasi instans baru kelas SqlDataAdapter.

Overload

SqlDataAdapter()

Menginisialisasi instans baru kelas SqlDataAdapter.

SqlDataAdapter(SqlCommand)

Menginisialisasi instans SqlDataAdapter baru kelas dengan yang ditentukan SqlCommand sebagai SelectCommand properti .

SqlDataAdapter(String, SqlConnection)

Menginisialisasi instans SqlDataAdapter baru kelas dengan SelectCommand objek dan SqlConnection .

SqlDataAdapter(String, String)

Menginisialisasi instans SqlDataAdapter baru kelas dengan SelectCommand dan string koneksi.

SqlDataAdapter()

Menginisialisasi instans baru kelas SqlDataAdapter.

public:
 SqlDataAdapter();
public SqlDataAdapter ();
Public Sub New ()

Contoh

Contoh berikut membuat SqlDataAdapter dan mengatur beberapa propertinya.

public static SqlDataAdapter CreateSqlDataAdapter(SqlConnection connection)
{
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

    // Create the commands.
    adapter.SelectCommand = new SqlCommand(
        "SELECT CustomerID, CompanyName FROM CUSTOMERS", connection);
    adapter.InsertCommand = new SqlCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (@CustomerID, @CompanyName)", connection);
    adapter.UpdateCommand = new SqlCommand(
        "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
        "WHERE CustomerID = @oldCustomerID", connection);
    adapter.DeleteCommand = new SqlCommand(
        "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

    // Create the parameters.
    adapter.InsertCommand.Parameters.Add("@CustomerID",
        SqlDbType.Char, 5, "CustomerID");
    adapter.InsertCommand.Parameters.Add("@CompanyName",
        SqlDbType.VarChar, 40, "CompanyName");

    adapter.UpdateCommand.Parameters.Add("@CustomerID",
        SqlDbType.Char, 5, "CustomerID");
    adapter.UpdateCommand.Parameters.Add("@CompanyName",
        SqlDbType.VarChar, 40, "CompanyName");
    adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
        SqlDbType.Char, 5, "CustomerID").SourceVersion =
        DataRowVersion.Original;

    adapter.DeleteCommand.Parameters.Add("@CustomerID",
        SqlDbType.Char, 5, "CustomerID").SourceVersion =
        DataRowVersion.Original;

    return adapter;
}
Public Function CreateSqlDataAdapter( _
    ByVal connection As SqlConnection) As SqlDataAdapter

    Dim adapter As New SqlDataAdapter()
    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey

    ' Create the commands.
    adapter.SelectCommand = New SqlCommand( _
        "SELECT CustomerID, CompanyName FROM CUSTOMERS", connection)
    adapter.InsertCommand = New SqlCommand( _
        "INSERT INTO Customers (CustomerID, CompanyName) " & _
         "VALUES (@CustomerID, @CompanyName)", connection)
    adapter.UpdateCommand = New SqlCommand( _
        "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = " & _
        "@CompanyName WHERE CustomerID = @oldCustomerID", connection)
    adapter.DeleteCommand = New SqlCommand( _
        "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection)

    ' Create the parameters.
    adapter.InsertCommand.Parameters.Add("@CustomerID", _
        SqlDbType.Char, 5, "CustomerID")
    adapter.InsertCommand.Parameters.Add("@CompanyName", _
        SqlDbType.VarChar, 40, "CompanyName")

    adapter.UpdateCommand.Parameters.Add("@CustomerID", _
        SqlDbType.Char, 5, "CustomerID")
    adapter.UpdateCommand.Parameters.Add("@CompanyName", _
        SqlDbType.VarChar, 40, "CompanyName")
    adapter.UpdateCommand.Parameters.Add("@oldCustomerID", _
        SqlDbType.Char, 5, "CustomerID").SourceVersion = _
        DataRowVersion.Original

    adapter.DeleteCommand.Parameters.Add("@CustomerID", _
        SqlDbType.Char, 5, "CustomerID").SourceVersion = _
        DataRowVersion.Original

    Return adapter
End Function

Keterangan

Saat instans SqlDataAdapter dibuat, properti baca/tulis berikut diatur ke nilai awal berikut.

Properti Nilai awal
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

Anda dapat mengubah nilai salah satu properti ini melalui panggilan terpisah ke properti .

Lihat juga

Berlaku untuk

SqlDataAdapter(SqlCommand)

Menginisialisasi instans SqlDataAdapter baru kelas dengan yang ditentukan SqlCommand sebagai SelectCommand properti .

public:
 SqlDataAdapter(System::Data::SqlClient::SqlCommand ^ selectCommand);
public SqlDataAdapter (System.Data.SqlClient.SqlCommand selectCommand);
new System.Data.SqlClient.SqlDataAdapter : System.Data.SqlClient.SqlCommand -> System.Data.SqlClient.SqlDataAdapter
Public Sub New (selectCommand As SqlCommand)

Parameter

selectCommand
SqlCommand

SqlCommand yang merupakan pernyataan T-SQL SELECT atau prosedur tersimpan dan ditetapkan sebagai SelectCommand properti dari SqlDataAdapter.

Contoh

Contoh berikut membuat SqlDataAdapter dan mengatur beberapa propertinya.

public static SqlDataAdapter CreateSqlDataAdapter(SqlCommand selectCommand,
    SqlConnection connection)
{
    SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

    // Create the other commands.
    adapter.InsertCommand = new SqlCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (@CustomerID, @CompanyName)", connection);

    adapter.UpdateCommand = new SqlCommand(
        "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
        "WHERE CustomerID = @oldCustomerID", connection);

    adapter.DeleteCommand = new SqlCommand(
        "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

    // Create the parameters.
    adapter.InsertCommand.Parameters.Add("@CustomerID",
        SqlDbType.Char, 5, "CustomerID");
    adapter.InsertCommand.Parameters.Add("@CompanyName",
        SqlDbType.VarChar, 40, "CompanyName");

    adapter.UpdateCommand.Parameters.Add("@CustomerID",
        SqlDbType.Char, 5, "CustomerID");
    adapter.UpdateCommand.Parameters.Add("@CompanyName",
        SqlDbType.VarChar, 40, "CompanyName");
    adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
        SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;

    adapter.DeleteCommand.Parameters.Add("@CustomerID",
        SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;

    return adapter;
}
Public Function CreateSqlDataAdapter(ByVal selectCommand As SqlCommand, _
    ByVal connection As SqlConnection) As SqlDataAdapter

    Dim adapter As New SqlDataAdapter(selectCommand)
    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey

    ' Create the commands.
    adapter.InsertCommand = New SqlCommand( _
        "INSERT INTO Customers (CustomerID, CompanyName) " & _
         "VALUES (@CustomerID, @CompanyName)", connection)

    adapter.UpdateCommand = New SqlCommand( _
        "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " & _
        "WHERE CustomerID = @oldCustomerID", connection)

    adapter.DeleteCommand = New SqlCommand( _
        "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection)

    ' Create the parameters.
    adapter.InsertCommand.Parameters.Add("@CustomerID", _
        SqlDbType.Char, 5, "CustomerID")
    adapter.InsertCommand.Parameters.Add("@CompanyName", _
        SqlDbType.VarChar, 40, "CompanyName")

    adapter.UpdateCommand.Parameters.Add("@CustomerID", _
        SqlDbType.Char, 5, "CustomerID")
    adapter.UpdateCommand.Parameters.Add("@CompanyName", _
        SqlDbType.VarChar, 40, "CompanyName")
    adapter.UpdateCommand.Parameters.Add("@oldCustomerID", _
        SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original

    adapter.DeleteCommand.Parameters.Add("@CustomerID", _
        SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original

    Return adapter
End Function

Keterangan

Implementasi SqlDataAdapter konstruktor ini mengatur SelectCommand properti ke nilai yang ditentukan dalam selectCommand parameter .

Saat instans SqlDataAdapter dibuat, properti baca/tulis berikut diatur ke nilai awal berikut.

Properti Nilai awal
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

Anda dapat mengubah nilai salah satu properti ini melalui panggilan terpisah ke properti .

Ketika SelectCommand (atau properti perintah lainnya) ditetapkan ke yang dibuat SqlCommandsebelumnya , SqlCommand tidak dikloning. SelectCommand mempertahankan referensi ke objek yang dibuat SqlCommand sebelumnya.

Lihat juga

Berlaku untuk

SqlDataAdapter(String, SqlConnection)

Menginisialisasi instans SqlDataAdapter baru kelas dengan SelectCommand objek dan SqlConnection .

public:
 SqlDataAdapter(System::String ^ selectCommandText, System::Data::SqlClient::SqlConnection ^ selectConnection);
public SqlDataAdapter (string selectCommandText, System.Data.SqlClient.SqlConnection selectConnection);
new System.Data.SqlClient.SqlDataAdapter : string * System.Data.SqlClient.SqlConnection -> System.Data.SqlClient.SqlDataAdapter
Public Sub New (selectCommandText As String, selectConnection As SqlConnection)

Parameter

selectCommandText
String

String yang merupakan pernyataan T-SQL SELECT atau prosedur tersimpan yang akan digunakan oleh SelectCommand properti dari SqlDataAdapter.

selectConnection
SqlConnection

SqlConnection yang mewakili koneksi. Jika string koneksi Anda tidak menggunakan Integrated Security = true, Anda dapat menggunakan SqlCredential untuk meneruskan ID pengguna dan kata sandi dengan lebih aman daripada dengan menentukan ID pengguna dan kata sandi sebagai teks dalam string koneksi.

Contoh

Contoh berikut membuat SqlDataAdapter dan mengatur beberapa propertinya.

public static SqlDataAdapter CreateSqlDataAdapter(string commandText,
    SqlConnection connection)
{
    SqlDataAdapter adapter = new SqlDataAdapter(commandText, connection);

    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

    // Create the other commands.
    adapter.InsertCommand = new SqlCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (@CustomerID, @CompanyName)");

    adapter.UpdateCommand = new SqlCommand(
        "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
        "WHERE CustomerID = @oldCustomerID");

    adapter.DeleteCommand = new SqlCommand(
        "DELETE FROM Customers WHERE CustomerID = @CustomerID");

    // Create the parameters.
    adapter.InsertCommand.Parameters.Add("@CustomerID",
        SqlDbType.Char, 5, "CustomerID");
    adapter.InsertCommand.Parameters.Add("@CompanyName",
        SqlDbType.VarChar, 40, "CompanyName");

    adapter.UpdateCommand.Parameters.Add("@CustomerID",
        SqlDbType.Char, 5, "CustomerID");
    adapter.UpdateCommand.Parameters.Add("@CompanyName",
        SqlDbType.VarChar, 40, "CompanyName");
    adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
        SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;

    adapter.DeleteCommand.Parameters.Add("@CustomerID",
        SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;

    return adapter;
}
Public Function CreateSqlDataAdapter(ByVal commandText As String, _
    ByVal connection As SqlConnection) As SqlDataAdapter

    Dim adapter As New SqlDataAdapter(commandText, connection)

    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey

    ' Create the commands.
    adapter.InsertCommand = New SqlCommand( _
        "INSERT INTO Customers (CustomerID, CompanyName) " & _
         "VALUES (@CustomerID, @CompanyName)")

    adapter.UpdateCommand = New SqlCommand( _
        "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " & _
        "WHERE CustomerID = @oldCustomerID")

    adapter.DeleteCommand = New SqlCommand( _
        "DELETE FROM Customers WHERE CustomerID = @CustomerID")

    ' Create the parameters.
    adapter.InsertCommand.Parameters.Add("@CustomerID", _
        SqlDbType.Char, 5, "CustomerID")
    adapter.InsertCommand.Parameters.Add("@CompanyName", _
        SqlDbType.VarChar, 40, "CompanyName")

    adapter.UpdateCommand.Parameters.Add("@CustomerID", _
        SqlDbType.Char, 5, "CustomerID")
    adapter.UpdateCommand.Parameters.Add("@CompanyName", _
        SqlDbType.VarChar, 40, "CompanyName")
    adapter.UpdateCommand.Parameters.Add("@oldCustomerID", _
        SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original

    adapter.DeleteCommand.Parameters.Add("@CustomerID", _
        SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original

    Return adapter
End Function

Keterangan

Implementasi pembukaan SqlDataAdapter ini dan menutup SqlConnection jika belum terbuka. Ini dapat berguna dalam aplikasi yang harus memanggil Fill metode untuk dua objek atau lebih SqlDataAdapter . SqlConnection Jika sudah terbuka, Anda harus secara eksplisit memanggil Tutup atau Buang untuk menutupnya.

Saat instans SqlDataAdapter dibuat, properti baca/tulis berikut diatur ke nilai awal berikut.

Properti Nilai awal
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

Anda dapat mengubah nilai salah satu properti ini melalui panggilan terpisah ke properti .

Lihat juga

Berlaku untuk

SqlDataAdapter(String, String)

Menginisialisasi instans SqlDataAdapter baru kelas dengan SelectCommand dan string koneksi.

public:
 SqlDataAdapter(System::String ^ selectCommandText, System::String ^ selectConnectionString);
public SqlDataAdapter (string selectCommandText, string selectConnectionString);
new System.Data.SqlClient.SqlDataAdapter : string * string -> System.Data.SqlClient.SqlDataAdapter
Public Sub New (selectCommandText As String, selectConnectionString As String)

Parameter

selectCommandText
String

String yang merupakan pernyataan T-SQL SELECT atau prosedur tersimpan yang akan digunakan oleh SelectCommand properti dari SqlDataAdapter.

selectConnectionString
String

String koneksi. Jika string koneksi Anda tidak menggunakan Integrated Security = true, Anda dapat menggunakan SqlDataAdapter(String, SqlConnection) dan SqlCredential untuk meneruskan ID pengguna dan kata sandi dengan lebih aman daripada dengan menentukan ID pengguna dan kata sandi sebagai teks dalam string koneksi.

Contoh

Contoh berikut membuat SqlDataAdapter dan mengatur beberapa propertinya.

public static SqlDataAdapter CreateSqlDataAdapter(string commandText,
    string connectionString)
{
    SqlDataAdapter adapter = new SqlDataAdapter(commandText, connectionString);
    SqlConnection connection = adapter.SelectCommand.Connection;

    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

    // Create the commands.
    adapter.InsertCommand = new SqlCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (@CustomerID, @CompanyName)", connection);

    adapter.UpdateCommand = new SqlCommand(
        "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
        "WHERE CustomerID = @oldCustomerID", connection);

    adapter.DeleteCommand = new SqlCommand(
        "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

    // Create the parameters.
    adapter.InsertCommand.Parameters.Add("@CustomerID",
        SqlDbType.Char, 5, "CustomerID");
    adapter.InsertCommand.Parameters.Add("@CompanyName",
        SqlDbType.VarChar, 40, "CompanyName");

    adapter.UpdateCommand.Parameters.Add("@CustomerID",
        SqlDbType.Char, 5, "CustomerID");
    adapter.UpdateCommand.Parameters.Add("@CompanyName",
        SqlDbType.VarChar, 40, "CompanyName");
    adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
        SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;

    adapter.DeleteCommand.Parameters.Add("@CustomerID",
        SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;

    return adapter;
}
Public Function CreateSqlDataAdapter(ByVal commandText As String, _
    ByVal connectionString As String) As SqlDataAdapter

    Dim adapter As New SqlDataAdapter(commandText, connectionString)
    Dim connection As SqlConnection = adapter.SelectCommand.Connection

    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey

    ' Create the commands.
    adapter.InsertCommand = New SqlCommand( _
        "INSERT INTO Customers (CustomerID, CompanyName) " & _
         "VALUES (@CustomerID, @CompanyName)", connection)

    adapter.UpdateCommand = New SqlCommand( _
        "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " & _
        "WHERE CustomerID = @oldCustomerID", connection)

    adapter.DeleteCommand = New SqlCommand( _
        "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection)

    ' Create the parameters.
    adapter.InsertCommand.Parameters.Add("@CustomerID", _
        SqlDbType.Char, 5, "CustomerID")
    adapter.InsertCommand.Parameters.Add("@CompanyName", _
        SqlDbType.VarChar, 40, "CompanyName")

    adapter.UpdateCommand.Parameters.Add("@CustomerID", _
        SqlDbType.Char, 5, "CustomerID")
    adapter.UpdateCommand.Parameters.Add("@CompanyName", _
        SqlDbType.VarChar, 40, "CompanyName")
    adapter.UpdateCommand.Parameters.Add("@oldCustomerID", _
        SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original

    adapter.DeleteCommand.Parameters.Add("@CustomerID", _
        SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original

    Return adapter
End Function

Keterangan

Kelebihan beban SqlDataAdapter konstruktor ini menggunakan selectCommandText parameter untuk mengatur SelectCommand properti . SqlDataAdapter akan membuat dan memelihara koneksi yang dibuat dengan selectConnectionString parameter .

Saat instans SqlDataAdapter dibuat, properti baca/tulis berikut diatur ke nilai awal berikut.

Properti Nilai awal
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

Anda dapat mengubah nilai salah satu properti ini melalui panggilan terpisah ke properti .

Lihat juga

Berlaku untuk