SqlDataAdapter Konstruktory

Definice

Inicializuje novou instanci SqlDataAdapter třídy.

Přetížení

SqlDataAdapter()

Inicializuje novou instanci SqlDataAdapter třídy.

SqlDataAdapter(SqlCommand)

Inicializuje novou instanci SqlDataAdapter třídy se zadaným SqlCommand jako SelectCommand vlastnost.

SqlDataAdapter(String, SqlConnection)

Inicializuje novou instanci SqlDataAdapter třídy s objektem SelectCommandSqlConnection a.

SqlDataAdapter(String, String)

Inicializuje novou instanci SqlDataAdapter třídy pomocí SelectCommand a připojovací řetězec.

SqlDataAdapter()

Inicializuje novou instanci SqlDataAdapter třídy.

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

Příklady

Následující příklad vytvoří SqlDataAdapter a nastaví některé jeho vlastnosti.

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

Poznámky

Při vytvoření instance objektu SqlDataAdapter jsou následující vlastnosti pro čtení a zápis nastaveny na následující počáteční hodnoty.

Vlastnosti Počáteční hodnota
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

Hodnotu kterékoli z těchto vlastností můžete změnit samostatným voláním vlastnosti.

Viz také

Platí pro

SqlDataAdapter(SqlCommand)

Inicializuje novou instanci SqlDataAdapter třídy se zadaným SqlCommand jako SelectCommand vlastnost.

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)

Parametry

selectCommand
SqlCommand

A SqlCommand to je příkaz Transact-SQL SELECT nebo uložená procedura a je nastaven jako SelectCommand vlastnost SqlDataAdapter.

Příklady

Následující příklad vytvoří SqlDataAdapter a nastaví některé jeho vlastnosti.

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

Poznámky

Tato implementace konstruktoru SqlDataAdapter nastaví SelectCommand vlastnost na hodnotu zadanou v parametru selectCommand .

Při vytvoření instance objektu SqlDataAdapter jsou následující vlastnosti pro čtení a zápis nastaveny na následující počáteční hodnoty.

Vlastnosti Počáteční hodnota
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

Hodnotu kterékoli z těchto vlastností můžete změnit samostatným voláním vlastnosti.

Pokud SelectCommand je k dříve vytvořenému SqlCommandobjektu přiřazena (nebo jakákoli jiná vlastnost příkazu), SqlCommand není klonován. Objekt SelectCommand udržuje odkaz na dříve vytvořený SqlCommand objekt.

Viz také

Platí pro

SqlDataAdapter(String, SqlConnection)

Inicializuje novou instanci SqlDataAdapter třídy s objektem SelectCommandSqlConnection a.

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)

Parametry

selectCommandText
String

A String to je příkaz Transact-SQL SELECT nebo uložená procedura, která SelectCommand má být použita vlastností SqlDataAdapter.

selectConnection
SqlConnection

A SqlConnection , který představuje připojení. Pokud váš připojovací řetězec nepoužívá Integrated Security = true, můžete id SqlCredential uživatele a heslo předat bezpečněji než zadáním ID uživatele a hesla jako textu v připojovací řetězec.

Příklady

Následující příklad vytvoří SqlDataAdapter a nastaví některé jeho vlastnosti.

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

Poznámky

Tato implementace otevře SqlDataAdapter a zavře, SqlConnection pokud ještě není otevřená. To může být užitečné v aplikaci, která musí volat metodu Fill pro dva nebo více SqlDataAdapter objektů. SqlConnection Pokud je již otevřen, je nutné explicitně volat Zavřít nebo Dispose, aby se zavřel.

Při vytvoření instance objektu SqlDataAdapter jsou následující vlastnosti pro čtení a zápis nastaveny na následující počáteční hodnoty.

Vlastnosti Počáteční hodnota
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

Hodnotu některé z těchto vlastností můžete změnit samostatným voláním vlastnosti.

Viz také

Platí pro

SqlDataAdapter(String, String)

Inicializuje novou instanci SqlDataAdapter třídy pomocí SelectCommand a připojovací řetězec.

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)

Parametry

selectCommandText
String

A String to je příkaz Transact-SQL SELECT nebo uložená procedura, která SelectCommand má být použita vlastností SqlDataAdapter.

selectConnectionString
String

Připojovací řetězec Pokud vaše připojovací řetězec nepoužívá Integrated Security = true, můžete použít SqlDataAdapter(String, SqlConnection) a SqlCredential k předání ID a hesla uživatele bezpečněji než zadáním ID uživatele a hesla jako textu v připojovací řetězec.

Příklady

Následující příklad vytvoří SqlDataAdapter a nastaví některé jeho vlastnosti.

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

Poznámky

Toto přetížení konstruktoru SqlDataAdapterselectCommandText používá parametr k nastavení SelectCommand vlastnosti. Vytvoří SqlDataAdapter a udržuje připojení vytvořené pomocí parametru selectConnectionString .

Při vytvoření instance objektu SqlDataAdapter jsou následující vlastnosti pro čtení a zápis nastaveny na následující počáteční hodnoty.

Vlastnosti Počáteční hodnota
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

Hodnotu kterékoli z těchto vlastností můžete změnit samostatným voláním vlastnosti.

Viz také

Platí pro