次の方法で共有


SqlDataAdapter コンストラクター

定義

SqlDataAdapter クラスの新しいインスタンスを初期化します。

オーバーロード

名前 説明
SqlDataAdapter()

SqlDataAdapter クラスの新しいインスタンスを初期化します。

SqlDataAdapter(SqlCommand)

指定したSqlCommandSelectCommand プロパティとして使用して、SqlDataAdapter クラスの新しいインスタンスを初期化します。

SqlDataAdapter(String, SqlConnection)

SelectCommandSqlConnection オブジェクトを使用して、SqlDataAdapter クラスの新しいインスタンスを初期化します。

SqlDataAdapter(String, String)

SelectCommandと接続文字列を使用して、SqlDataAdapter クラスの新しいインスタンスを初期化します。

SqlDataAdapter()

ソース:
System.Data.SqlClient.notsupported.cs

SqlDataAdapter クラスの新しいインスタンスを初期化します。

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

次の例では、 SqlDataAdapter を作成し、そのプロパティの一部を設定します。

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

注釈

SqlDataAdapterのインスタンスが作成されると、次の読み取り/書き込みプロパティが次の初期値に設定されます。

プロパティ 初期値
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

これらのプロパティの値は、プロパティを個別に呼び出して変更できます。

こちらもご覧ください

適用対象

SqlDataAdapter(SqlCommand)

ソース:
System.Data.SqlClient.notsupported.cs

指定したSqlCommandSelectCommand プロパティとして使用して、SqlDataAdapter クラスの新しいインスタンスを初期化します。

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)

パラメーター

selectCommand
SqlCommand

Transact-SQL SELECT ステートメントまたはストアド プロシージャであり、SqlDataAdapterSelectCommand プロパティとして設定されるSqlCommand

次の例では、 SqlDataAdapter を作成し、そのプロパティの一部を設定します。

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

注釈

SqlDataAdapter コンストラクターのこの実装では、SelectCommand プロパティを selectCommand パラメーターで指定された値に設定します。

SqlDataAdapterのインスタンスが作成されると、次の読み取り/書き込みプロパティが次の初期値に設定されます。

プロパティ 初期値
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

これらのプロパティの値は、プロパティを個別に呼び出して変更できます。

以前に作成したSqlCommandSelectCommand (またはその他のコマンド プロパティ) が割り当てられている場合、SqlCommandは複製されません。 SelectCommandは、以前に作成したSqlCommand オブジェクトへの参照を保持します。

こちらもご覧ください

適用対象

SqlDataAdapter(String, SqlConnection)

ソース:
System.Data.SqlClient.notsupported.cs

SelectCommandSqlConnection オブジェクトを使用して、SqlDataAdapter クラスの新しいインスタンスを初期化します。

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)

パラメーター

selectCommandText
String

SqlDataAdapterSelectCommand プロパティによって使用される Transact-SQL SELECT ステートメントまたはストアド プロシージャであるString

selectConnection
SqlConnection

接続を表す SqlConnection 。 接続文字列で Integrated Security = trueを使用しない場合は、 SqlCredential を使用して、ユーザー ID とパスワードを接続文字列のテキストとして指定するよりも安全にユーザー ID とパスワードを渡すことができます。

次の例では、 SqlDataAdapter を作成し、そのプロパティの一部を設定します。

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

注釈

この SqlDataAdapter の実装では、 SqlConnection がまだ開いていない場合は、開いて閉じます。 これは、2 つ以上のSqlDataAdapter オブジェクトに対して Fill メソッドを呼び出す必要があるアプリケーションで役立ちます。 SqlConnectionが既に開いている場合は、明示的に Close または Dispose を呼び出して閉じる必要があります。

SqlDataAdapterのインスタンスが作成されると、次の読み取り/書き込みプロパティが次の初期値に設定されます。

プロパティ 初期値
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

これらのプロパティの値は、プロパティを個別に呼び出して変更できます。

こちらもご覧ください

適用対象

SqlDataAdapter(String, String)

ソース:
System.Data.SqlClient.notsupported.cs

SelectCommandと接続文字列を使用して、SqlDataAdapter クラスの新しいインスタンスを初期化します。

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)

パラメーター

selectCommandText
String

SqlDataAdapterSelectCommand プロパティによって使用される Transact-SQL SELECT ステートメントまたはストアド プロシージャであるString

selectConnectionString
String

接続文字列。 接続文字列で Integrated Security = trueを使用しない場合は、 SqlDataAdapter(String, SqlConnection)SqlCredential を使用して、ユーザー ID とパスワードを接続文字列のテキストとして指定するよりも安全にユーザー ID とパスワードを渡すことができます。

次の例では、 SqlDataAdapter を作成し、そのプロパティの一部を設定します。

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

注釈

SqlDataAdapter コンストラクターのこのオーバーロードでは、selectCommandText パラメーターを使用してSelectCommand プロパティを設定します。 SqlDataAdapterは、selectConnectionString パラメーターを使用して作成された接続を作成して維持します。

SqlDataAdapterのインスタンスが作成されると、次の読み取り/書き込みプロパティが次の初期値に設定されます。

プロパティ 初期値
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

これらのプロパティの値は、プロパティを個別に呼び出して変更できます。

こちらもご覧ください

適用対象