Dela via


SqlCommand Constructors

Definition

Overloads

SqlCommand()

Initializes a new instance of the SqlCommand class.

SqlCommand(String)

Initializes a new instance of the SqlCommand class with the text of the query.

SqlCommand(String, SqlConnection)

Initializes a new instance of the SqlCommand class with the text of the query and a SqlConnection .

SqlCommand(String, SqlConnection, SqlTransaction)

Initializes a new instance of the SqlCommand class with the text of the query, a SqlConnection , and the SqlTransaction .

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

Initializes a new instance of the SqlCommand class with specified command text, connection, transaction, and encryption setting.

SqlCommand()

Initializes a new instance of the SqlCommand class.

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

Examples

The following example creates a SqlCommand and sets the CommandTimeout property.

// <Snippet1>
using System;
using System.Xml;
using System.Data;
using Microsoft.Data.SqlClient;
using System.Data.Common;
using System.Windows.Forms;

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


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

Remarks

The base constructor initializes all fields to their default values. The following table shows initial property values for an instance of SqlCommand.

Properties Initial value
CommandText empty string ("")
CommandTimeout 30
CommandType CommandType.Text
Connection Null

You can change the value for any of these properties through a separate call to the property.

Applies to

SqlCommand(String)

Initializes a new instance of the SqlCommand class with the text of the query.

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)

Parameters

cmdText
String

The text of the query.

Examples

The following example creates a SqlCommand, passing in the connection string and command text.

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

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;
    }

Remarks

When an instance of SqlCommand is created, the following read/write properties are set to initial values.

Properties Initial value
CommandText cmdText
CommandTimeout 30
CommandType CommandType.Text
Connection null

You can change the value for any of these properties through a separate call to the property.

Applies to

SqlCommand(String, SqlConnection)

Initializes a new instance of the SqlCommand class with the text of the query and a 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)

Parameters

cmdText
String

The text of the query.

connection
SqlConnection

A SqlConnection that represents the connection to an instance of SQL Server.

Examples

The following example creates a SqlCommand and sets some of its properties.

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]));
                }
            }
        }

Remarks

The following table shows initial property values for an instance of SqlCommand.

Properties Initial value
CommandText cmdText
CommandTimeout 30
CommandType CommandType.Text
Connection A new SqlConnection that is the value for the connection parameter.

You can change the value for any of these parameters by setting the related property.

Applies to

SqlCommand(String, SqlConnection, SqlTransaction)

Initializes a new instance of the SqlCommand class with the text of the query, a SqlConnection , and the 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)

Parameters

cmdText
String

The text of the query.

connection
SqlConnection

A SqlConnection that represents the connection to an instance of SQL Server.

transaction
SqlTransaction

The SqlTransaction in which the SqlCommand executes.

Remarks

The following table shows initial property values for an instance of SqlCommand.

Properties Initial value
CommandText cmdText
CommandTimeout 30
CommandType CommandType.Text
Connection A new SqlConnection that is the value for the connection parameter.

You can change the value for any of these parameters by setting the related property.

Applies to

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

Initializes a new instance of the SqlCommand class with specified command text, connection, transaction, and encryption setting.

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)

Parameters

cmdText
String

The text of the query.

connection
SqlConnection

A SqlConnection that represents the connection to an instance of SQL Server.

transaction
SqlTransaction

The SqlTransaction in which the SqlCommand executes.

columnEncryptionSetting
SqlCommandColumnEncryptionSetting

The encryption setting. For more information, see Always Encrypted.

Applies to