語言

SqlDataAdapter 建構函式

定義

多載

名稱 Description
SqlDataAdapter()

初始化 SqlDataAdapter 類別的新執行個體。

SqlDataAdapter(SqlCommand)

使用指定 SqlDataAdapter 做為 SqlCommand 屬性,初始化 SelectCommand 類別的新執行個體。

SqlDataAdapter(String, SqlConnection)

使用 SqlDataAdapter 和 SelectCommand 物件,初始化 SqlConnection 類別的新執行個體。

SqlDataAdapter(String, String)

使用 SqlDataAdapter 和連接字串,初始化 SelectCommand 類別的新執行個體。

SqlDataAdapter()

初始化 SqlDataAdapter 類別的新執行個體。

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

範例

下列範例會建立 並 SqlDataAdapter 設定其部分屬性。

public static SqlDataAdapter CreateSqlDataAdapter(SqlConnection connection)
{
    // Assumes that connection is a valid SqlConnection object
    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;
}

備註

建立 的 SqlDataAdapter 實例時,下列讀取/寫入屬性會設定為下列初始值。

屬性 初始值
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

您可以透過對 屬性的個別呼叫來變更這些屬性的值。

適用於

SqlDataAdapter(SqlCommand)

使用指定 SqlDataAdapter 做為 SqlCommand 屬性,初始化 SelectCommand 類別的新執行個體。

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

參數

selectCommand
SqlCommand

SqlCommand,為 Transact-SQL SELECT 陳述式或預存程序 (Stored Procedure),且設定成 SelectCommand 的 SqlDataAdapter 屬性。

範例

下列範例會建立 並 SqlDataAdapter 設定其部分屬性。

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
    }
    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;
    }
}

備註

這個建構函式的實作會將 SqlDataAdapterSelectCommand 屬性設定為 參數中指定的 selectCommand 值。

建立 的 SqlDataAdapter 實例時,下列讀取/寫入屬性會設定為下列初始值。

屬性 初始值
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

您可以透過對 屬性的個別呼叫來變更這些屬性的值。

當 SelectCommand (或任何其他命令屬性) 指派給先前建立 SqlCommand 的 時, SqlCommand 不會複製 。 會 SelectCommand 維護先前建立 SqlCommand 之 物件的參考。

適用於

SqlDataAdapter(String, SqlConnection)

使用 SqlDataAdapter 和 SelectCommand 物件,初始化 SqlConnection 類別的新執行個體。

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

參數

selectCommandText
String

String,即要由 SelectCommand 的 SqlDataAdapter 屬性使用的 Transact-SQL SELECT 陳述式或預存程序。

selectConnection
SqlConnection

表示連接的 SqlConnection。 如果您的連接字串不使用 Integrated Security = true,您便可以使用 SqlCredential 來傳遞使用者 ID 和密碼,這種方式比在連接字串中以文字指定使用者 ID 和密碼來得更加安全。

範例

下列範例會建立 並 SqlDataAdapter 設定其部分屬性。

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
    }
    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;
    }
}

備註

如果尚未開啟,則會開啟並關閉 的這個實 SqlDataAdapter 作 SqlConnection 。 這在必須針對兩個或多個 SqlDataAdapter 物件呼叫 Fill 方法的應用程式中很有用。 SqlConnection如果 已經開啟,您必須明確呼叫Close或Dispose以關閉它。

建立 的 SqlDataAdapter 實例時,下列讀取/寫入屬性會設定為下列初始值。

屬性 初始值
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

您可以透過對 屬性的個別呼叫來變更其中一個屬性的值。

適用於

SqlDataAdapter(String, String)

使用 SqlDataAdapter 和連接字串,初始化 SelectCommand 類別的新執行個體。

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

參數

selectCommandText
String

String,即要由 SelectCommand 的 SqlDataAdapter 屬性使用的 Transact-SQL SELECT 陳述式或預存程序。

selectConnectionString
String

連接字串。 如果您的連接字串不使用 Integrated Security = true,您可以使用 SqlDataAdapter(String, SqlConnection) 和 SqlCredential 傳遞使用者 ID 和密碼,比起在連接字串中將使用者 ID 和密碼指定為文字更安全。

範例

下列範例會建立 並 SqlDataAdapter 設定其部分屬性。

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
    }
    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;
    }
}

備註

建構函式的 SqlDataAdapter 這個多載會 selectCommandText 使用 參數來設定 SelectCommand 屬性。 SqlDataAdapter將會建立和維護使用 參數建立的連接 selectConnectionString 。

建立 的 SqlDataAdapter 實例時,下列讀取/寫入屬性會設定為下列初始值。

屬性 初始值
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

您可以透過對 屬性的個別呼叫來變更這些屬性的值。

適用於