Export (0) Print
Expand All

SqlCommand.Parameters Property

Gets the SqlParameterCollection.

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

public SqlParameterCollection Parameters { get; 
/** @property */
public SqlParameterCollection get_Parameters ()

public function get Parameters () : SqlParameterCollection

Property Value

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

The Microsoft .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

NoteNote

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.

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.

public void CreateSqlCommand(SqlConnection myConnection,
    string queryString, SqlParameter[] paramArray) 
{
    SqlCommand command = new SqlCommand(queryString, myConnection);
    command.CommandText = 
        "SELECT CustomerID, CompanyName FROM Customers " 
        + "WHERE Country = @Country AND City = @City";    
    command.Parameters.Add(paramArray);

    for (int j=0; j<paramArray.Length; j++)
    {
        command.Parameters.Add(paramArray[j]) ;
    

    string message = "";
    for (int i = 0; i < command.Parameters.Count; i++) 
    {
        message += command.Parameters[i].ToString() + "\n";
    
    Console.WriteLine(message);


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft