SqlCommand.Parameters Propiedad

Definición

Obtiene la estructura SqlParameterCollection.Gets the SqlParameterCollection.

public:
 property System::Data::SqlClient::SqlParameterCollection ^ Parameters { System::Data::SqlClient::SqlParameterCollection ^ get(); };
public System.Data.SqlClient.SqlParameterCollection Parameters { get; }
[System.Data.DataSysDescription("DbCommand_Parameters")]
public System.Data.SqlClient.SqlParameterCollection Parameters { get; }
member this.Parameters : System.Data.SqlClient.SqlParameterCollection
[<System.Data.DataSysDescription("DbCommand_Parameters")>]
member this.Parameters : System.Data.SqlClient.SqlParameterCollection
Public ReadOnly Property Parameters As SqlParameterCollection

Valor de propiedad

SqlParameterCollection

Parámetros del procedimiento almacenado o instrucción de Transact-SQL.The parameters of the Transact-SQL statement or stored procedure. El valor predeterminado es una colección vacía.The default is an empty collection.

Atributos

Ejemplos

En el ejemplo siguiente se muestra cómo crear un SqlCommand y agregar parámetros a SqlParameterCollection .The following example demonstrates how to create a SqlCommand and add parameters to the SqlParameterCollection.

private static void UpdateDemographics(Int32 customerID,
    string demoXml, string connectionString)
{
    // Update the demographics for a store, which is stored
    // in an xml column.
    string commandText = "UPDATE Sales.Store SET Demographics = @demographics "
        + "WHERE CustomerID = @ID;";

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand(commandText, connection);
        command.Parameters.Add("@ID", SqlDbType.Int);
        command.Parameters["@ID"].Value = customerID;

        // Use AddWithValue to assign Demographics.
        // SQL Server will implicitly convert strings into XML.
        command.Parameters.AddWithValue("@demographics", demoXml);

        try
        {
            connection.Open();
            Int32 rowsAffected = command.ExecuteNonQuery();
            Console.WriteLine("RowsAffected: {0}", rowsAffected);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}
Private Sub UpdateDemographics(ByVal customerID As Integer, _
    ByVal demoXml As String, _
    ByVal connectionString As String)

    ' Update the demographics for a store, which is stored 
    ' in an xml column.
    Dim commandText As String = _
     "UPDATE Sales.Store SET Demographics = @demographics " _
     & "WHERE CustomerID = @ID;"

    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(commandText, connection)

        ' Add CustomerID parameter for WHERE clause.
        command.Parameters.Add("@ID", SqlDbType.Int)
        command.Parameters("@ID").Value = customerID

        ' Use AddWithValue to assign Demographics.
        ' SQL Server will implicitly convert strings into XML.
        command.Parameters.AddWithValue("@demographics", demoXml)

        Try
            connection.Open()
            Dim rowsAffected As Integer = command.ExecuteNonQuery()
            Console.WriteLine("RowsAffected: {0}", rowsAffected)

        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Using
End Sub

Comentarios

El proveedor de datos de Microsoft .NET Framework para SQL Server no admite el marcador de posición del signo de interrogación (?) para pasar parámetros a una instrucción SQL o a un procedimiento almacenado al que llama un comando de CommandType.Text .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. En este caso, se deben usar parámetros con nombre.In this case, named parameters must be used. Por ejemplo:For example:

SELECT * FROM Customers WHERE CustomerID = @CustomerIDSELECT * FROM Customers WHERE CustomerID = @CustomerID

Nota

Si los parámetros de la colección no coinciden con los requisitos de la consulta que se va a ejecutar, puede producirse un error.If the parameters in the collection do not match the requirements of the query to be executed, an error may result.

Para obtener más información, vea Configurar parámetros y tipos de datos de parámetros.For more information, see Configuring Parameters and Parameter Data Types.

Se aplica a

Consulte también