Sdílet prostřednictvím


SqlCeParameterCollection Class

Collects all parameters relevant to a SqlCeCommand as well as their respective mappings to DataSet columns.

Inheritance Hierarchy

System.Object
  System.MarshalByRefObject
    System.Data.Common.DbParameterCollection
      System.Data.SqlServerCe.SqlCeParameterCollection

Namespace:  System.Data.SqlServerCe
Assembly:  System.Data.SqlServerCe (in System.Data.SqlServerCe.dll)

Syntax

'Declaration
Public NotInheritable Class SqlCeParameterCollection _
    Inherits DbParameterCollection
'Usage
Dim instance As SqlCeParameterCollection
public sealed class SqlCeParameterCollection : DbParameterCollection
public ref class SqlCeParameterCollection sealed : public DbParameterCollection
[<SealedAttribute>]
type SqlCeParameterCollection =  
    class
        inherit DbParameterCollection
    end
public final class SqlCeParameterCollection extends DbParameterCollection

The SqlCeParameterCollection type exposes the following members.

Properties

  Name Description
Public property Count Gets the number of SqlCeParameter objects in the collection. (Overrides DbParameterCollection.Count.)
Public property IsFixedSize Infrastructure. (Overrides DbParameterCollection.IsFixedSize.)
Public property IsReadOnly Infrastructure. (Overrides DbParameterCollection.IsReadOnly.)
Public property IsSynchronized Infrastructure. (Overrides DbParameterCollection.IsSynchronized.)
Public property Item[Int32] Gets or sets the SqlCeParameter at the specified index.
Public property Item[String] Gets or sets the SqlCeParameter with the specified name.
Public property SyncRoot Infrastructure. (Overrides DbParameterCollection.SyncRoot.)

Top

Methods

  Name Description
Public method Add(Object) Adds a SqlCeParameter object to the SqlCeCommand. (Overrides DbParameterCollection.Add(Object).)
Public method Add(SqlCeParameter) Adds the specified SqlCeParameter to the SqlCeParameterCollection.
Public method Add(String, SqlDbType) Adds a SqlCeParameter to the SqlCeParameterCollection given the parameter name and data type.
Public method Add(String, Object) Obsolete. Adds a SqlCeParameter to the SqlCeParameterCollection given the parameter name and value.
Public method Add(String, SqlDbType, Int32) Adds a SqlCeParameter to the SqlCeParameterCollection given the parameter name, data type, and column width.
Public method Add(String, SqlDbType, Int32, String) Adds a SqlCeParameter to the SqlCeCommand given the parameter name, data type, column width, and source column name.
Public method AddRange Adds an array of SqlCeParameter objects to SqlCeParameterCollection. (Overrides DbParameterCollection.AddRange(Array).)
Public method AddWithValue Adds a new SqlCeParameter to the SqlCeParameterCollection and sets its value.
Public method Clear Removes all items from the collection. (Overrides DbParameterCollection.Clear().)
Public method Contains(Object) Gets a value indicating whether or not a SqlCeParameter object exists in the collection. (Overrides DbParameterCollection.Contains(Object).)
Public method Contains(String) Gets a value indicating whether a SqlCeParameter with the specified parameter name exists in the collection. (Overrides DbParameterCollection.Contains(String).)
Public method CopyTo Copies SqlCeParameter objects from the SqlCeParameterCollection to the specified array. (Overrides DbParameterCollection.CopyTo(Array, Int32).)
Public method CreateObjRef (inherited from MarshalByRefObject)
Public method Equals (inherited from Object)
Protected method Finalize (inherited from Object)
Public method GetEnumerator Infrastructure. (Overrides DbParameterCollection.GetEnumerator().)
Public method GetHashCode (inherited from Object)
Public method GetLifetimeService (inherited from MarshalByRefObject)
Protected method GetParameter(Int32) (inherited from DbParameterCollection)
Protected method GetParameter(String) (inherited from DbParameterCollection)
Public method GetType (inherited from Object)
Public method IndexOf(Object) Gets the location of the SqlCeParameter object in the collection. (Overrides DbParameterCollection.IndexOf(Object).)
Public method IndexOf(String) Gets the location of the SqlCeParameter in the collection with the specified parameter name. (Overrides DbParameterCollection.IndexOf(String).)
Public method InitializeLifetimeService (inherited from MarshalByRefObject)
Public method Insert Inserts a SqlCeParameter in the collection at the specified index. (Overrides DbParameterCollection.Insert(Int32, Object).)
Protected method MemberwiseClone() (inherited from Object)
Protected method MemberwiseClone(Boolean) (inherited from MarshalByRefObject)
Public method Remove Removes the specified SqlCeParameter from the collection. (Overrides DbParameterCollection.Remove(Object).)
Public method RemoveAt(Int32) Removes the SqlCeParameter at the specified index from the collection. (Overrides DbParameterCollection.RemoveAt(Int32).)
Public method RemoveAt(String) Removes the SqlCeParameter with the specified name from the collection. (Overrides DbParameterCollection.RemoveAt(String).)
Protected method SetParameter(Int32, DbParameter) (inherited from DbParameterCollection)
Protected method SetParameter(String, DbParameter) (inherited from DbParameterCollection)
Public method ToString (inherited from Object)

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate property IList.Item (inherited from DbParameterCollection)
Explicit interface implemetationPrivate property IDataParameterCollection.Item (inherited from DbParameterCollection)

Top

Remarks

The number of parameters in the collection must be equal to the number of parameter placeholders within the command text. Otherwise, the .NET Compact Framework Data Provider for SQL Server Compact may raise an error.

Examples

The following example creates multiple instances of SqlCeParameter through the SqlCeParameterCollection collection within the SqlCeDataAdapter. These parameters are used to select data within the data source. Then they place the data in the DataSet. This example assumes that a DataSet and a SqlCeDataAdapter have already been created with the appropriate schema, commands, and connection.

Dim cmd As SqlCeCommand = Nothing
Dim adp As SqlCeDataAdapter = Nothing

Try
    adp = New SqlCeDataAdapter()
    Dim conn As New SqlCeConnection("Data Source = MyDatabase.sdf")

    ' Create the SelectCommand
    '
    cmd = conn.CreateCommand()
    cmd.CommandText = "SELECT * FROM Orders WHERE [Ship Country] = @country AND [Ship City] = @city"
    cmd.Parameters.Add("@country", SqlDbType.NVarChar, 15)
    cmd.Parameters.Add("@city", SqlDbType.NVarChar, 15)

    cmd.Parameters("@country").Value = "UK"
    cmd.Parameters("@city").Value = "London"

    adp.SelectCommand = cmd

    ' Create the DeleteCommand
    '
    cmd = conn.CreateCommand()
    cmd.CommandText = "DELETE FROM Orders WHERE [Order ID] = @orderID"

    Dim p As SqlCeParameter = cmd.Parameters.Add("@orderID", SqlDbType.NChar, 5, "Order ID")
    p.SourceVersion = DataRowVersion.Original

    adp.DeleteCommand = cmd

    ' Populate the dataset with the results from the SELECT statement
    '
    Dim ds As New DataSet()
    adp.Fill(ds)

    ' Modify the dataset
    '
    MessageBox.Show("Number of rows: " & ds.Tables(0).Rows.Count)

    ' Delete some rows
    '
    ds.Tables(0).Rows(3).Delete()
    ds.Tables(0).Rows(4).Delete()

    ' This will execute two DELETE statements 
    '
    adp.Update(ds.Tables(0))
Catch e As Exception
    MessageBox.Show(e.Message)
Finally
    If Not Nothing Is adp.SelectCommand Then
        adp.SelectCommand.Dispose()
    End If
    If Not Nothing Is adp.DeleteCommand Then
        adp.DeleteCommand.Dispose()
    End If
End Try
SqlCeCommand cmd = null;
SqlCeDataAdapter adp = null;

try
{
    adp = new SqlCeDataAdapter();
    SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf");

    // Create the SelectCommand
    //
    cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT * FROM Orders WHERE [Ship Country] = @country AND [Ship City] = @city";
    cmd.Parameters.Add("@country", SqlDbType.NVarChar, 15);
    cmd.Parameters.Add("@city", SqlDbType.NVarChar, 15);

    cmd.Parameters["@country"].Value = "UK";
    cmd.Parameters["@city"].Value = "London";

    adp.SelectCommand = cmd;

    // Create the DeleteCommand
    //
    cmd = conn.CreateCommand();
    cmd.CommandText = "DELETE FROM Orders WHERE [Order ID] = @orderID";

    SqlCeParameter p = cmd.Parameters.Add("@orderID", SqlDbType.NChar, 5, "Order ID");
    p.SourceVersion = DataRowVersion.Original;

    adp.DeleteCommand = cmd;

    // Populate the dataset with the results from the SELECT statement
    //
    DataSet ds = new DataSet();
    adp.Fill(ds);

    // Modify the dataset
    //
    MessageBox.Show("Number of rows: " + ds.Tables[0].Rows.Count);

    // Delete some rows
    //
    ds.Tables[0].Rows[3].Delete();
    ds.Tables[0].Rows[4].Delete();

    // This will execute two DELETE statements 
    //
    adp.Update(ds.Tables[0]);
}
catch (Exception e)
{
    MessageBox.Show(e.Message);
}
finally
{
    if (null != adp.SelectCommand) adp.SelectCommand.Dispose();
    if (null != adp.DeleteCommand) adp.DeleteCommand.Dispose();
}

Thread Safety

Any public static (Shared in Microsoft Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

System.Data.SqlServerCe Namespace