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

Updating Data in a Rowset

Show this topic in Library frames

When updating a row at the data source, you call the Update statement. The Update statement uses parameters that contain the unique identifier, such as the primary key, and the columns to be updated, as shown in the following example:

[C#] 
string updateSQL As String = "UPDATE emp SET sal = ?, job 
= ? + 
     = WHERE empno = ?; 

The parameterized query statements define the parameters that will be created. See Parameter Markers for more information about using parameters.

The following code example, which requires the emp table (see Sample Tables for Pervasive.SQL ), uses the Parameters.Add method to create the parameters for the preceding SQL statement, fill a DataSet, and print the updated table:

void test () { 
string selectText = "select sal, job, empno from emp"; 
string updateText = "update emp set sal = ?, job = ? where 
empno = ?"; 
    
PsqlConnection con = new 
PsqlConnection("ServerDSN=DefaultDB;ServerName=loca
lhost;"); 
try { 
 PsqlDataAdapter  adapter = new 
PsqlDataAdapter(selectText, con); 
 PsqlCommand updateCommand = new PsqlCommand(updateText, 
con); 
 adapter.UpdateCommand = updateCommand; 
 updateCommand.Parameters.Add("@sal", 
PsqlDbType.Integer, 15, "SAL"); 
 updateCommand.Parameters.Add("@job", 
PsqlDbType.Varchar, 9, "JOB"); 
  updateCommand.Parameters.Add("@empno",                                       
PsqlDbType.Integer, 15, "empno");  
   DataSet   myDataSet = new DataSet("emp"); 
   adapter.Fill(myDataSet, "emp"); 
   // print 
   PrintTable(myDataSet); 
   // Give employee number 12 a promotion and a raise 
   DataRow changeRow = myDataSet.Tables["emp"].Rows[11]; 
   changeRow["sal"] = "35000"; 
   changeRow["job"] = "MANAGER"; 
   // Send back to database and reprint 
   try 
   { 
      adapter.Update(myDataSet, "emp"); 
      myDataSet.Dispose(); 
      myDataSet = new DataSet(); 
      adapter.Fill(myDataSet, "emp"); 
      PrintTable(myDataSet); 
   } 
   catch (Exception ex)  
   { 
     // Display any exceptions in a messagebox 
     MessageBox.Show (ex.Message); 
   } 
   //  Close the connection 
   con.Close(); 
} 

Chapter contents
Publication contents

Prev topic: Limiting the Rows Returned by a Select Statement
Next topic: Calling a Stored Procedure