Using DataAdaptors

From PeformIQ Upgrade
Jump to navigation Jump to search

http://dotnetperls.com/datagridview-tutorial

See:

public class Supplier
{
  protected SqlDataAdapter DataAdapter;
 
  public DataSet GetSupplierByID(int supplierID)
  {
    // Build the connection string
    string ConnectionString = "server=(local);"+
       "uid=sa;pwd=;"+
       "database=Northwind";
 
    // Build the command string
    string CommandString = "SELECT supplierid, CompanyName" +
       " FROM Suppliers WHERE supplierid = " + supplierID;
 
    // Create the data adapter, passing the command and connection strings
    DataAdapter = new SqlDataAdapter(CommandString, ConnectionString);
 
    // Instantiate a DataSet object
    DataSet ds = new DataSet();
 
     // Fill the DataSet
    DataAdapter.Fill(ds);
 
    return ds;
  }
 
  /// Save the specified DataSet
  public int Save(DataSet ds)
  {
    // Create a Command Builder and build the delete, update and insert commands
    SqlCommandBuilder CommandBuilder = new SqlCommandBuilder(DataAdapter);
    DataAdapter.DeleteCommand = CommandBuilder.GetDeleteCommand();
    DataAdapter.UpdateCommand = CommandBuilder.GetUpdateCommand();
    DataAdapter.InsertCommand = CommandBuilder.GetInsertCommand();
 
    // Update the data in thet DataSet
    int RowsUpdated = DataAdapter.Update(ds, ds.Tables[0].ToString());
 
    return RowsUpdated;
  }
}