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,並使用 Update 將在 DataSet 中所做的變更傳回到資料來源,來提供這座橋樑。
當 SqlCeDataAdapter 填入 DataSet 時,如果傳回資料所需的資料表和資料行不存在,則會加以建立。不過,除非 MissingSchemaAction 屬性是設定為 AddWithKey,否則在隱含建立的結構描述中將不包含主索引鍵資訊。您也可以使用 SqlCeDataAdapter 建立 DataSet 的結構描述 (包括主索引鍵資訊),然後再使用 FillSchema 在其中填入資料。
SqlCeDataAdapter 包含 SelectCommand、InsertCommand、DeleteCommand、UpdateCommand 和 TableMappings 屬性,可協助載入及更新資料。
當您建立 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.