#region Copyright
// Advanced.Data.Provider.AdpDataAdapter
//
// Copyright (C) 2004 Astrein Engenharia de Manuteno S/A
// Copyright (C) 2004 Everaldo Canuto <everaldo_canuto@yahoo.com.br>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#endregion
using System;
using System.Data;
using System.Data.Common;
namespace Advanced.Data.Provider{
/// <summary>
/// Represents a set of data commands and a database connection that are used to fill the <see cref='DataSet'/> and update a database server.
/// </summary>
public class AdpDataAdapter : IAdpDataAdapter
{
#region Fields
internal IDbDataAdapter dataAdapter;
private AdpCommand _Select;
private AdpCommand _Delete;
private AdpCommand _Update;
private AdpCommand _Insert;
#endregion
#region Constructors e destructors
/// <summary>
/// Initializes a new instance of the <see cref='AdpDataAdapter'/> class.
/// </summary>
public AdpDataAdapter()
{
}
/// <summary>
/// Initializes a new instance of the <see cref='AdpDataAdapter'/> class with the specified <see cref='AdpCommand'/> as the SelectCommand property.
/// </summary>
/// <param name="selectCommand">A <see cref='AdpCommand'/> that is a Transact-SQL SELECT statement or stored procedure and is set as the SelectCommand property of the <see cref='AdpDataAdapter'/>.</param>
public AdpDataAdapter(AdpCommand selectCommand)
{
this.SelectCommand = selectCommand;
}
/// <summary>
/// Initializes a new instance of the <see cref='AdpDataAdapter'/> class with a SelectCommand and a <see cref='AdpConnection'/> object.
/// </summary>
/// <param name="selectCommandText">A string that is a Transact-SQL SELECT statement or stored procedure and is set as the SelectCommand property of the <see cref='AdpDataAdapter'/>.</param>
/// <param name="selectConnection">A <see cref='AdpConnection'/> that represents the connection.</param>
public AdpDataAdapter(string selectCommandText, AdpConnection selectConnection)
{
this.SelectCommand = new AdpCommand(selectCommandText, selectConnection);
}
/// <summary>
/// Initializes a new instance of the <see cref='AdpDataAdapter'/> class with a SelectCommand and a connection string.
/// </summary>
/// <param name="selectCommandText">A string that is a Transact-SQL SELECT statement or stored procedure and is set as the SelectCommand property of the <see cref='AdpDataAdapter'/>.</param>
/// <param name="selectConnectionString">The connection string.</param>
public AdpDataAdapter(string selectCommandText, string selectConnectionString)
{
this.SelectCommand = new AdpCommand(selectCommandText, new AdpConnection(selectConnectionString));
}
#endregion
#region Public properties
/// <summary>
/// Gets or sets a Transact-SQL statement or stored procedure used to update records in the data source.
/// </summary>
IDbCommand System.Data.IDbDataAdapter.UpdateCommand
{
get
{
return _Update;
}
set
{
_Update=(AdpCommand)value;
SetAdapter(_Update);
dataAdapter.UpdateCommand=_Update.command;
}
}
/// <summary>
/// Gets or sets a Transact-SQL statement or stored procedure used to update records in the data source.
/// </summary>
public AdpCommand UpdateCommand
{
get
{
return (AdpCommand)((IDbDataAdapter)this).UpdateCommand;
}
set
{
((IDbDataAdapter)this).UpdateCommand=value;
}
}
/// <summary>
/// Gets or sets a Transact-SQL statement or stored procedure used to select records in the data source.
/// </summary>
IDbCommand System.Data.IDbDataAdapter.SelectCommand
{
get
{
return _Select;
}
set
{
_Select=(AdpCommand)value;
SetAdapter(_Select);
dataAdapter.SelectCommand=_Select.command;
}
}
/// <summary>
/// Gets or sets a Transact-SQL statement or stored procedure used to select records in the data source.
/// </summary>
public AdpCommand SelectCommand
{
get
{
return (AdpCommand)((IDbDataAdapter)this).SelectCommand;
}
set
{
((IDbDataAdapter)this).SelectCommand=value;
}
}
/// <summary>
/// Gets or sets a Transact-SQL statement or stored procedure to delete records from the data set.
/// </summary>
IDbCommand System.Data.IDbDataAdapter.DeleteCommand
{
get
{
return _Delete;
}
set
{
_Delete=(AdpCommand)value;
SetAdapter(_Delete);
dataAdapter.DeleteCommand=_Delete.command;
}
}
/// <summary>
/// Gets or sets a Transact-SQL statement or stored procedure to delete records from the data set.
/// </summary>
public AdpCommand DeleteCommand
{
get
{
return (AdpCommand)((IDbDataAdapter)this).DeleteCommand;
}
set
{
((IDbDataAdapter)this).DeleteCommand=value;
}
}
/// <summary>
/// Gets or sets a Transact-SQL statement or stored procedure to insert new records into the data source.
/// </summary>
IDbCommand System.Data.IDbDataAdapter.InsertCommand
{
get
{
return _Insert;
}
set
{
_Insert=(AdpCommand)value;
SetAdapter(_Insert);
dataAdapter.InsertCommand=_Insert.command;
}
}
/// <summary>
/// Gets or sets a Transact-SQL statement or stored procedure to insert new records into the data source.
/// </summary>
public AdpCommand InsertCommand
{
get
{
return (AdpCommand)((IDbDataAdapter)this).InsertCommand;
}
set
{
((IDbDataAdapter)this).InsertCommand=value;
}
}
/// <summary>
/// Gets a collection that provides the master mapping between a source table and a DataTable.
/// </summary>
public ITableMappingCollection TableMappings
{
get
{
return dataAdapter.TableMappings;
}
}
/// <summary>
/// Determines the action to take when existing <see cref='DataSet'/> schema does not match incoming data.
/// </summary>
public System.Data.MissingSchemaAction MissingSchemaAction
{
get
{
return dataAdapter.MissingSchemaAction;
}
set
{
dataAdapter.MissingSchemaAction=value;
}
}
/// <summary>
/// Determines the action to take when incoming data does not have a matching table or column.
/// </summary>
public System.Data.MissingMappingAction MissingMappingAction
{
get
{
return dataAdapter.MissingMappingAction;
}
set
{
dataAdapter.MissingMappingAction=value;
}
}
#endregion
#region Public methods
/// <summary>
/// Adds or refreshes rows in the DataSet to match those in the data source using the <see cref='DataSet'/> name, and creates a <see cref='DataTable'/> named "Table".
/// </summary>
/// <param name="dataSet">A <see cref='DataSet'/> to fill with records and, if necessary, schema.</param>
/// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows.</returns>
public int Fill(DataSet dataSet)
{
if (dataAdapter==null) SetAdapter(_Select);
if (dataAdapter==null)
throw new Exception("DataAdapter cannot be determined.Check select command and select command's connection");
return dataAdapter.Fill(dataSet);
}
/// <summary>
/// Adds or refreshes rows in the <see cref='DataSet'/> to match those in the data source using the <see cref='DataSet'/> and <see cref='DataTable'/> names.
/// </summary>
/// <param name="dataSet">A <see cref='DataSet'/> to fill with records and, if necessary, schema.</param>
/// <param name="tableName">The name of the source table to use for table mapping.</param>
/// <returns>The number of rows successfully added to or refreshed in the <see cref='DataSet'/>. This does not include rows affected by statements that do not return rows.</returns>
public int Fill(DataSet dataSet, string tableName)
{
return ((DbDataAdapter) dataAdapter).Fill(dataSet, tableName);
}
/// <summary>
/// Gets the parameters set by the user when executing an SQL SELECT statement.
/// </summary>
/// <returns>An array of IDataParameter objects that contains the parameters set by the user.</returns>
public IDataParameter[] GetFillParameters()
{
return dataAdapter.GetFillParameters();
}
/// <summary>
/// Adds a <see cref='DataTable'/> named "Table" to the specified <see cref='DataSet'/> and configures the schema to match that in the data source based on the specified <see cref='SchemaType'/>.
/// </summary>
/// <param name="dataSet">A <see cref='DataSet'/> to insert the schema in.</param>
/// <param name="schemaType">One of the <see cref='SchemaType'/> values that specify how to insert the schema.</param>
/// <returns>A reference to a collection of <see cref='DataTable'/> objects that were added to the <see cref='DataSet'/>.</returns>
public DataTable[] FillSchema(DataSet dataSet, System.Data.SchemaType schemaType)
{
return dataAdapter.FillSchema(dataSet,schemaType);
}
/// <summary>
/// Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified <see cref='DataSet'/>.
/// </summary>
/// <param name="dataRow">A DataRow object used to update the data source.</param>
/// <returns>The number of rows successfully updated from the <see cref='DataSet'/>.</returns>
public int Update(DataRow dataRow)
{
DataRow[] rows = {dataRow};
return ((DbDataAdapter) dataAdapter).Update(rows);
}
/// <summary>
/// Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified <see cref='DataSet'/>.
/// </summary>
/// <param name="dataRows">An array of DataRow objects used to update the data source.</param>
/// <returns>The number of rows successfully updated from the <see cref='DataSet'/>.</returns>
public int Update(DataRow[] dataRows)
{
return ((DbDataAdapter) dataAdapter).Update(dataRows);
}
/// <summary>
/// Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified <see cref='DataSet'/>.
/// </summary>
/// <param name="dataSet">The <see cref='DataSet'/> used to update the data source.</param>
/// <returns>The number of rows successfully updated from the <see cref='DataSet'/>.</returns>
public int Update(DataSet dataSet)
{
return dataAdapter.Update(dataSet);
}
/// <summary>
/// Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified <see cref='DataSet'/>.
/// </summary>
/// <param name="dataSet">A <see cref='DataSet'/> to fill with records and, if necessary, schema.</param>
/// <param name="tableName">The name of the source table to use for table mapping.</param>
/// <returns>The number of rows successfully updated from the <see cref='DataSet'/>.</returns>
public int Update(DataSet dataSet, string tableName)
{
return ((DbDataAdapter) dataAdapter).Update(dataSet, tableName);
}
#endregion
#region Private methods
private void SetAdapter(AdpCommand command)
{
if (command!=null && command.Connection!=null && dataAdapter==null)
dataAdapter = command.connection.Provider.CreateDataAdapter();
}
#endregion
}
}
|