SqlCeParameterCollection 类
收集与 SqlCeCommand 有关的所有参数以及它们各自到 DataSet 列的映射。
继承层次结构
System.Object
System.MarshalByRefObject
System.Data.Common.DbParameterCollection
System.Data.SqlServerCe.SqlCeParameterCollection
命名空间: System.Data.SqlServerCe
程序集: System.Data.SqlServerCe(在 System.Data.SqlServerCe.dll 中)
语法
声明
Public NotInheritable Class SqlCeParameterCollection _
Inherits DbParameterCollection
用法
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
SqlCeParameterCollection 类型公开以下成员。
属性
名称 | 说明 | |
---|---|---|
Count | 获取集合中的 SqlCeParameter 对象数。 (覆盖 DbParameterCollection.Count。) | |
IsFixedSize | 基础结构。 (覆盖 DbParameterCollection.IsFixedSize。) | |
IsReadOnly | 基础结构。 (覆盖 DbParameterCollection.IsReadOnly。) | |
IsSynchronized | 基础结构。 (覆盖 DbParameterCollection.IsSynchronized。) | |
Item[Int32] | 获取或设置指定索引处的 SqlCeParameter。 | |
Item[String] | 获取或设置具有指定名称的 SqlCeParameter。 | |
SyncRoot | 基础结构。 (覆盖 DbParameterCollection.SyncRoot。) |
页首
方法
页首
显式接口实现
名称 | 说明 | |
---|---|---|
IList.Item | (继承自 DbParameterCollection) | |
IDataParameterCollection.Item | (继承自 DbParameterCollection) |
页首
注释
集合中参数的数量必须与命令文本中参数占位符的数量相等。否则,SQL Server Compact 的 .NET Compact Framework 数据访问接口可能会引发错误。
示例
下面的示例通过 SqlCeDataAdapter 中的 SqlCeParameterCollection 集合创建 SqlCeParameter 的多个实例。这些参数用于选择数据源中的数据。然后它们将数据放置在 DataSet 中。此示例假定已经用适当的架构、命令和连接创建了 DataSet 和 SqlCeDataAdapter。
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();
}
线程安全
此类型的任何公共静态(在 Microsoft Visual Basic 中为共享)成员是线程安全的。不保证所有实例成员都对于线程安全的。