SqlCommand コンストラクター

定義

オーバーロード

名前 説明
SqlCommand()

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

SqlCommand(String)

クエリのテキストを使用して、 SqlCommand クラスの新しいインスタンスを初期化します。

SqlCommand(String, SqlConnection)

クエリのテキストとSqlCommandを使用して、SqlConnection クラスの新しいインスタンスを初期化します。

SqlCommand(String, SqlConnection, SqlTransaction)

クエリ、SqlCommand、およびSqlConnectionのテキストを使用して、SqlTransaction クラスの新しいインスタンスを初期化します。

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

指定したコマンド テキスト、接続、トランザクション、および暗号化設定を使用して、 SqlCommand クラスの新しいインスタンスを初期化します。

SqlCommand()

ソース:
SqlCommand.cs
ソース:
SqlCommand.cs
ソース:
SqlCommand.cs
ソース:
SqlCommand.cs

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

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

次の例では、 SqlCommand を作成し、 CommandTimeout プロパティを設定します。

using System;
using System.Xml;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using Microsoft.Data.SqlClient;

public class Form1 : Form
{
    protected DataSet DataSet1;
    protected DataGrid dataGrid1;

    public void CreateSqlCommand()
    {
        SqlCommand command = new SqlCommand();
        command.CommandTimeout = 15;
        command.CommandType = CommandType.Text;
    }
}

注釈

基本コンストラクターは、すべてのフィールドを既定値に初期化します。 次の表に、 SqlCommandのインスタンスの初期プロパティ値を示します。

プロパティ初期値
CommandText 空の文字列 ("")
CommandTimeout 30
CommandType Text
Connection null

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

適用対象

SqlCommand(String)

ソース:
SqlCommand.cs
ソース:
SqlCommand.cs
ソース:
SqlCommand.cs
ソース:
SqlCommand.cs

クエリのテキストを使用して、 SqlCommand クラスの新しいインスタンスを初期化します。

public:
 SqlCommand(System::String ^ cmdText);
public SqlCommand(string cmdText);
new Microsoft.Data.SqlClient.SqlCommand : string -> Microsoft.Data.SqlClient.SqlCommand
Public Sub New (cmdText As String)

パラメーター

cmdText
String

クエリのテキスト。

次の例では、コマンド テキストを渡して SqlCommandを作成します。

using System;
using System.Xml;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using Microsoft.Data.SqlClient;

public class Form1 : Form
{
    protected DataSet DataSet1;
    protected DataGrid dataGrid1;

    public void CreateCommand()
    {
        string queryString = "SELECT * FROM Categories ORDER BY CategoryID";
        SqlCommand command = new SqlCommand(queryString);
        command.CommandTimeout = 15;
        command.CommandType = CommandType.Text;
    }
}

注釈

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

プロパティ初期値
CommandText cmdText
CommandTimeout 30
CommandType Text
Connection null

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

適用対象

SqlCommand(String, SqlConnection)

ソース:
SqlCommand.cs
ソース:
SqlCommand.cs
ソース:
SqlCommand.cs
ソース:
SqlCommand.cs

クエリのテキストとSqlCommandを使用して、SqlConnection クラスの新しいインスタンスを初期化します。

public:
 SqlCommand(System::String ^ cmdText, Microsoft::Data::SqlClient::SqlConnection ^ connection);
public SqlCommand(string cmdText, Microsoft.Data.SqlClient.SqlConnection connection);
new Microsoft.Data.SqlClient.SqlCommand : string * Microsoft.Data.SqlClient.SqlConnection -> Microsoft.Data.SqlClient.SqlCommand
Public Sub New (cmdText As String, connection As SqlConnection)

パラメーター

cmdText
String

クエリのテキスト。

connection
SqlConnection

SQL Serverのインスタンスへの接続を表す SqlConnection

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

using System;
using System.Data;
using Microsoft.Data.SqlClient;

namespace SqlCommandCS
{
    class Program
    {
        static void Main()
        {
            string str = "Data Source=(local);Initial Catalog=Northwind;"
                + "Integrated Security=SSPI";
            string qs = "SELECT OrderID, CustomerID FROM dbo.Orders;";
            CreateCommand(qs, str);

        }

        private static void CreateCommand(string queryString, string connectionString)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(queryString, connection);
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1]));
                }
            }
        }
    }
}

注釈

次の表に、 SqlCommandのインスタンスの初期プロパティ値を示します。

プロパティ初期値
CommandText cmdText
CommandTimeout 30
CommandType Text
Connection connection パラメーターの値である新しいSqlConnection

これらのパラメーターの値は、関連するプロパティを設定することで変更できます。

適用対象

SqlCommand(String, SqlConnection, SqlTransaction)

ソース:
SqlCommand.cs
ソース:
SqlCommand.cs
ソース:
SqlCommand.cs
ソース:
SqlCommand.cs

クエリ、SqlCommand、およびSqlConnectionのテキストを使用して、SqlTransaction クラスの新しいインスタンスを初期化します。

public:
 SqlCommand(System::String ^ cmdText, Microsoft::Data::SqlClient::SqlConnection ^ connection, Microsoft::Data::SqlClient::SqlTransaction ^ transaction);
public SqlCommand(string cmdText, Microsoft.Data.SqlClient.SqlConnection connection, Microsoft.Data.SqlClient.SqlTransaction transaction);
new Microsoft.Data.SqlClient.SqlCommand : string * Microsoft.Data.SqlClient.SqlConnection * Microsoft.Data.SqlClient.SqlTransaction -> Microsoft.Data.SqlClient.SqlCommand
Public Sub New (cmdText As String, connection As SqlConnection, transaction As SqlTransaction)

パラメーター

cmdText
String

クエリのテキスト。

connection
SqlConnection

SQL Serverのインスタンスへの接続を表す SqlConnection

transaction
SqlTransaction

SqlCommandが実行されるSqlTransaction

注釈

次の表に、 SqlCommandのインスタンスの初期プロパティ値を示します。

プロパティ初期値
CommandText cmdText
CommandTimeout 30
CommandType Text
Connection connection パラメーターの値である新しいSqlConnection

これらのパラメーターの値は、関連するプロパティを設定することで変更できます。

適用対象

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

ソース:
SqlCommand.cs
ソース:
SqlCommand.cs
ソース:
SqlCommand.cs
ソース:
SqlCommand.cs

指定したコマンド テキスト、接続、トランザクション、および暗号化設定を使用して、 SqlCommand クラスの新しいインスタンスを初期化します。

public:
 SqlCommand(System::String ^ cmdText, Microsoft::Data::SqlClient::SqlConnection ^ connection, Microsoft::Data::SqlClient::SqlTransaction ^ transaction, Microsoft::Data::SqlClient::SqlCommandColumnEncryptionSetting columnEncryptionSetting);
public SqlCommand(string cmdText, Microsoft.Data.SqlClient.SqlConnection connection, Microsoft.Data.SqlClient.SqlTransaction transaction, Microsoft.Data.SqlClient.SqlCommandColumnEncryptionSetting columnEncryptionSetting);
new Microsoft.Data.SqlClient.SqlCommand : string * Microsoft.Data.SqlClient.SqlConnection * Microsoft.Data.SqlClient.SqlTransaction * Microsoft.Data.SqlClient.SqlCommandColumnEncryptionSetting -> Microsoft.Data.SqlClient.SqlCommand
Public Sub New (cmdText As String, connection As SqlConnection, transaction As SqlTransaction, columnEncryptionSetting As SqlCommandColumnEncryptionSetting)

パラメーター

cmdText
String

クエリのテキスト。

connection
SqlConnection

SQL Serverのインスタンスへの接続を表す SqlConnection

transaction
SqlTransaction

SqlCommandが実行されるSqlTransaction

columnEncryptionSetting
SqlCommandColumnEncryptionSetting

暗号化設定。 詳細については、「 Always Encrypted」を参照してください。

適用対象