Export (0) Print
Expand All

SqlCommand.Parameters Property

Gets the SqlParameterCollection.

[Visual Basic]
Public ReadOnly Property Parameters As SqlParameterCollection
[C#]
public SqlParameterCollection Parameters {get;}
[C++]
public: __property SqlParameterCollection* get_Parameters();
[JScript]
public function get Parameters() : SqlParameterCollection;

Property Value

The parameters of the Transact-SQL statement or stored procedure. The default is an empty collection.

Remarks

The .NET Framework Data Provider for SQL Server does not support the question mark (?) placeholder for passing parameters to a SQL Statement or a stored procedure called by a Command of CommandType.Text. In this case, named parameters must be used. For example:

SELECT * FROM Customers WHERE CustomerID = @CustomerID

Note   If the parameters in the collection do not match the requirements of the query to be executed, an error may result.

For more information see Using Stored Procedures with a Command.

Example

[Visual Basic, C#, C++] The following example creates a SqlCommand and displays its parameters. To accomplish this, the method is passed a SqlConnection, a query string that is a Transact-SQL SELECT statement, and an array of SqlParameter objects.

[Visual Basic] 
Public Sub CreateMySqlCommand(myConnection As SqlConnection, _
mySelectQuery As String, myParamArray() As SqlParameter)
    Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
    myCommand.CommandText = "SELECT CustomerID, CompanyName FROM Customers WHERE Country = @Country AND City = @City"
    myCommand.UpdatedRowSource = UpdateRowSource.Both
    myCommand.Parameters.Add(myParamArray)
    Dim j As Integer
    For j = 0 To myCommand.Parameters.Count - 1
       myCommand.Parameters.Add(myParamArray(j))
    Next j
    Dim myMessage As String = ""
    Dim i As Integer
    For i = 0 To myCommand.Parameters.Count - 1
        myMessage += myCommand.Parameters(i).ToString() & ControlChars.Cr
    Next i
    Console.WriteLine(myMessage)
End Sub

[C#] 
public void CreateMySqlCommand(SqlConnection myConnection,
string mySelectQuery, SqlParameter[] myParamArray) {
   SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
   myCommand.CommandText = "SELECT CustomerID, CompanyName FROM Customers WHERE Country = @Country AND City = @City";    
   myCommand.Parameters.Add(myParamArray);
   for (int j=0; j<myParamArray.Length; j++)
   {
      myCommand.Parameters.Add(myParamArray[j]) ;
   }
   string myMessage = "";
   for (int i = 0; i < myCommand.Parameters.Count; i++) 
   {
      myMessage += myCommand.Parameters[i].ToString() + "\n";
   }
   MessageBox.Show(myMessage);
}

[C++] 
public:
 void CreateMySqlCommand(SqlConnection* myConnection,
 String* mySelectQuery, SqlParameter* myParamArray[]) {
    SqlCommand* myCommand = new SqlCommand(mySelectQuery, myConnection);
    myCommand->CommandText = S"SELECT CustomerID, CompanyName FROM Customers WHERE Country = @Country AND City = @City";    
    myCommand->Parameters->Add(myParamArray);
    for (int j=0; j<myParamArray->Length; j++)
    {
       myCommand->Parameters->Add(myParamArray[j]) ;
    }
    String* myMessage = S"";
    for (int i = 0; i < myCommand->Parameters->Count; i++) 
    {
       myMessage = String::Concat( myMessage, myCommand->Parameters->Item[i], S"\n" );
    }
    MessageBox::Show(myMessage);
 }

[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.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

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

Show:
© 2014 Microsoft