共用方式為


SqlCeParameterCollection Class

收集與 SqlCeCommand 相關的所有參數,以及這些參數的 DataSet 資料行個別對應。

命名空間: System.Data.SqlServerCe
組件: System.Data.SqlServerCe (在 system.data.sqlserverce.dll)

語法

'宣告
<DefaultMemberAttribute("Item")> _
Public NotInheritable Class SqlCeParameterCollection
    Inherits DbParameterCollection
[DefaultMemberAttribute("Item")] 
public sealed class SqlCeParameterCollection : DbParameterCollection
[DefaultMemberAttribute(L"Item")] 
public ref class SqlCeParameterCollection sealed : public DbParameterCollection
/** @attribute DefaultMemberAttribute("Item") */ 
public final class SqlCeParameterCollection extends DbParameterCollection
DefaultMemberAttribute("Item") 
public final class SqlCeParameterCollection extends DbParameterCollection

備註

集合中的參數數目必須等於命令文字中參數替代符號的數目。否則,.NET Compact Framework Data Provider for SQL Server Compact 3.5 可能會引發錯誤。

繼承階層

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

範例

下列範例會透過 SqlCeDataAdapter 內的 SqlCeParameterCollection 集合,建立 SqlCeParameter 的多個執行個體。這些參數用於選取資料來源內的資料。接著會將資料放入 DataSet。這個範例假設已使用適當的結構描述、命令和連接建立了 DataSetSqlCeDataAdapter

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) 此型別的成員具備執行緒安全。不保證任何執行個體成員安全執行緒。

平台

開發平台

Windows Vista, Windows Mobile 5.0, Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Mobile 2003 for Pocket PC, Windows CE 5.0
版本資訊
.NET Framework 及 NET Compact Framework
支援於 3.5
.NET Framework
支援於 3.0
.NET Compact Framework 及 .Net Framework
支援於 2.0

另請參閱

參考

SqlCeParameterCollection Members
System.Data.SqlServerCe Namespace