PreviousData Provider for .NET Guide (9.1 revision 1) Next

Using a Local Transaction

Show this topic in Library frames

The following code example, which uses the emp table (see Sample Tables for Pervasive.SQL ), shows how to use a local transaction with a DataReader object on Pervasive.SQL:

PsqlConnection   DBConn; 
DBConn = new PsqlConnection("ServerDSN=Demodata; 
		UID=test; 
		PWD=test; 
		Server=localhost;"); 
PsqlCommand     DBCmd = new PsqlCommand(); 
PsqlTransaction DBTxn = null; 
try  
{ 
    DBConn.Open(); 
    DBTxn = DBConn.BeginTransaction(); 
    // Set the Connection property of the Command object 
    DBCmd.Connection = DBConn; 
    // Set the text of the Command to the INSERT statement 
    DBCmd.CommandText = "insert into emp VALUES 
(15,'HAYES','ADMIN',6, 
                         '2002-APR-17',18000,NULL,4)"; 
    // Set the transaction property of the Command object 
    DBCmd.Transaction = DBTxn; 
    // Execute the statement with ExecuteNonQuery, 
because we are not  
    // returning results 
    DBCmd.ExecuteNonQuery(); 
    // Now commit the transaction 
    DBTxn.Commit(); 
} 
catch (Exception ex)  
{ 
    // Display any exceptions in a messagebox 
    MessageBox.Show (ex.Message); 
    // If anything failed after the connection was opened, 
roll back the  
    // transaction 
    if (DBTxn != null)  
    { 
        DBTxn.Rollback(); 
    } 
} 
//  Close the connection 
DBConn.Close(); 

Chapter contents
Publication contents

Prev topic: Retrieving a Scalar Value
Next topic: Using the CommandBuilder