Connection Property (SqlCommand)
Gets or sets the
SqlConnection used by this instance of the
SqlCommand.
Property Value
The connection to a data source. The default value is a null reference.
The following example creates a
SqlCommand and sets some of its properties.
public void CreateCommand()
{
string mySelectQuery = "SELECT * FROM Test.Dept ORDER BY DeptNo";
SqlCommand myCommand = new SqlCommand(mySelectQuery);
myCommand.Connection = new SqlConnection(
"User Id=sa;Server=localhost;Initial Catalog=Test;");
myCommand.CommandType = System.Data.CommandType.Text;
}
Public Sub CreateCommand()
Dim mySelectQuery As String = _
"SELECT * FROM Test.Dept ORDER BY DeptNo"
Dim myCommand As New SqlCommand(mySelectQuery)
myCommand.Connection = New SqlConnection( _
"User Id=sa;Server=localhost;Initial Catalog=Test;")
myCommand.CommandType = System.Data.CommandType.Text
End Sub