Skip to main content

InitPhoneAdapter: Part 1

The InitPhoneAdapter method creates a CacheDataAdapter object named phoneAdapter. This adapter connects the data set to the Provider.PhoneNumber table. The first part of the method, displayed below, does the following:

  1. Creates the SQL strings defining the SQL commands supported by phoneAdapter. Note that inserts are handled by a stored procedure defined in Provider.PhoneNumber. The storedproc variable contains the string defining the call to the stored procedure.

  2. Creates the CacheDataAdapter instance. The adapter is initialized with the string containing the SELECT SQL and the CacheConnection instance.

  3. Creates the Insert command for the adapter.

  4. Adds the input parameters to the adapter's Insert command using the AddInputParams method.

  5. Creates an output parameter and adds it to the Insert command. The parameter represents the Provider.PhoneNumber ID. The stored procedure treats the column's values as output parameters.

Add the body of the method to the InitPhoneAdapter stub in PhoneForm.cs.


private void InitPhoneAdapter()
{
  string selectSQL = "Select Contact, %ID, Number, PhoneNumberType " +
                      "from Provider.PhoneNumber";
  string updateSQL = "Update Provider.PhoneNumber Set Number = ?, " +
                       "PhoneNumberType = ? Where ID = ?";
  string storedProc = "{call Provider.PhoneNumber_InsertData(?,?,?)}";
  string deleteSQL = "Delete from Provider.PhoneNumber Where ID = ?";
            
  phoneAdapter = new CacheDataAdapter(selectSQL, cnCache);

  //Create InsertCommand using stored procedure
  phoneAdapter.InsertCommand = cnCache.CreateCommand();
  phoneAdapter.InsertCommand.CommandText = storedProc;

  //Add input parameters for InsertCommand
  AddInputParams(phoneAdapter.InsertCommand, "Number", "PhoneNumberType", 
                 "Contact");

  //Add output parameter - the row id - for the InsertCommand
  CacheParameter Id = phoneAdapter.InsertCommand.CreateParameter(); 
  Id.ParameterName = "ID";
  Id.CacheDbType = CacheDbType.NVarChar;
  Id.Direction = ParameterDirection.Output;
  Id.SourceColumn = "ID";
  phoneAdapter.InsertCommand.Parameters.Add(Id); 
  ...

FeedbackOpens in a new tab