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

📄 sqlhelper.cs

📁 比较好的异步数据库访问示例
💻 CS
📖 第 1 页 / 共 5 页
字号:
		/// <param name="commandParameters">an array of IDataParameters to be associated with the command or 'null' if no parameters are required</param>
		/// <returns>an int representing the number of rows affected by the command</returns>
		public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params IDataParameter[] commandParameters)
		{
			//create & open a connection, and dispose of it after we are done.
			using (SqlConnection cn = new SqlConnection(connectionString))
			{
				//call the overload that takes a connection in place of the connection string
				return ExecuteNonQuery(cn, commandType, commandText, commandParameters);
			}
		}

		/// <summary>
		/// Execute a stored procedure via a command (that returns no 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 prcedure</param>
		/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
		/// <returns>an int representing the number of rows affected by the command</returns>
		public static int ExecuteNonQuery(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 ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
			}
				//otherwise we can just call the SP without params
			else 
			{
				return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
			}
		}

		/// <summary>
		/// Execute a command that returns no resultset
		/// </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>an int representing the number of rows affected by the command</returns>
		public static int ExecuteNonQuery(IDbConnection connection, CommandType commandType, string commandText)
		{
			//pass through the call providing null for the set of IDataParameters
			return ExecuteNonQuery(connection, commandType, commandText, (SqlParameter[])null);
		}

		/// <summary>
		/// Execute a command that returns no resultset
		/// </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>
		/// <returns>an int representing the number of rows affected by the command</returns>
		public static int ExecuteNonQuery(IDbConnection connection, CommandType commandType, string commandText, params IDataParameter[] commandParameters)
		{	
			//pass through the call providing null for the transaction
			return ExecuteNonQuery(connection, (SqlTransaction)null, commandType, commandText, commandParameters);
		}

		/// <summary>
		/// Execute a stored procedure via a command (that returns no resultset) against the specified IDbConnection 
		/// 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="connection">a valid IDbConnection</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>an int representing the number of rows affected by the command</returns>
		public static int ExecuteNonQuery(IDbConnection connection, 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(connection.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 ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);
			}
				//otherwise we can just call the SP without params
			else 
			{
				return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);
			}
		}

		/// <summary>
		/// Execute a command that returns no resultset
		/// </summary>
		/// <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>
		/// <returns>an int representing the number of rows affected by the command</returns>
		public static int ExecuteNonQuery(IDbTransaction transaction, CommandType commandType, string commandText)
		{
			//pass through the call providing null for the set of IDataParameters
			return ExecuteNonQuery(transaction, commandType, commandText, (SqlParameter[])null);
		}

		/// <summary>
		/// Execute a command that returns no resultset
		/// </summary>
		/// <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="commandParameters">an array of IDataParameters used to execute the command</param>
		/// <returns>an int representing the number of rows affected by the command</returns>
		public static int ExecuteNonQuery(IDbTransaction transaction, CommandType commandType, string commandText, params IDataParameter[] commandParameters)
		{
			//pass through the call using the transaction's own connection
			return ExecuteNonQuery(transaction.Connection, transaction, commandType, commandText, commandParameters);
		}

		/// <summary>
		/// Execute a stored procedure via a SqlCommand (that returns no 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="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
		/// <returns>an int representing the number of rows affected by the command</returns>
		public static int ExecuteNonQuery(IDbTransaction transaction, 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(transaction.Connection.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 ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
			}
				//otherwise we can just call the SP without params
			else 
			{
				return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
			}
		}

		private delegate int ExecuteNonQueryDelegate1(string connectionString, CommandType commandType, string commandText);
		private delegate int ExecuteNonQueryDelegate2(string connectionString, CommandType commandType, string commandText, params IDataParameter[] commandParameters);
		private delegate int ExecuteNonQueryDelegate3(string connectionString, string spName, params object[] parameterValues);
		private delegate int ExecuteNonQueryDelegate4(IDbConnection connection, CommandType commandType, string commandText);
		private delegate int ExecuteNonQueryDelegate5(IDbConnection connection, CommandType commandType, string commandText, params IDataParameter[] commandParameters);
		private delegate int ExecuteNonQueryDelegate6(IDbConnection connection, string spName, params object[] parameterValues);
		private delegate int ExecuteNonQueryDelegate7(IDbTransaction transaction, CommandType commandType, string commandText);
		private delegate int ExecuteNonQueryDelegate8(IDbTransaction transaction, CommandType commandType, string commandText, params IDataParameter[] commandParameters);
		private delegate int ExecuteNonQueryDelegate9(IDbTransaction transaction, string spName, params object[] parameterValues);

		/// <summary>
		/// Execute a command that returns no resultset
		/// </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="ac">callback object containing a delegate</param>
		/// <param name="state">state object supplied by the client</param>
		/// <returns>asynchronous result object</returns>
		public static IAsyncResult BeginExecuteNonQuery
			(string connectionString, CommandType commandType, string commandText, AsyncCallback ac, Object state)
		{
			ExecuteNonQueryDelegate1 d = new ExecuteNonQueryDelegate1(ExecuteNonQuery);
			IAsyncResult result = d.BeginInvoke(connectionString, commandType, commandText, ac, state);
			return result;
		}

		/// <summary>
		/// Execute a command that returns no resultset
		/// </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="ac">callback object containing a delegate</param>
		/// <param name="state">state object supplied by the client</param>
		/// <param name="commandParameters">an array of IDataParameters to be associated with the command or 'null' if no parameters are required</param>
		/// <returns>asynchronous result object</returns>
		public static IAsyncResult BeginExecuteNonQuery
			(string connectionString, CommandType commandType, string commandText, AsyncCallback ac, Object state, params IDataParameter[] commandParameters)
		{
			ExecuteNonQueryDelegate2 d = new ExecuteNonQueryDelegate2(ExecuteNonQuery);
			IAsyncResult result = d.BeginInvoke(connectionString, commandType, commandText, commandParameters, ac, state);
			return result;
		}

		/// <summary>
		/// Execute a stored procedure via a command (that returns no 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 prcedure</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 BeginExecuteNonQuery
			(string connectionString, string spName, AsyncCallback ac, Object state, params object[] parameterValues)
		{
			ExecuteNonQueryDelegate3 d = new ExecuteNonQueryDelegate3(ExecuteNonQuery);
			IAsyncResult result = d.BeginInvoke(connectionString, spName, parameterValues, ac, state);
			return result;
		}

		/// <summary>
		/// Execute a command that returns no resultset
		/// </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="ac">callback object containing a delegate</param>
		/// <param name="state">state object supplied by the client</param>
		/// <returns>asynchronous result object</returns>
		public static IAsyncResult BeginExecuteNonQuery
			(IDbConnection connection, CommandType commandType, string commandText, AsyncCallback ac, Object state)
		{
			ExecuteNonQueryDelegate4 d = new ExecuteNonQueryDelegate4(ExecuteNonQuery);
			IAsyncResult result = d.BeginInvoke(connection, commandType, commandText, ac, state);
			return result;
		}

		/// <summary>
		/// Execute a command that returns no resultset
		/// </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="ac">callback object containing a delegate</param>
		/// <param name="state">state object supplied by the client</param>
		/// <param name="commandParameters">an array of parameters used to execute the command</param>

⌨️ 快捷键说明

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