This documentation is archived and is not being maintained.

SqlCommand Constructor

.NET Framework 1.1

Initializes a new instance of the SqlCommand class.

Overload List

Initializes a new instance of the SqlCommand class.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New()
[C#] public SqlCommand();
[C++] public: SqlCommand();
[JScript] public function SqlCommand();

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String)
[C#] public SqlCommand(string);
[C++] public: SqlCommand(String*);
[JScript] public function SqlCommand(String);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, SqlConnection)
[C#] public SqlCommand(string, SqlConnection);
[C++] public: SqlCommand(String*, SqlConnection*);
[JScript] public function SqlCommand(String, SqlConnection);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, SqlConnection, SqlTransaction)
[C#] public SqlCommand(string, SqlConnection, SqlTransaction);
[C++] public: SqlCommand(String*, SqlConnection*, SqlTransaction*);
[JScript] public function SqlCommand(String, SqlConnection, SqlTransaction);

Example

[Visual Basic, C#, C++] The following example creates a SqlCommand and sets some of its properties.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the SqlCommand constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Sub CreateMySqlCommand()
    Dim myConnection As New SqlConnection _
       ("Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer")
    myConnection.Open()
    Dim myTrans As SqlTransaction = myConnection.BeginTransaction()
    Dim mySelectQuery As String = _
       "SELECT * FROM Categories ORDER BY CategoryID"
    Dim myCommand As New SqlCommand(mySelectQuery, myConnection, myTrans)
    myCommand.CommandTimeout = 20
End Sub


[C#] 
public void CreateMySqlCommand() 
{
   SqlConnection myConnection = new SqlConnection("Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer");
   myConnection.Open();
   SqlTransaction myTrans = myConnection.BeginTransaction();
   string mySelectQuery = "SELECT * FROM Categories ORDER BY CategoryID";
   SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection,myTrans);
   myCommand.CommandTimeout = 20;
}


[C++] 
public:
    void CreateMySqlCommand()
    {
        SqlConnection* myConnection = new SqlConnection(S"Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer");
        myConnection->Open();
        SqlTransaction* myTrans = myConnection->BeginTransaction();
        String* mySelectQuery = S"SELECT * FROM Categories ORDER BY CategoryID";
        SqlCommand* myCommand = new SqlCommand(mySelectQuery, myConnection, myTrans);
        myCommand->CommandTimeout = 20;
    };

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

SqlCommand Class | SqlCommand Members | System.Data.SqlClient Namespace

Show: