SqlCeConnection.CreateCommand Method
Создает и возвращает объект SqlCeCommand, связанный с SqlCeConnection.
Пространство имен: System.Data.SqlServerCe
Сборка: System.Data.SqlServerCe (в system.data.sqlserverce.dll)
Синтаксис
'Декларация
Public Function CreateCommand As SqlCeCommand
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 ('Decker', 'Barbara')"
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 ('Decker', 'Barbara')";
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();
}
Многопоточное использование
Все общие статические члены (Shared в 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
См. также
Справочник
SqlCeConnection Class
SqlCeConnection Members
System.Data.SqlServerCe Namespace