SqlCeCommand.Parameters 属性

获取 SqlCeParameterCollection

命名空间:  System.Data.SqlServerCe
程序集:  System.Data.SqlServerCe(在 System.Data.SqlServerCe.dll 中)

语法

声明
Public ReadOnly Property Parameters As SqlCeParameterCollection
    Get
用法
Dim instance As SqlCeCommand
Dim value As SqlCeParameterCollection

value = instance.Parameters
public SqlCeParameterCollection Parameters { get; }
public:
property SqlCeParameterCollection^ Parameters {
    SqlCeParameterCollection^ get ();
}
member Parameters : SqlCeParameterCollection
function get Parameters () : SqlCeParameterCollection

属性值

类型:System.Data.SqlServerCe.SqlCeParameterCollection
SQL 语句的参数。默认为空集合。

注释

CommandType 设置为 Text 时,SQL Server Compact 的 .NET Compact Framework 数据提供程序支持向 SqlCeCommand 调用的 SQL 语句传递参数的命名参数。例如:

SELECT * FROM Customers WHERE CustomerID = @customerID 

备注

如果集合中的参数与要执行的查询的要求不匹配,则可能会导致错误。

示例

下面的示例将创建一个 SqlCeCommand 并设置一个 SqlCeParameter 对象数组。

Dim conn As New SqlCeConnection("Data Source = MyDatabase.sdf;")
conn.Open()

Dim command As SqlCeCommand = conn.CreateCommand()

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

Dim param As SqlCeParameter = Nothing

' 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)
command.Parameters.Add(param)

param = New SqlCeParameter("@desc", SqlDbType.NVarChar, 100)
command.Parameters.Add(param)

command.Parameters("@desc").Size = 100

' Calling Prepare after having set the CommandText and parameters
'
command.Prepare()

' Execute the SQL statement
'
command.ExecuteNonQuery()

' Change parameter values and call ExecuteNonQuery again
'
command.Parameters(0).Value = 21
command.Parameters(1).Value = "mySecondRegion"
command.ExecuteNonQuery()
SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf;");
conn.Open();

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);
command.Parameters.Add(param);

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

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

// Calling Prepare after having set the CommandText and parameters
//
command.Prepare();

// Execute the SQL statement
//
command.ExecuteNonQuery();

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

请参阅

参考

SqlCeCommand 类

System.Data.SqlServerCe 命名空间

SqlCeParameter