Поделиться через


SqlCeCommand Constructor (String, SqlCeConnection, SqlCeTransaction)

Инициализирует новый экземпляр класса SqlCeCommand по тексту запроса, соединению SqlCeConnection и объекту транзакции SqlCeTransaction.

Пространство имен: System.Data.SqlServerCe
Сборка: System.Data.SqlServerCe (в system.data.sqlserverce.dll)

Синтаксис

'Декларация
Public Sub New ( _
    commandText As String, _
    connection As SqlCeConnection, _
    transaction As SqlCeTransaction _
)
public SqlCeCommand (
    string commandText,
    SqlCeConnection connection,
    SqlCeTransaction transaction
)
public:
SqlCeCommand (
    String^ commandText, 
    SqlCeConnection^ connection, 
    SqlCeTransaction^ transaction
)
public SqlCeCommand (
    String commandText, 
    SqlCeConnection connection, 
    SqlCeTransaction transaction
)
public function SqlCeCommand (
    commandText : String, 
    connection : SqlCeConnection, 
    transaction : SqlCeTransaction
)

Параметры

  • commandText
    Текст запроса.
  • connection
    Объект SqlCeConnection, представляющий соединение с источником данных.
  • transaction
    Транзакция, в которой выполняется команда SqlCeCommand.

Замечания

В следующей таблице приведены начальные значения свойств для экземпляра SqlCeCommand.

Свойство

Начальное значение

CommandText

cmdText

CommandType

Text

Connection

Новый объект SqlCeConnection, который является значением параметра connection.

Изменение значений всех этих параметров производится установкой соответствующих свойств.

Пример

В следующем примере производится создание SqlCeCommand и установка некоторых его свойств.

Dim cmdText As String = "INSERT INTO FactSalesQuota " & _
        "(EmployeeKey, TimeKey, SalesAmountQuota) " & _
        "VALUES (2, 1158, 150000.00)"

Dim conn As New SqlCeConnection("Data Source = AdventureWorks.sdf;")
conn.Open()

' Start a local transaction; SQL Mobile supports the following 
' isolation levels: ReadCommitted, RepeatableRead, Serializable
'
Dim tx As SqlCeTransaction = conn.BeginTransaction(IsolationLevel.ReadCommitted)

' By default, commands run in auto-commit mode; 
'
Dim cmd As New SqlCeCommand(cmdText, conn, tx)

Try
    cmd.ExecuteNonQuery()

    ' Commit the changes to disk if everything above succeeded;
    ' Use Deferred mode for optimal performance; the changes will 
    ' be flashed to disk within the timespan specified in the 
    ' ConnectionString 'FLUSH INTERVAL' property; 
    '
    tx.Commit(CommitMode.Deferred)

    ' Alternatively, you could use:
    ' tx.Commit(CommitMode.Immediate);
    '
    ' or use default (Deferred) commit mode:
    ' tx.Commit()

Catch e As Exception
    ' Handle errors here
    '
    tx.Rollback()
Finally
    conn.Close()
End Try
string cmdText = "INSERT INTO FactSalesQuota " +
    "(EmployeeKey, TimeKey, SalesAmountQuota) " +
    "VALUES (2, 1158, 150000.00)";
        
SqlCeConnection conn = new SqlCeConnection("Data Source = AdventureWorks.sdf;");
conn.Open();

// Start a local transaction; SQL Mobile supports the following 
// isolation levels: ReadCommitted, RepeatableRead, Serializable
//
SqlCeTransaction tx = conn.BeginTransaction(IsolationLevel.ReadCommitted);

SqlCeCommand cmd = new SqlCeCommand(cmdText, conn, tx);

try
{
    cmd.ExecuteNonQuery();

      
    // Commit the changes to disk if everything above succeeded;
    // Use Deferred mode for optimal performance; the changes will 
    // be flushed to disk within the timespan specified in the 
    // ConnectionString 'FLUSH INTERVAL' property; 
    //
    tx.Commit(CommitMode.Deferred);

    // Alternatively, you could use:
    // tx.Commit(CommitMode.Immediate);
    //
    // or use default (Deferred) commit mode:
    // tx.Commit()
}
catch (Exception)
{
    // Handle errors here
    //
    tx.Rollback();
}
finally
{
    conn.Close();
}

Платформы

Платформы разработки

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

См. также

Справочник

SqlCeCommand Class
SqlCeCommand Members
System.Data.SqlServerCe Namespace