📄 adohelper.cs
字号:
// ===============================================================================
// Microsoft Data Access Application Block for .NET 3.0
//
// AdoHelper.cs
//
// This file contains an abstract implementations of the AdoHelper class.
//
// For more information see the Documentation.
// ===============================================================================
// Release history
// VERSION DESCRIPTION
// 2.0 Added support for FillDataset, UpdateDataset and "Param" helper methods
// 3.0 New abstract class supporting the same methods using ADO.NET interfaces
//
// ===============================================================================
// Copyright (C) 2000-2001 Microsoft Corporation
// All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
// ==============================================================================
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Reflection;
using System.Xml;
using System.Diagnostics;
namespace Club.Framework.Data
{
/// <summary>
/// The AdoHelper class is intended to encapsulate high performance, scalable best practices for
/// common data access uses. It uses the Abstract Factory pattern to be easily extensible
/// to any ADO.NET provider. The current implementation provides helpers for SQL Server, ODBC,
/// OLEDB, and Oracle.
/// </summary>
public abstract class AdoHelper
{
/// <summary>
/// This enum is used to indicate whether the connection was provided by the caller, or created by AdoHelper, so that
/// we can set the appropriate CommandBehavior when calling ExecuteReader()
/// </summary>
protected enum AdoConnectionOwnership
{
/// <summary>Connection is owned and managed by ADOHelper</summary>
Internal,
/// <summary>Connection is owned and managed by the caller</summary>
External
}
#region Declare members
// necessary for handling the general case of needing event handlers for RowUpdating/ed events
/// <summary>
/// Internal handler used for bubbling up the event to the user
/// </summary>
protected RowUpdatingHandler m_rowUpdating;
/// <summary>
/// Internal handler used for bubbling up the event to the user
/// </summary>
protected RowUpdatedHandler m_rowUpdated;
#endregion
#region Provider specific abstract methods
/// <summary>
/// Returns an IDbConnection object for the given connection string
/// </summary>
/// <param name="connectionString">The connection string to be used to create the connection</param>
/// <returns>An IDbConnection object</returns>
/// <exception cref="System.ArgumentNullException">Thrown if connectionString is null</exception>
public abstract IDbConnection GetConnection( string connectionString );
/// <summary>
/// Returns an IDbDataAdapter object
/// </summary>
/// <returns>The IDbDataAdapter</returns>
public abstract IDbDataAdapter GetDataAdapter();
/// <summary>
/// Calls the CommandBuilder.DeriveParameters method for the specified provider, doing any setup and cleanup necessary
/// </summary>
/// <param name="cmd">The IDbCommand referencing the stored procedure from which the parameter information is to be derived. The derived parameters are added to the Parameters collection of the IDbCommand. </param>
public abstract void DeriveParameters( IDbCommand cmd );
/// <summary>
/// Returns an IDataParameter object
/// </summary>
/// <returns>The IDataParameter object</returns>
public abstract IDataParameter GetParameter();
/// <summary>
/// Execute an IDbCommand (that returns a resultset) against the provided IDbConnection.
/// </summary>
/// <example>
/// <code>
/// XmlReader r = helper.ExecuteXmlReader(command);
/// </code></example>
/// <param name="cmd">The IDbCommand to execute</param>
/// <returns>An XmlReader containing the resultset generated by the command</returns>
/// <exception cref="System.ArgumentNullException">Thrown if command is null.</exception>
public abstract XmlReader ExecuteXmlReader( IDbCommand cmd );
/// <summary>
/// Provider specific code to set up the updating/ed event handlers used by UpdateDataset
/// </summary>
/// <param name="dataAdapter">DataAdapter to attach the event handlers to</param>
/// <param name="rowUpdatingHandler">The handler to be called when a row is updating</param>
/// <param name="rowUpdatedHandler">The handler to be called when a row is updated</param>
protected abstract void AddUpdateEventHandlers(IDbDataAdapter dataAdapter, RowUpdatingHandler rowUpdatingHandler, RowUpdatedHandler rowUpdatedHandler);
/// <summary>
/// Returns an array of IDataParameters of the specified size
/// </summary>
/// <param name="size">size of the array</param>
/// <returns>The array of IDataParameters</returns>
protected abstract IDataParameter[] GetDataParameters(int size);
/// <summary>
/// Handle any provider-specific issues with BLOBs here by "washing" the IDataParameter and returning a new one that is set up appropriately for the provider.
/// </summary>
/// <param name="connection">The IDbConnection to use in cleansing the parameter</param>
/// <param name="p">The parameter before cleansing</param>
/// <returns>The parameter after it's been cleansed.</returns>
protected abstract IDataParameter GetBlobParameter(IDbConnection connection, IDataParameter p);
#endregion
#region Delegates
// also used in our general case of RowUpdating/ed events
/// <summary>
/// Delegate for creating a RowUpdatingEvent handler
/// </summary>
/// <param name="obj">The object that published the event</param>
/// <param name="e">The RowUpdatingEventArgs for the event</param>
public delegate void RowUpdatingHandler(object obj, RowUpdatingEventArgs e);
/// <summary>
/// Delegate for creating a RowUpdatedEvent handler
/// </summary>
/// <param name="obj">The object that published the event</param>
/// <param name="e">The RowUpdatedEventArgs for the event</param>
public delegate void RowUpdatedHandler(object obj, RowUpdatedEventArgs e);
#endregion
#region Factory
/// <summary>
/// Create an AdoHelper for working with a specific provider (i.e. Sql, Odbc, OleDb, Oracle)
/// </summary>
/// <param name="providerAssembly">Assembly containing the specified helper subclass</param>
/// <param name="providerType">Specific type of the provider</param>
/// <returns>An AdoHelper instance of the specified type</returns>
/// <example><code>
/// AdoHelper helper = AdoHelper.CreateHelper("GotDotNet.ApplicationBlocks.Data", "GotDotNet.ApplicationBlocks.Data.OleDb");
/// </code></example>
public static AdoHelper CreateHelper( string providerAssembly, string providerType )
{
Assembly assembly = Assembly.Load( providerAssembly );
object provider = assembly.CreateInstance( providerType );
if( provider is AdoHelper )
{
return provider as AdoHelper;
}
else
{
throw new InvalidOperationException( "The provider specified does not extend the AdoHelper abstract class." );
}
}
/// <summary>
/// Create an AdoHelper instance for working with a specific provider by using a providerAlias specified in the App.Config file.
/// </summary>
/// <param name="providerAlias">The alias to look up</param>
/// <returns>An AdoHelper instance of the specified type</returns>
/// <example><code>
/// AdoHelper helper = AdoHelper.CreateHelper("OracleHelper");
/// </code></example>
public static AdoHelper CreateHelper( string providerAlias )
{
IDictionary dict;
try
{
dict = ConfigurationSettings.GetConfig( "daabProviders" ) as IDictionary;
}
catch( Exception e )
{
throw new InvalidOperationException( "If the section is not defined on the configuration file this method can't be used to create an AdoHelper instance.", e );
}
ProviderAlias providerConfig = dict[ providerAlias ] as ProviderAlias;
string providerAssembly = providerConfig.AssemblyName;
string providerType = providerConfig.TypeName;
Assembly assembly = Assembly.Load( providerAssembly );
object provider = assembly.CreateInstance( providerType );
if( provider is AdoHelper )
{
return provider as AdoHelper;
}
else
{
throw new InvalidOperationException( "The provider specified does not extends the AdoHelper abstract class." );
}
}
#endregion
#region GetParameter
/// <summary>
/// Get an IDataParameter for use in a SQL command
/// </summary>
/// <param name="name">The name of the parameter to create</param>
/// <param name="value">The value of the specified parameter</param>
/// <returns>An IDataParameter object</returns>
public virtual IDataParameter GetParameter( string name, object value )
{
IDataParameter parameter = GetParameter();
parameter.ParameterName = name;
parameter.Value = value;
return parameter;
}
/// <summary>
/// Get an IDataParameter for use in a SQL command
/// </summary>
/// <param name="name">The name of the parameter to create</param>
/// <param name="dbType">The System.Data.DbType of the parameter</param>
/// <param name="size">The size of the parameter</param>
/// <param name="direction">The System.Data.ParameterDirection of the parameter</param>
/// <returns>An IDataParameter object</returns>
public virtual IDataParameter GetParameter ( string name, DbType dbType, int size, ParameterDirection direction )
{
IDataParameter dataParameter = GetParameter();
dataParameter.DbType = dbType;
dataParameter.Direction = direction;
dataParameter.ParameterName = name;
if (size > 0 && dataParameter is IDbDataParameter)
{
IDbDataParameter dbDataParameter = (IDbDataParameter)dataParameter;
dbDataParameter.Size = size;
}
return dataParameter;
}
/// <summary>
/// Get an IDataParameter for use in a SQL command
/// </summary>
/// <param name="name">The name of the parameter to create</param>
/// <param name="dbType">The System.Data.DbType of the parameter</param>
/// <param name="size">The size of the parameter</param>
/// <param name="sourceColumn">The source column of the parameter</param>
/// <param name="sourceVersion">The System.Data.DataRowVersion of the parameter</param>
/// <returns>An IDataParameter object</returns>
public virtual IDataParameter GetParameter ( string name, DbType dbType, int size, string sourceColumn, DataRowVersion sourceVersion )
{
IDataParameter dataParameter = GetParameter();
dataParameter.DbType = dbType;
dataParameter.ParameterName = name;
dataParameter.SourceColumn = sourceColumn;
dataParameter.SourceVersion = sourceVersion;
if (size > 0 && dataParameter is IDbDataParameter)
{
IDbDataParameter dbDataParameter = (IDbDataParameter)dataParameter;
dbDataParameter.Size = size;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -