SqlCeDataAdapter 类

表示一组数据命令和一个数据库连接,它们用于填充 DataSet 和更新数据源。

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

语法

声明
Public NotInheritable Class SqlCeDataAdapter _
    Inherits DbDataAdapter _
    Implements ICloneable
用法
Dim instance As SqlCeDataAdapter
public sealed class SqlCeDataAdapter : DbDataAdapter, 
    ICloneable
public ref class SqlCeDataAdapter sealed : public DbDataAdapter, 
    ICloneable
[<SealedAttribute>]
type SqlCeDataAdapter =  
    class
        inherit DbDataAdapter
        interface ICloneable
    end
public final class SqlCeDataAdapter extends DbDataAdapter implements ICloneable

注释

SqlCeDataAdapter 可作为 DataSet 和数据源之间的桥接器。 它用于从数据源检索数据,也用于将数据保存到数据源。 SqlCeDataAdapter 通过以下方法提供这个桥接器:使用 Fill 将数据从数据源加载到 DataSet 中,并使用 UpdateDataSet 中所做的更改发回数据源。

当 SqlCeDataAdapter 填充 DataSet 时,它为返回的数据创建必要的表和列(如果这些表和列尚不存在)。 但是,除非 MissingSchemaAction 属性设置为 AddWithKey,否则这个隐式创建的架构中将不包括主键信息。 您也可以让 SqlCeDataAdapter 创建 DataSet 的架构,并在使用 FillSchema 向架构填充数据之前将主键信息包括进去。

SqlCeDataAdapter 包括 SelectCommandInsertCommandDeleteCommandUpdateCommandTableMappings 属性,为数据的加载和更新提供便利。

当创建 SqlCeDataAdapter 的实例时,属性都设置为其初始值。 有关这些值的列表,请参阅 SqlCeDataAdapter 构造函数。

示例

下面的示例使用了 SqlCeCommand、SqlCeDataAdapter 和 SqlCeConnection 从数据源选择记录并用选定的行填充 DataSet。 然后,将返回已填充的 DataSet。 为完成此任务,向该方法传递一个已初始化的 DataSet、一个连接字符串和一个查询字符串,后者是一个 SQL SELECT 语句。

Try
    Dim strDataSource As String
    strDataSource = "" & _
        "Data Source = C:\Program Files\" & _
        "Microsoft SQL Server Compact Edition\v3.5\Samples\" & _
        "Northwind.sdf"
    Dim conn As New SqlCeConnection
    conn.ConnectionString = strDataSource & ";Password='<password>'"

    Dim selectCmd As SqlCeCommand = conn.CreateCommand
    selectCmd.CommandText = "SELECT * FROM Employees"

    Dim adp As New SqlCeDataAdapter(selectCmd)

    Dim ds As New DataSet

    ' Note: Fill will leave the connection in its original state;
    ' In this case, the connection was closed so it will be left closed
    adp.Fill(ds)

    Console.WriteLine(("The SqlCeDataAdapter succesfully filled " & _
                       ds.Tables.Item(0).Rows.Count & " rows in the DataSet!"))
Catch ds As Exception
    Console.WriteLine(ds.Message)
Finally
    Console.WriteLine(vbNewLine & vbNewLine & vbNewLine & _
                      "Press any key to continue...")
    Console.ReadKey()
End Try
try
{
    string strDataSource =
        @"Data Source = C:\Program Files\" +
        @"Microsoft SQL Server Compact Edition\v3.5\Samples\" +
        @"Northwind.sdf";
    SqlCeConnection conn = new SqlCeConnection();
    conn.ConnectionString = strDataSource + ";Password='<password>'";

    SqlCeCommand selectCmd = conn.CreateCommand();
    selectCmd.CommandText = "SELECT * FROM Employees";

    SqlCeDataAdapter adp = new SqlCeDataAdapter(selectCmd);

    DataSet ds = new DataSet();

    // Note: Fill will leave the connection in its original state;
    // In this case, the connection was closed so it will be left closed
    //
    adp.Fill(ds);

    Console.WriteLine("The SqlCeDataAdapter succesfully filled " +
        ds.Tables[0].Rows.Count + " rows in the DataSet!");
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
finally
{
    Console.WriteLine("\n\n\nPress any key to continue...");
    Console.ReadKey();
}

继承层次结构

System. . :: . .Object
  System. . :: . .MarshalByRefObject
    System.ComponentModel. . :: . .Component
      System.Data.Common. . :: . .DataAdapter
        System.Data.Common. . :: . .DbDataAdapter
          System.Data.SqlServerCe..::..SqlCeDataAdapter

线程安全

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.

请参阅

参考

SqlCeDataAdapter 成员

System.Data.SqlServerCe 命名空间

SqlCeConnection

SqlCeCommand

DataSet

DataTable