asp.net.ph

SqlCommand.Parameters Property

System.Data.SqlClient Namespace   SqlCommand Class


Returns the SqlParameterCollection.

Syntax


Script [ SqlParameterCollection variable = ] SqlCommand.Parameters

Property Value


variable The parameters of the Transact-SQL statement or stored procedure.

The property is read only with no default value.

Remarks

The SQL Server™ .NET Data Provider does not support the question mark ( ? ) placeholder for passing parameters to an 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

The following example initializes an SqlCommand and displays its parameters. To accomplish this, the method is passed an SqlConnection, a query string that is a Transact-SQL SELECT statement, and an array of SqlParameter objects.

void setSqlCommand ( SqlConnection myConn,
      string query, SqlParameter [ ] myParamArray ) {
   SqlCommand myCommand = new SqlCommand ( query, myConn );
   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 [ ] );
   }

   string myMessage = "";
   for ( int i = 0; i < myCommand.Parameters.Count; i++ ) {
      myMessage += myCommand.Parameters [ ].ToString ( ) + "<br>";
   }
   Response.Write ( myMessage );
}
  C# VB

See Also

SqlCommand Members   SqlParameter Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph