SqlCeCommand.Parameters Property

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

public SqlCeParameterCollection Parameters { get; }

Property Value

Type: System.Data.SqlServerCe.SqlCeParameterCollection
The parameters of the SQL statement. The default is an empty collection.

The .NET Compact Framework Data Provider for SQL Server Compact supports named parameters for passing parameters to an SQL statement called by a SqlCeCommand when CommandType is set to Text. For example:

SELECT * FROM Customers WHERE CustomerID = @customerID 

If the parameters in the collection do not match the requirements of the query to be executed, an error may result.

The following example creates a SqlCeCommand and sets an array of SqlCeParameter objects.

SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf;");

SqlCeCommand command = conn.CreateCommand();

// Create and prepare a SQL statement
command.CommandText = "INSERT INTO Region (RegionID, RegionDescription) VALUES (@id, @desc)";

SqlCeParameter param = null;

// NOTE:
// For optimal performance, make sure you always set the parameter
// type and the maximum size - this is especially important for non-fixed
// types such as NVARCHAR or NTEXT; In case of named parameters, 
// SqlCeParameter instances do not need to be added to the collection
// in the order specified in the query; If however you use ? as parameter
// specifiers, then you do need to add the parameters in the correct order
param = new SqlCeParameter("@id", SqlDbType.Int);

param = new SqlCeParameter("@desc", SqlDbType.NVarChar, 100);

command.Parameters["@desc"].Size = 100;

// Calling Prepare after having set the CommandText and parameters

// Execute the SQL statement

// Change parameter values and call ExecuteNonQuery again
command.Parameters[0].Value = 21;
command.Parameters[1].Value = "mySecondRegion";