共用方式為


DataAdapter 參數

適用於:.NET Framework .NET .NET Standard

下載 ADO.NET

DbDataAdapter 具有四個屬性,可用來擷取資料來源的資料,以及將資料更新至資料來源:SelectCommand 屬性可傳回資料來源的資料,而 InsertCommandUpdateCommandDeleteCommand 屬性可用來管理在資料來源的變更。

注意

在您呼叫 SelectCommandFill 方法前,必須先設定 DataAdapter 屬性。 您必須先設定 InsertCommandUpdateCommandDeleteCommand 屬性,然後再呼叫 UpdateDataAdapter 方法,端視針對 DataTable 中的資料進行哪些變更而定。 例如,如果已經加入資料列,則必須先設定 InsertCommand,才能呼叫 UpdateUpdate 正在處理已插入、已更新或已刪除的資料列時,DataAdapter 會使用個別的 Command 屬性來處理這項動作。 已修改資料列的目前資訊會透過 Command 集合傳遞給 Parameters 物件。

當您更新資料來源的資料列時,您會呼叫 UPDATE 陳述式,此陳述式會使用唯一識別碼,來識別待更新資料表中的資料列。 唯一的識別項一般是主索引鍵欄位的值。 UPDATE 陳述式所使用的參數包含唯一的識別項,以及要更新的資料行和值,如下列 Transact-SQL 陳述式所示。

UPDATE Customers SET CompanyName = @CompanyName
  WHERE CustomerID = @CustomerID  

注意

參數預留位置的語法會隨資料來源而有所不同。 此範例將說明 SQL Server 資料來源的保留字元。

在此範例中,會針對 CustomerID 等於 @CustomerID 參數值的資料列,以 @CompanyName 參數的值來更新 CompanyName 欄位。 這些參數會使用 SqlParameter 物件的 SourceColumn 屬性,從已修改的資料列擷取資訊。 下列是前述範例 UPDATE 陳述式的參數。 程式碼會假設變數 adapter 表示有效的 SqlDataAdapter 物件。

// Assumes that connection is a valid SqlAdapter object
adapter.UpdateCommand.Parameters.Add("@CompanyName", 
            SqlDbType.VarChar, 15, "CompanyName");
SqlParameter parameter = adapter.UpdateCommand.Parameters.Add("@CustomerID",
            SqlDbType.Char, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;

Add 集合的 Parameters 方法會擷取參數的名稱、資料型別、大小 (如果此型別有大小),以及來自 SourceColumnDataTable 名稱。 請注意,SourceVersion 參數的 @CustomerID 會設定為 Original。 如此一來,如果已修改的 DataRow 內識別欄位的值有所變更,便可確保資料來源內的現有資料列也已經更新。 在這種情況下,Original 資料列值會與資料來源中的目前值相符,而 Current 資料列值會包含已更新的值。 SourceVersion 參數的 @CompanyName 並未設定,因此會使用預設的 Current 資料列值。

注意

針對 DataAdapterFill 作業與 DataReaderGet 方法,會以 Microsoft SqlClient Data Provider for SQL Server 所傳回的類型推斷 .NET 類型。 由 Microsoft SQL Server 資料類型所推斷的 .NET 類型與存取子方法,會在 ADO.NET 中的資料類型對應一文中詳述。

Parameter.SourceColumn、Parameter.SourceVersion

SourceColumnSourceVersion 可當做引數傳遞給 Parameter 建構函式 (Constructor),或設定為現有 Parameter 的屬性。 SourceColumn 是將在其中擷取 DataColumn 值之 DataRowParameter 名稱。 SourceVersion 會指定 DataRow 用來擷取值的 DataAdapter 版本。

下表顯示可與 DataRowVersion 搭配使用的 SourceVersion 列舉值。

DataRowVersion 列舉型別 描述
Current 這個參數會使用資料行目前的值。 此為預設值。
Default 此參數會使用資料行的 DefaultValue
Original 這個參數會使用資料行的原始值。
Proposed 這個會參數使用建議值。

下一區段中的 SqlClient 程式碼範例會定義 UpdateCommand 的參數,其中 CustomerID 資料行將做為兩個參數的 SourceColumn 使用:@CustomerID (SET CustomerID = @CustomerID) 和 @OldCustomerID (WHERE CustomerID = @OldCustomerID)。 @CustomerID 參數是用來將 CustomerID 資料行更新為 DataRow 中目前的值。 因此會使用 SourceVersionCurrentCustomerIDSourceColumn@OldCustomerID 參數是用來識別資料來源中的目前資料列。 因為在資料列的 Original 版本中找到相符的資料行值,所以會使用 SourceColumnCustomerID 的同一個 SourceVersion (Original)。

使用 SqlClient 參數

下列範例將示範如何建立 SqlDataAdapter 並將 MissingSchemaAction 設定為 AddWithKey,以便從資料庫中擷取其他結構描述資訊。 SelectCommandInsertCommandUpdateCommandDeleteCommand 屬性已設定而且其對應的 SqlParameter 物件已加入至 Parameters 集合。 此方法會傳回 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;
}

請參閱