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

📄 adohelper.cs

📁 1、用SQL查询器打开install目录下的dooogo.sql运行之后创建数据库dooogo。 2、然后打开web.config修改 <DbProvider type="Club.Fram
💻 CS
📖 第 1 页 / 共 5 页
字号:
			return dataParameter;
		}
		#endregion

		#region private utility methods
		/// <summary>
		/// This method is used to attach array of IDataParameters to an IDbCommand.
		/// 
		/// This method will assign a value of DbNull to any parameter with a direction of
		/// InputOutput and a value of null.  
		/// 
		/// This behavior will prevent default values from being used, but
		/// this will be the less common case than an intended pure output parameter (derived as InputOutput)
		/// where the user provided no input value.
		/// </summary>
		/// <param name="command">The command to which the parameters will be added</param>
		/// <param name="commandParameters">An array of IDataParameterParameters to be added to command</param>
		/// <exception cref="System.ArgumentNullException">Thrown if command is null.</exception>
		protected virtual void AttachParameters(IDbCommand command, IDataParameter[] commandParameters)
		{
			if( command == null ) throw new ArgumentNullException( "command" );
			if( commandParameters != null )
			{
				foreach (IDataParameter p in commandParameters)
				{
					if( p != null )
					{
						// Check for derived output value with no value assigned
						if ( ( p.Direction == ParameterDirection.InputOutput || 
							p.Direction == ParameterDirection.Input ) && 
							(p.Value == null))
						{
							p.Value = DBNull.Value;
						}
						if (p.DbType == DbType.Binary) 
						{
							// special handling for BLOBs
							command.Parameters.Add(GetBlobParameter(command.Connection, p));
						}
						else
						{
							command.Parameters.Add(p);
						}
					}
				}
			}
		}

		/// <summary>
		/// This method assigns dataRow column values to an IDataParameterCollection
		/// </summary>
		/// <param name="commandParameters">The IDataParameterCollection to be assigned values</param>
		/// <param name="dataRow">The dataRow used to hold the stored procedure's parameter values</param>
		/// <exception cref="System.InvalidOperationException">Thrown if any of the parameter names are invalid.</exception>
		protected internal void AssignParameterValues(IDataParameterCollection commandParameters, DataRow dataRow)
		{
			if (commandParameters == null || dataRow == null)
			{
				// Do nothing if we get no data
				return;
			}

			DataColumnCollection columns = dataRow.Table.Columns;

			int i = 0;
			// Set the parameters values
			foreach(IDataParameter commandParameter in commandParameters)
			{
				// Check the parameter name
				if( commandParameter.ParameterName == null || 
					commandParameter.ParameterName.Length <= 1 )
					throw new InvalidOperationException( string.Format( 
						"Please provide a valid parameter name on the parameter #{0}, the ParameterName property has the following value: '{1}'.", 
						i, commandParameter.ParameterName ) );

				if (columns.Contains( commandParameter.ParameterName ) )
					commandParameter.Value = dataRow[commandParameter.ParameterName];
				else if(columns.Contains( commandParameter.ParameterName.Substring(1) ) )
					commandParameter.Value = dataRow[commandParameter.ParameterName.Substring(1)];
	
				i++;
			}
		}

		/// <summary>
		/// This method assigns dataRow column values to an array of IDataParameters
		/// </summary>
		/// <param name="commandParameters">Array of IDataParameters to be assigned values</param>
		/// <param name="dataRow">The dataRow used to hold the stored procedure's parameter values</param>
		/// <exception cref="System.InvalidOperationException">Thrown if any of the parameter names are invalid.</exception>
		protected void AssignParameterValues(IDataParameter[] commandParameters, DataRow dataRow)
		{
			if ((commandParameters == null) || (dataRow == null)) 
			{
				// Do nothing if we get no data
				return;
			}

			DataColumnCollection columns = dataRow.Table.Columns;

			int i = 0;
			// Set the parameters values
			foreach(IDataParameter commandParameter in commandParameters)
			{
				// Check the parameter name
				if( commandParameter.ParameterName == null || 
					commandParameter.ParameterName.Length <= 1 )
					throw new InvalidOperationException( string.Format( 
						"Please provide a valid parameter name on the parameter #{0}, the ParameterName property has the following value: '{1}'.", 
						i, commandParameter.ParameterName ) );

				if (columns.Contains( commandParameter.ParameterName ) )
					commandParameter.Value = dataRow[commandParameter.ParameterName];
				else if(columns.Contains( commandParameter.ParameterName.Substring(1) ) )
					commandParameter.Value = dataRow[commandParameter.ParameterName.Substring(1)];
	
				i++;
			}
		}

		/// <summary>
		/// This method assigns an array of values to an array of IDataParameters
		/// </summary>
		/// <param name="commandParameters">Array of IDataParameters to be assigned values</param>
		/// <param name="parameterValues">Array of objects holding the values to be assigned</param>
		/// <exception cref="System.ArgumentException">Thrown if an incorrect number of parameters are passed.</exception>
		protected void AssignParameterValues(IDataParameter[] commandParameters, object[] parameterValues)
		{
			if ((commandParameters == null) || (parameterValues == null)) 
			{
				// Do nothing if we get no data
				return;
			}

			// We must have the same number of values as we pave parameters to put them in
			if (commandParameters.Length != parameterValues.Length)
			{
				throw new ArgumentException("Parameter count does not match Parameter Value count.");
			}

			// Iterate through the IDataParameters, assigning the values from the corresponding position in the 
			// value array
			for (int i = 0, j = commandParameters.Length, k = 0; i < j; i++)
			{
				if (commandParameters[i].Direction != ParameterDirection.ReturnValue)
				{
					// If the current array value derives from IDataParameter, then assign its Value property
					if (parameterValues[k] is IDataParameter)
					{
						IDataParameter paramInstance;
						paramInstance = (IDataParameter)parameterValues[k];
						if (paramInstance.Direction == ParameterDirection.ReturnValue)
						{
							paramInstance = (IDataParameter)parameterValues[++k];
						}
						if( paramInstance.Value == null )
						{
							commandParameters[i].Value = DBNull.Value; 
						}
						else
						{
							commandParameters[i].Value = paramInstance.Value;
						}
					}
					else if (parameterValues[k] == null)
					{
						commandParameters[i].Value = DBNull.Value;
					}
					else
					{
						commandParameters[i].Value = parameterValues[k];
					}
					k++;
				}
			}
		}

		/// <summary>
		/// This method cleans up the parameter syntax for the provider
		/// </summary>
		/// <param name="command">The IDbCommand containing the parameters to clean up.</param>
		public virtual void CleanParameterSyntax(IDbCommand command)
		{
			// do nothing by default
		}

		/// <summary>
		/// This method opens (if necessary) and assigns a connection, transaction, command type and parameters 
		/// to the provided command
		/// </summary>
		/// <param name="command">The IDbCommand to be prepared</param>
		/// <param name="connection">A valid IDbConnection, on which to execute this command</param>
		/// <param name="transaction">A valid IDbTransaction, or 'null'</param>
		/// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
		/// <param name="commandText">The stored procedure name or SQL command</param>
		/// <param name="commandParameters">An array of IDataParameters to be associated with the command or 'null' if no parameters are required</param>
		/// <param name="mustCloseConnection"><c>true</c> if the connection was opened by the method, otherwose is false.</param>
		/// <exception cref="System.ArgumentNullException">Thrown if command is null.</exception>
		/// <exception cref="System.ArgumentNullException">Thrown if commandText is null.</exception>
		protected virtual void PrepareCommand(IDbCommand command, IDbConnection connection, IDbTransaction transaction, CommandType commandType, string commandText, IDataParameter[] commandParameters, out bool mustCloseConnection )
		{
			if( command == null ) throw new ArgumentNullException( "command" );
			if( commandText == null || commandText.Length == 0 ) throw new ArgumentNullException( "commandText" );

			// If the provided connection is not open, we will open it
			if (connection.State != ConnectionState.Open)
			{
				mustCloseConnection = true;
				connection.Open();
			}
			else
			{
				mustCloseConnection = false;
			}

			// Associate the connection with the command
			command.Connection = connection;

			// Set the command text (stored procedure name or SQL statement)
			command.CommandText = commandText;

			// If we were provided a transaction, assign it
			if (transaction != null)
			{
				if( transaction.Connection == null ) throw new ArgumentException( "The transaction was rolled back or commited, please provide an open transaction.", "transaction" );
				command.Transaction = transaction;
			}

			// Set the command type
			command.CommandType = commandType;

			// Attach the command parameters if they are provided
			if (commandParameters != null)
			{
				AttachParameters(command, commandParameters);
			}
			return;
		}

		/// <summary>
		/// This method clears (if necessary) the connection, transaction, command type and parameters 
		/// from the provided command
		/// </summary>
		/// <remarks>
		/// Not implemented here because the behavior of this method differs on each data provider. 
		/// </remarks>
		/// <param name="command">The IDbCommand to be cleared</param>
		protected virtual void ClearCommand(IDbCommand command )
		{
			// do nothing by default
		}

		#endregion private utility methods

		#region ExecuteDataset

		/// <summary>
		/// Execute an IDbCommand (that returns a resultset) against the database specified in 
		/// the connection string. 
		/// </summary>
		/// <param name="command">The IDbCommand object to use</param>
		/// <returns>A DataSet containing the resultset generated by the command</returns>
		/// <exception cref="System.ArgumentNullException">Thrown if command is null.</exception>
		public virtual DataSet ExecuteDataset(IDbCommand command)
		{
			bool mustCloseConnection = false;

			// Clean Up Parameter Syntax
			CleanParameterSyntax(command);

⌨️ 快捷键说明

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