SqlCommand.Parameters 屬性

定義

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

屬性值

Transact-SQL 陳述式或預存程序的參數。 預設為空集合。

屬性

範例

下列範例示範如何建立 SqlCommand ,並將參數新增至 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

備註

Microsoft .NET Framework data Provider for SQL Server 不支援問號 (?) 占位符,將參數傳遞至 SQL 語句或 命令CommandType.Text所呼叫的預存程式。 在此情況下,必須使用具名參數。 例如:

SELECT * FROM Customers WHERE CustomerID = @CustomerID

注意

如果集合中的參數不符合要執行之查詢的需求,可能會產生錯誤。

如需詳細資訊,請參閱 設定參數和參數數據類型

適用於

另請參閱