⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oledb.cs

📁 人物传记/成功经验人物传记/成功经验人物传记/成功经验人物传记/成功经验人物传记/成功经验
💻 CS
字号:
// ===============================================================================
// Microsoft Data Access Application Block for .NET 3.0
//
// Oldedb.cs
//
// This file contains the implementations of the AdoHelper supporting Sql.
//
// 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.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Xml;
using System.IO;

namespace SkyiSite.DB
{
	/// <summary>
	/// The Sql class is intended to encapsulate high performance, scalable best practices for 
	/// common uses of the Sql ADO.NET provider.  It is created using the abstract factory in AdoHelper
	/// </summary>
	public class Sql : AdoHelper
	{
		/// <summary>
		/// Create an Sql Helper.  Needs to be a default constructor so that the Factory can create it
		/// </summary>
		public Sql()
		{
		}

		#region Overrides
		/// <summary>
		/// Returns an array of SqlParameters of the specified size
		/// </summary>
		/// <param name="size">size of the array</param>
		/// <returns>The array of OdbcParameters</returns>
		protected override IDataParameter[] GetDataParameters(int size)
		{
			return new SqlParameter[size];
		}

		/// <summary>
		/// Returns an SqlConnection object for the given connection string
		/// </summary>
		/// <param name="connectionString">The connection string to be used to create the connection</param>
		/// <returns>An SqlConnection object</returns>
		public override IDbConnection GetConnection( string connectionString )
		{
			return new SqlConnection( connectionString );
		}

		/// <summary>
		/// Returns an SqlDataAdapter object
		/// </summary>
		/// <returns>The SqlDataAdapter</returns>
		public override IDbDataAdapter GetDataAdapter()
		{
			return new SqlDataAdapter(); 
		}

		/// <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 override void DeriveParameters( IDbCommand cmd )
		{
			bool mustCloseConnection = false;

			if( !( cmd is SqlCommand ) )
				throw new ArgumentException( "The command provided is not a SqlCommand instance.", "cmd" );
	
			if (cmd.Connection.State != ConnectionState.Open) 
			{
				cmd.Connection.Open();
				mustCloseConnection = true;
			}
			
			SqlCommandBuilder.DeriveParameters( (SqlCommand)cmd );

			if (mustCloseConnection)
			{
				cmd.Connection.Close();
			}
		}

		/// <summary>
		/// Returns an SqlParameter object
		/// </summary>
		/// <returns>The SqlParameter object</returns>
		public override IDataParameter GetParameter()
		{
			return new SqlParameter(); 
		}

		/// <summary>
		/// This cleans up the parameter syntax for an Sql call.  This was split out from PrepareCommand so that it could be called independently.
		/// </summary>
		/// <param name="command">An IDbCommand object containing the CommandText to clean.</param>
		public override void CleanParameterSyntax(IDbCommand command)
		{
		}

		/// <summary>
		/// Execute an IDbCommand (that returns a resultset) against the provided IDbConnection. 
		/// </summary>
		/// <example>
		/// <code>
		/// XmlReader r = helper.ExecuteXmlReader(command);
		/// </code></example>
		/// <param name="command">The IDbCommand to execute</param>
		/// <returns>An XmlReader containing the resultset generated by the command</returns>
		public override XmlReader ExecuteXmlReader(IDbCommand command)
		{
			bool mustCloseConnection = false;

			if (command.Connection.State != ConnectionState.Open) 
			{
				command.Connection.Open();
				mustCloseConnection = true;
			}

			CleanParameterSyntax(command);

			SqlDataAdapter da = new SqlDataAdapter((SqlCommand)command);
			DataSet ds = new DataSet();

			da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
			da.Fill(ds);

			StringReader stream = new StringReader(ds.GetXml());
			if (mustCloseConnection)
			{
				command.Connection.Close();
			}

			return new XmlTextReader(stream);
		}

		/// <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 override void AddUpdateEventHandlers(IDbDataAdapter dataAdapter, RowUpdatingHandler rowUpdatingHandler, RowUpdatedHandler rowUpdatedHandler)
		{
			if (rowUpdatingHandler != null)
			{
				this.m_rowUpdating = rowUpdatingHandler;
				((SqlDataAdapter)dataAdapter).RowUpdating += new SqlRowUpdatingEventHandler(RowUpdating);
			}

			if (rowUpdatedHandler != null)
			{
				this.m_rowUpdated = rowUpdatedHandler;
				((SqlDataAdapter)dataAdapter).RowUpdated +=  new SqlRowUpdatedEventHandler(RowUpdated);
			}
		}

		/// <summary>
		/// Handles the RowUpdating event
		/// </summary>
		/// <param name="obj">The object that published the event</param>
		/// <param name="e">The SqlRowUpdatingEventArgs</param>
		protected void RowUpdating(object obj, SqlRowUpdatingEventArgs e)
		{
			base.RowUpdating(obj, e);
		}

		/// <summary>
		/// Handles the RowUpdated event
		/// </summary>
		/// <param name="obj">The object that published the event</param>
		/// <param name="e">The SqlRowUpdatedEventArgs</param>
		protected void RowUpdated(object obj, SqlRowUpdatedEventArgs e)
		{
			base.RowUpdated(obj, e);
		}
		
		/// <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 override IDataParameter GetBlobParameter(IDbConnection connection, IDataParameter p)
		{
			// nothing special needed for Sql...as far as we know now
			return p;
		}
		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -