SqlConnection.CreateCommand Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Creates and returns a SqlCommand object associated with the SqlConnection.

Namespace:   System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)

public SqlCommand CreateCommand()

Return Value

Type: System.Data.SqlClient.SqlCommand

A SqlCommand object.

using System.Data;
using System.Data.SqlClient;
public class A {
   public static void Main() {
      using (SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI;")) {
         SqlCommand command= connection.CreateCommand();
         command.CommandText = "SELECT * FROM Categories ORDER BY CategoryID";
         command.CommandTimeout = 15;
         command.CommandType = CommandType.Text;

.NET Framework
Available since 1.1
Return to top