表示对数据源执行的 SQL 语句。
命名空间: System.Data.SqlServerCe
程序集: System.Data.SqlServerCe(在 System.Data.SqlServerCe.dll 中)
语法
声明
Public NotInheritable Class SqlCeCommand _
Inherits DbCommand _
Implements ICloneable
用法
Dim instance As SqlCeCommand
public sealed class SqlCeCommand : DbCommand,
ICloneable
public ref class SqlCeCommand sealed : public DbCommand,
ICloneable
[<SealedAttribute>]
type SqlCeCommand =
class
inherit DbCommand
interface ICloneable
end
public final class SqlCeCommand extends DbCommand implements ICloneable
注释
当创建 SqlCeCommand 的实例时,读/写属性将被设置为它们的初始值。有关这些值的列表,请参阅 SqlCeCommand 构造函数。
SqlCeCommand 具有以下方法(这些方法在数据源执行命令):
项 |
说明 |
---|---|
执行返回行的命令。 |
|
执行诸如 INSERT、DELETE 和 UPDATE 语句之类的 SQL 命令。 |
|
从数据库中检索单个值(例如一个聚合值)。 |
|
执行命令并返回结果集。 |
SQL Server Compact 3.5 的数据提供程序不支持批查询。命令的格式必须是:
Select * from Customers 而不能是Select * from Customers; Select * from Orders;
如果使用为 System.Data.SqlClient 生成的代码,则必须更改查询以使它们符合此限制。
SQL Server Compact 3.5 不仅支持共享同一连接的多个命令,还支持多个并发连接。这意味着可能在同一个连接上存在多个 SqlCeDataReader 实例。此行为不同于 System.Data.SqlClient 的行为。
如果执行 SqlCeCommand 的方法产生了致命的 SqlCeException,SqlCeConnection 可能会被关闭。您可以重新打开连接,然后继续。
示例
下面的示例使用 SqlCeCommand 以及 SqlCeConnection 从数据库选择行。
Dim query As String = "SELECT [Order ID], [Customer] FROM Orders"
Dim conn As New SqlCeConnection(connString)
Dim cmd As New SqlCeCommand(query, conn)
conn.Open()
Dim rdr As SqlCeDataReader = cmd.ExecuteReader()
Try
' Iterate through the results
'
While rdr.Read()
Dim val1 As Integer = rdr.GetInt32(0)
Dim val2 As String = rdr.GetString(1)
End While
Finally
' Always call Close when done reading
'
rdr.Close()
' Always call Close when done reading
'
conn.Close()
End Try
string query = "SELECT [Order ID], [Customer] FROM Orders";
SqlCeConnection conn = new SqlCeConnection(connString);
SqlCeCommand cmd = new SqlCeCommand(query, conn);
conn.Open();
SqlCeDataReader rdr = cmd.ExecuteReader();
try
{
// Iterate through the results
//
while (rdr.Read())
{
int val1 = rdr.GetInt32(0);
string val2 = rdr.GetString(1);
}
}
finally
{
// Always call Close when done reading
//
rdr.Close();
// Always call Close when done reading
//
conn.Close();
}
继承层次结构
System. . :: . .Object
System. . :: . .MarshalByRefObject
System.ComponentModel. . :: . .Component
System.Data.Common. . :: . .DbCommand
System.Data.SqlServerCe..::..SqlCeCommand
线程安全
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.