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

📄 sqlhelper.cs

📁 SharpNuke源代码
💻 CS
📖 第 1 页 / 共 5 页
字号:
					}
				}
				
				if (canClear)
				{
					cmd.Parameters.Clear();
				}
				
				return dataReader;
			}
			catch
			{
				if (mustCloseConnection)
				{
					connection.Close();
				}
				throw;
			}
		} // ExecuteReader
		
		// Execute a SqlCommand (that returns a resultset and takes no parameters) against the database specified in
		// the connection string.
		// e.g.:
		// Dim dr As SqlDataReader = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders")
		// Parameters:
		// -connectionString - a valid connection string for a SqlConnection
		// -commandType - the CommandType (stored procedure, text, etc.)
		// -commandText - the stored procedure name or T-SQL command
		// Returns: A SqlDataReader containing the resultset generated by the command
		public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText)
		{
			// Pass through the call providing null for the set of SqlParameters
			return ExecuteReader(connectionString, commandType, commandText,((SqlParameter[]) null));
		} // ExecuteReader
		
		// Execute a SqlCommand (that returns a resultset) against the database specified in the connection string
		// using the provided parameters.
		// e.g.:
		// Dim dr As SqlDataReader = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24))
		// Parameters:
		// -connectionString - a valid connection string for a SqlConnection
		// -commandType - the CommandType (stored procedure, text, etc.)
		// -commandText - the stored procedure name or T-SQL command
		// -commandParameters - an array of SqlParamters used to execute the command
		// Returns: A SqlDataReader containing the resultset generated by the command
		public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
		{
			if (connectionString == null || connectionString.Length == 0)
			{
				throw (new ArgumentNullException("connectionString"));
			}
			
			// Create & open a SqlConnection
			SqlConnection connection = null;
			try
			{
				connection = new SqlConnection(connectionString);
				connection.Open();
				// Call the private overload that takes an internally owned connection in place of the connection string
				return ExecuteReader(connection,((SqlTransaction) null), commandType, commandText, commandParameters, SqlConnectionOwnership.Internal);
			}
			catch
			{
				// If we fail to return the SqlDatReader, we need to close the connection ourselves
				if (connection != null)
				{
					connection.Dispose();
				}
				throw;
			}
		} // ExecuteReader
		
		// Execute a stored procedure via a SqlCommand (that returns a resultset) against the database specified in
		// the connection string using the provided parameter values.  This method will discover the parameters for the
		// stored procedure, and assign the values based on parameter order.
		// This method provides no access to output parameters or the stored procedure' s return value parameter.
		// e.g.:
		// Dim dr As SqlDataReader = ExecuteReader(connString, "GetOrders", 24, 36)
		// Parameters:
		// -connectionString - a valid connection string for a SqlConnection
		// -spName - the name of the stored procedure
		// -parameterValues - an array of objects to be assigned as the input values of the stored procedure
		// Returns: A SqlDataReader containing the resultset generated by the command
		public static SqlDataReader ExecuteReader(string connectionString, string spName, params object[] parameterValues)
		{
			if (connectionString == null || connectionString.Length == 0)
			{
				throw (new ArgumentNullException("connectionString"));
			}
			if (spName == null || spName.Length == 0)
			{
				throw (new ArgumentNullException("spName"));
			}
			
			SqlParameter[] commandParameters;
			
			// 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)
				commandParameters = 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 SqlParameters
				return ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters);
				// Otherwise we can just call the SP without params
			}
			else
			{
				return ExecuteReader(connectionString, CommandType.StoredProcedure, spName);
			}
		} // ExecuteReader
		
		// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlConnection.
		// e.g.:
		// Dim dr As SqlDataReader = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders")
		// Parameters:
		// -connection - a valid SqlConnection
		// -commandType - the CommandType (stored procedure, text, etc.)
		// -commandText - the stored procedure name or T-SQL command
		// Returns: A SqlDataReader containing the resultset generated by the command
		public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText)
		{
			return ExecuteReader(connection, commandType, commandText,((SqlParameter[]) null));
			
		} // ExecuteReader
		
		// Execute a SqlCommand (that returns a resultset) against the specified SqlConnection
		// using the provided parameters.
		// e.g.:
		// Dim dr As SqlDataReader = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24))
		// Parameters:
		// -connection - a valid SqlConnection
		// -commandType - the CommandType (stored procedure, text, etc.)
		// -commandText - the stored procedure name or T-SQL command
		// -commandParameters - an array of SqlParamters used to execute the command
		// Returns: A SqlDataReader containing the resultset generated by the command
		public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
		{
			// Pass through the call to private overload using a null transaction value
			return ExecuteReader(connection,((SqlTransaction) null), commandType, commandText, commandParameters, SqlConnectionOwnership.External);
			
		} // ExecuteReader
		
		// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlConnection
		// using the provided parameter values.  This method will discover the parameters for the
		// stored procedure, and assign the values based on parameter order.
		// This method provides no access to output parameters or the stored procedure' s return value parameter.
		// e.g.:
		// Dim dr As SqlDataReader = ExecuteReader(conn, "GetOrders", 24, 36)
		// Parameters:
		// -connection - a valid SqlConnection
		// -spName - the name of the stored procedure
		// -parameterValues - an array of objects to be assigned as the input values of the stored procedure
		// Returns: A SqlDataReader containing the resultset generated by the command
		public static SqlDataReader ExecuteReader(SqlConnection connection, string spName, params object[] parameterValues)
		{
			if (connection == null)
			{
				throw (new ArgumentNullException("connection"));
			}
			if (spName == null || spName.Length == 0)
			{
				throw (new ArgumentNullException("spName"));
			}
			
			SqlParameter[] commandParameters;
			// If we receive parameter values, we need to figure out where they go
			if (parameterValues != null && parameterValues.Length > 0)
			{
				commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
				
				AssignParameterValues(commandParameters, parameterValues);
				
				return ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters);
			}
			else // Otherwise we can just call the SP without params
			{
				return ExecuteReader(connection, CommandType.StoredProcedure, spName);
			}
			
		} // ExecuteReader
		
		// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlTransaction.
		// e.g.:
		// Dim dr As SqlDataReader = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders")
		// Parameters:
		// -transaction - a valid SqlTransaction
		// -commandType - the CommandType (stored procedure, text, etc.)
		// -commandText - the stored procedure name or T-SQL command
		// Returns: A SqlDataReader containing the resultset generated by the command
		public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText)
		{
			// Pass through the call providing null for the set of SqlParameters
			return ExecuteReader(transaction, commandType, commandText,((SqlParameter[]) null));
		} // ExecuteReader
		
		// Execute a SqlCommand (that returns a resultset) against the specified SqlTransaction
		// using the provided parameters.
		// e.g.:
		// Dim dr As SqlDataReader = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24))
		// Parameters:
		// -transaction - a valid SqlTransaction
		// -commandType - the CommandType (stored procedure, text, etc.)
		// -commandText - the stored procedure name or T-SQL command
		// -commandParameters - an array of SqlParamters used to execute the command
		// Returns: A SqlDataReader containing the resultset generated by the command
		public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
		{
			if (transaction == null)
			{
				throw (new ArgumentNullException("transaction"));
			}
			if (transaction != null && transaction.Connection == null)
			{
				throw (new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"));
			}
			// Pass through to private overload, indicating that the connection is owned by the caller
			return ExecuteReader(transaction.Connection, transaction, commandType, commandText, commandParameters, SqlConnectionOwnership.External);
		} // ExecuteReader
		
		// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlTransaction
		// using the provided parameter values.  This method will discover the parameters for the
		// stored procedure, and assign the values based on parameter order.
		// This method provides no access to output parameters or the stored procedure' s return value parameter.
		// e.g.:
		// Dim dr As SqlDataReader = ExecuteReader(trans, "GetOrders", 24, 36)
		// Parameters:
		// -transaction - a valid SqlTransaction
		// -spName - the name of the stored procedure
		// -parameterValues - an array of objects to be assigned as the input values of the stored procedure
		// Returns: A SqlDataReader containing the resultset generated by the command
		public static SqlDataReader ExecuteReader(SqlTransaction transaction, string spName, params object[] parameterValues)
		{
			if (transaction == null)
			{
				throw (new ArgumentNullException("transaction"));
			}
			if (transaction != null && transaction.Connection == null)
			{
				throw (new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"));
			}
			if (spName == null || spName.Length == 0)
			{
				throw (new ArgumentNullException("spName"));
			}
			
			SqlParameter[] commandParameters;
			
			// If we receive parameter values, we need to figure out where they go
			if (parameterValues != null && parameterValues.Length > 0)
			{
				commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
				
				AssignParameterValues(commandParameters, parameterValues);
				
				return ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters);
			}
			else // Otherwise we can just call the SP without params
			{
				return ExecuteReader(transaction, CommandType.StoredProcedure, spName);
			}
		} // ExecuteReader
		
		#endregion
		
		#region "ExecuteScalar"
		
		// Execute a SqlCommand (that returns a 1x1 resultset and takes no parameters) against the database specified in
		// the connection string.
		// e.g.:
		// Dim orderCount As Integer = CInt(ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount"))
		// Parameters:
		// -connectionString - a valid connection string for a SqlConnection
		// -commandType - the CommandType (stored procedure, text, etc.)
		// -commandText - the stored procedure name or T-SQL command
		// Returns: An object containing the value in the 1x1 resultset generated by the command
		public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText)
		{
			// Pass through the call providing null for the set of SqlParameters
			return ExecuteScalar(connectionString, commandType, commandText,((SqlParameter[]) null));
		} // ExecuteScalar
		
		// Execute a SqlCommand (that returns a 1x1 resultset) against the database specified in the connection string
		// using the provided parameters.
		// e.g.:
		// Dim orderCount As Integer = Cint(ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24)))
		// Parameters:
		// -connectionString - a valid connection string for a SqlConnection
		// -commandType - the CommandType (stored procedure, text, etc.)
		// -commandText - the stored procedure name or T-SQL command

⌨️ 快捷键说明

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