SqlCeConnection.CreateCommand 方法

创建并返回一个与 SqlCeConnection 关联的 SqlCeCommand 对象。

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

语法

声明
Public Function CreateCommand As SqlCeCommand
用法
Dim instance As SqlCeConnection
Dim returnValue As SqlCeCommand

returnValue = instance.CreateCommand
public SqlCeCommand CreateCommand ()
public:
SqlCeCommand^ CreateCommand ()
public SqlCeCommand CreateCommand ()
public function CreateCommand () : SqlCeCommand
不适用。

返回值

一个 SqlCeCommand 对象。

示例

下面的示例说明如何使用 CreateCommand 方法创建 SqlCeCommand 对象。

Dim conn As New SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'")
conn.Open()

' Start a local transaction
'
Dim tx As SqlCeTransaction = conn.BeginTransaction()

' By default, commands run in auto-commit mode; 
'
Dim cmd1 As SqlCeCommand = conn.CreateCommand()

' You may create multiple commands on the same connection
'
Dim cmd2 As SqlCeCommand = conn.CreateCommand()

' To enlist a command in a transaction, set the Transaction property
'
cmd1.Transaction = tx

Try
    cmd1.CommandText = "INSERT INTO Shippers ([Company Name]) VALUES ('Northwind Traders')"
    cmd1.ExecuteNonQuery()

    ' Auto-commited because cmd2 is not enlisted in a transaction
    '
    cmd2.CommandText = "INSERT INTO Employees ([Last Name], [First Name]) VALUES ('Nancy', 'Smith')"
    cmd2.ExecuteNonQuery()

    ' This will cause referential constraint violation
    '
    cmd1.CommandText = "DELETE FROM Products WHERE [Product ID] = 1"
    cmd1.ExecuteNonQuery()

    ' Commit the changes to disk if everything above succeeded
    '
    tx.Commit()
Catch
    tx.Rollback()
Finally
    conn.Close()
End Try
SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'");
conn.Open();

// Start a local transaction
//
SqlCeTransaction tx = conn.BeginTransaction();

// By default, commands run in auto-commit mode; 
//
SqlCeCommand cmd1 = conn.CreateCommand();

// You may create multiple commands on the same connection
//
SqlCeCommand cmd2 = conn.CreateCommand();

// To enlist a command in a transaction, set the Transaction property
//
cmd1.Transaction = tx;

try
{
    cmd1.CommandText = "INSERT INTO Shippers ([Company Name]) VALUES ('Northwind Traders')";
    cmd1.ExecuteNonQuery();

    // Auto-commited because cmd2 is not enlisted in a transaction
    //
    cmd2.CommandText = "INSERT INTO Employees ([Last Name], [First Name]) VALUES ('Nancy', 'Smith')";
    cmd2.ExecuteNonQuery();

    // This will cause referential constraint violation
    //
    cmd1.CommandText = "DELETE FROM Products WHERE [Product ID] = 1";
    cmd1.ExecuteNonQuery();

    // Commit the changes to disk if everything above succeeded
    //
    tx.Commit();
}
catch (Exception)
{
    tx.Rollback();
}
finally
{
    conn.Close();
}

平台

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

Windows Vista、Microsoft Windows XP SP2 和 Windows Server 2003 SP1 支持 Microsoft .NET Framework 3.0。

版本信息

.NET Framework

受以下版本支持:3.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

SqlCeConnection 类
SqlCeConnection 成员
System.Data.SqlServerCe 命名空间