SqlCommand.Parameters Właściwość

Definicja

Pobiera klasę 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

Wartość właściwości

Parametry instrukcji Transact-SQL lub procedury składowanej. Wartością domyślną jest pusta kolekcja.

Atrybuty

Przykłady

W poniższym przykładzie pokazano, jak utworzyć i SqlCommand dodać parametry do elementu 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

Uwagi

Dostawca danych microsoft .NET Framework dla SQL Server nie obsługuje symbolu zastępczego znaku zapytania (?) do przekazywania parametrów do instrukcji SQL lub procedury składowanej wywoływanej przez polecenie CommandType.Text. W takim przypadku należy użyć nazwanych parametrów. Na przykład:

SELECT * FROM Customers WHERE CustomerID = @CustomerID

Uwaga

Jeśli parametry w kolekcji nie są zgodne z wymaganiami zapytania do wykonania, może to spowodować błąd.

Aby uzyskać więcej informacji, zobacz Konfigurowanie parametrów i typów danych parametrów.

Dotyczy

Zobacz też