次の方法で共有


SqlCommand コンストラクタ (String, SqlConnection)

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

名前空間: System.Data.SqlClient
アセンブリ: System.Data (system.data.dll 内)

構文

'宣言
Public Sub New ( _
    cmdText As String, _
    connection As SqlConnection _
)
'使用
Dim cmdText As String
Dim connection As SqlConnection

Dim instance As New SqlCommand(cmdText, connection)
public SqlCommand (
    string cmdText,
    SqlConnection connection
)
public:
SqlCommand (
    String^ cmdText, 
    SqlConnection^ connection
)
public SqlCommand (
    String cmdText, 
    SqlConnection connection
)
public function SqlCommand (
    cmdText : String, 
    connection : SqlConnection
)

パラメータ

  • cmdText
    クエリ テキスト。
  • connection
    SQL Server のインスタンスへの接続を表す SqlConnection

解説

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

プロパティ

初期値

CommandText

cmdText

CommandTimeout

30

CommandType

CommandType.Text

Connection

connection パラメータの値である新しい SqlConnection

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

使用例

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

Public Sub CreateCommand(ByVal queryString As String, _
  ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)

        connection.Open()
        Dim reader As SqlDataReader = command.ExecuteReader()
            While reader.Read()
            Console.WriteLine(String.Format("{0}, {1}", _
                reader(0), reader(1)))
        End While
    End Using
End Sub
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]));
        }
    }
}

プラットフォーム

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

.NET Compact Framework

サポート対象 : 2.0、1.0

参照

関連項目

SqlCommand クラス
SqlCommand メンバ
System.Data.SqlClient 名前空間

その他の技術情報

ADO.NET でのデータへの接続およびデータの取得
.NET Framework Data Provider for SQL Server の使用