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

📄 sqlhelper.cs

📁 比较好的异步数据库访问示例
💻 CS
📖 第 1 页 / 共 5 页
字号:
		/// <param name="transaction">a valid IDbTransaction</param>
		/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
		/// <param name="commandText">the stored procedure name or T-SQL command</param>
		/// <param name="ac">callback object containing a delegate</param>
		/// <param name="state">state object supplied by the client</param>
		/// <param name="commandParameters">an array of IDataParameters used to execute the command</param>
		/// <returns>asynchronous result object</returns>
		public static IAsyncResult BeginExecuteScalar
			(IDbTransaction transaction, CommandType commandType, string commandText, AsyncCallback ac, Object state, params IDataParameter[] commandParameters)
		{
			ExecuteScalarDelegate8 d = new ExecuteScalarDelegate8(ExecuteScalar);
			IAsyncResult result = d.BeginInvoke(transaction, commandType, commandText, commandParameters, ac, state);
			return result;
		}

		/// <summary>
		/// Execute a stored procedure via a command (that returns a 1x1 resultset) against the specified
		/// IDbTransaction using the provided parameter values.  This method will query the database to discover the parameters for the 
		/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
		/// </summary>
		/// <param name="transaction">a valid IDbTransaction</param>
		/// <param name="spName">the name of the stored procedure</param>
		/// <param name="ac">callback object containing a delegate</param>
		/// <param name="state">state object supplied by the client</param>
		/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
		/// <returns>asynchronous result object</returns>
		public static IAsyncResult BeginExecuteScalar
			(IDbTransaction transaction, string spName, AsyncCallback ac, Object state, params object[] parameterValues)
		{
			ExecuteScalarDelegate9 d = new ExecuteScalarDelegate9(ExecuteScalar);
			IAsyncResult result = d.BeginInvoke(transaction, spName, parameterValues, ac, state);
			return result;
		}

		/// <summary>
		/// Receive the result at the end of an asynchronous call.
		/// </summary>
		/// <param name="result">asynchronous result object</param>
		/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
		public static object EndExecuteScalar(IAsyncResult result)
		{
			if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteScalarDelegate1))
			{
				ExecuteScalarDelegate1 d = (ExecuteScalarDelegate1) ((AsyncResult)result).AsyncDelegate;
				return d.EndInvoke(result);
			}
			else if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteScalarDelegate2))
			{
				ExecuteScalarDelegate2 d = (ExecuteScalarDelegate2) ((AsyncResult)result).AsyncDelegate;
				return d.EndInvoke(result);
			}
			else if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteScalarDelegate3))
			{
				ExecuteScalarDelegate3 d = (ExecuteScalarDelegate3) ((AsyncResult)result).AsyncDelegate;
				return d.EndInvoke(result);
			}
			else if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteScalarDelegate4))
			{
				ExecuteScalarDelegate4 d = (ExecuteScalarDelegate4) ((AsyncResult)result).AsyncDelegate;
				return d.EndInvoke(result);
			}
			else if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteScalarDelegate5))
			{
				ExecuteScalarDelegate5 d = (ExecuteScalarDelegate5) ((AsyncResult)result).AsyncDelegate;
				return d.EndInvoke(result);
			}
			else if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteScalarDelegate6))
			{
				ExecuteScalarDelegate6 d = (ExecuteScalarDelegate6) ((AsyncResult)result).AsyncDelegate;
				return d.EndInvoke(result);
			}
			else if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteScalarDelegate7))
			{
				ExecuteScalarDelegate7 d = (ExecuteScalarDelegate7) ((AsyncResult)result).AsyncDelegate;
				return d.EndInvoke(result);
			}
			else if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteScalarDelegate8))
			{
				ExecuteScalarDelegate8 d = (ExecuteScalarDelegate8) ((AsyncResult)result).AsyncDelegate;
				return d.EndInvoke(result);
			}
			else //if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteScalarDelegate9))
			{
				ExecuteScalarDelegate9 d = (ExecuteScalarDelegate9) ((AsyncResult)result).AsyncDelegate;
				return d.EndInvoke(result);
			}
		}


		#endregion ExecuteScalar	
		
		#region ExecuteReader

		/// <summary>
		/// this enum is used to indicate whether the connection was provided by the caller, or created by SqlHelper, so that
		/// we can set the appropriate CommandBehavior when calling ExecuteReader()
		/// </summary>
		private enum SqlConnectionOwnership	
		{
			/// <summary>Connection is owned and managed by SqlHelper</summary>
			Internal, 
			/// <summary>Connection is owned and managed by the caller</summary>
			External
		}

		/// <summary>
		/// Create and prepare a command, and call ExecuteReader with the appropriate CommandBehavior.
		/// </summary>
		/// <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 T-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="connectionOwnership">indicates whether the connection parameter was provided by the caller, or created by SqlHelper</param>
		/// <returns>IDataReader containing the results of the command</returns>
		private static IDataReader ExecuteReader(IDbConnection connection, IDbTransaction transaction, CommandType commandType, string commandText, IDataParameter[] commandParameters, SqlConnectionOwnership connectionOwnership)
		{	
			//create a command and prepare it for execution
			SqlCommand cmd = new SqlCommand();
			//remember the state of the connection because PrepareCommand might change it
			ConnectionState connState = connection.State;
			//link all objects to the command
			PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters);
			
			//create a reader
			SqlDataReader dr;

			try
			{
				// call ExecuteReader with the appropriate CommandBehavior
				if ((connectionOwnership == SqlConnectionOwnership.External) &&
					(connState != ConnectionState.Closed))
				{
					dr = cmd.ExecuteReader();
				}
				else
				{
					dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
				}
			}
			catch (SqlException e)
			{
				//Handle data access exception condition
				LogException(e);
				//Wrap the current exception and re-throw the new exception
				throw e;
			}
			finally
			{
				//detach the IDataParameters from the command object, so they can be used again.
				cmd.Parameters.Clear();
			}
			
			return dr;
		}

		/// <summary>
		/// Create and prepare a command, and call ExecuteReader with the appropriate CommandBehavior.
		/// </summary>
		/// <param name="connectionString">a valid connection string for a connection yet to be opened</param>
		/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
		/// <param name="commandText">the stored procedure name or T-SQL command</param>
		/// <returns>a IDataReader containing the resultset generated by the command</returns>
		public static IDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText)
		{
			//pass through the call providing null for the set of IDataParameters
			return ExecuteReader(connectionString, commandType, commandText, (SqlParameter[])null);
		}

		/// <summary>
		/// Create and prepare a command, and call ExecuteReader with the appropriate CommandBehavior.
		/// </summary>
		/// <param name="connectionString">a valid connection string for a connection yet to be opened</param>
		/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
		/// <param name="commandText">the stored procedure name or T-SQL command</param>
		/// <param name="commandParameters">an array of IDataParameters used to execute the command</param>
		/// <returns>a IDataReader containing the resultset generated by the command</returns>
		public static IDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params IDataParameter[] commandParameters)
		{
			//create & open a IDbConnection
			SqlConnection cn = new SqlConnection(connectionString);
			cn.Open();
			try
			{
				//call the private overload that takes an internally owned connection in place of the connection string
				return ExecuteReader(cn, null, commandType, commandText, commandParameters,SqlConnectionOwnership.Internal);
			}
			catch
			{
				//if we fail to return the SqlDataReader, we need to close the connection ourselves
				cn.Close();
				throw;
			}
		}

		/// <summary>
		/// Execute a stored procedure via a command (that returns a resultset) against the database specified in 
		/// the connection string using the provided parameter values.  This method will query the database to discover the parameters for the 
		/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
		/// </summary>
		/// <param name="connectionString">a valid connection string for a connection yet to be opened</param>
		/// <param name="spName">the name of the stored procedure</param>
		/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
		/// <returns>a IDataReader containing the resultset generated by the command</returns>
		public static IDataReader ExecuteReader(string connectionString, string spName, params object[] parameterValues)
		{
			//if we receive parameter values, we need to figure out where they go
			if ((parameterValues != null) && (parameterValues.Length > 0)) 
			{
				//pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
				SqlParameter[] commandParameters = (SqlParameter[])SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);

				//assign the provided values to these parameters based on parameter order
				AssignParameterValues(commandParameters, parameterValues);

				//call the overload that takes an array of IDataParameters
				return ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters);
			}
				//otherwise we can just call the SP without params
			else 
			{
				return ExecuteReader(connectionString, CommandType.StoredProcedure, spName);
			}
		}

		/// <summary>
		/// Create and prepare a command, and call ExecuteReader with the appropriate CommandBehavior.
		/// </summary>
		/// <param name="connection">a valid IDbConnection</param>
		/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
		/// <param name="commandText">the stored procedure name or T-SQL command</param>
		/// <returns>a IDataReader containing the resultset generated by the command</returns>
		public static IDataReader ExecuteReader(IDbConnection connection, CommandType commandType, string commandText)
		{
			//pass through the call providing null for the set of IDataParameters
			return ExecuteReader(connection, commandType, commandText, (SqlParameter[])null);
		}

		/// <summary>
		/// Create and prepare a command, and call ExecuteReader with the appropriate CommandBehavior.
		/// </summary>
		/// <param name="connection">a valid IDbConnection</param>
		/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
		/// <param name="commandText">the stored procedure name or T-SQL command</param>
		/// <param name="commandParameters">an array of parameters used to execute the command</param>
		/

⌨️ 快捷键说明

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