Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

SqlCeCommand Constructor (String, SqlCeConnection, SqlCeTransaction)

Initializes a new instance of the SqlCeCommand class with the text of the query, a SqlCeConnection, and the SqlCeTransaction.

Namespace:  System.Data.SqlServerCe
Assembly:  System.Data.SqlServerCe (in System.Data.SqlServerCe.dll)

public SqlCeCommand(
	string commandText,
	SqlCeConnection connection,
	SqlCeTransaction transaction
)

Parameters

commandText
Type: System.String
The text of the query.
connection
Type: System.Data.SqlServerCe.SqlCeConnection
A SqlCeConnection that represents the connection to a data source.
transaction
Type: System.Data.SqlServerCe.SqlCeTransaction
The transaction in which the SqlCeCommand executes.

The following table shows initial property values for an instance of SqlCeCommand.

Properties

Initial Value

CommandText

cmdText

CommandType

Text

Connection

A new SqlCeConnection that is the value for the connection parameter.

You can change the value for any of these parameters by setting the related property.

The following example creates a SqlCeCommand and sets some of its properties.

		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();
        }


Show:
© 2015 Microsoft