CarrotCMSData/LinqToSqlExtensions.cs
/// <summary>
/// Combines multiple SELECT commands into a single SqlCommand so that all statements can be executed in a 
/// single roundtrip to the database and return multiple result sets.
/// </summary>
/// <param name="commandList">Represents a collection of commands to be batched together.</param>
/// <returns>Returns a single SqlCommand that executes all SELECT statements at once.</returns>
Combines multiple SELECT commands into a single SqlCommand so that all statements can be executed in a
single roundtrip to the database and return multiple result sets.
Combines multiple SELECT commands into a single SqlCommand so that all statements can be executed in a single roundtrip to the database and return multiple result sets .
private static SqlCommand CombineCommands( List<DbCommand> selectCommands )
		{
			SqlCommand batchCommand = new SqlCommand();
			SqlParameterCollection newParamList = batchCommand.Parameters;
			int commandCount = 0;
			foreach ( DbCommand cmd in selectCommands )
			{
				string commandText = cmd.CommandText;
				DbParameterCollection paramList = cmd.Parameters;
				int paramCount = paramList.Count;
				for ( int currentParam = paramCount - 1; currentParam >= 0; currentParam-- )
				{
					DbParameter param = paramList[ currentParam ];
					DbParameter newParam = CloneParameter( param );
					string newParamName = param.ParameterName.Replace( "@", string.Format( "@{0}_", commandCount ) );
					commandText = commandText.Replace( param.ParameterName, newParamName );
					newParam.ParameterName = newParamName;
					newParamList.Add( newParam );
				}
				if ( batchCommand.CommandText.Length > 0 )
				{
					batchCommand.CommandText += ";";
				}
				batchCommand.CommandText += commandText;
				commandCount++;
			}
			return batchCommand;
		}
private static SqlCommand CombineCommands ( List < DbCommand > selectCommands ) { SqlCommand batchCommand = new SqlCommand ( ) ; SqlParameterCollection newParamList = batchCommand . Parameters ; int commandCount = 0 ; foreach ( DbCommand cmd in selectCommands ) { string commandText = cmd . CommandText ; DbParameterCollection paramList = cmd . Parameters ; int paramCount = paramList . Count ; for ( int currentParam = paramCount - 1 ; currentParam >= 0 ; currentParam -- ) { DbParameter param = paramList [ currentParam ] ; DbParameter newParam = CloneParameter ( param ) ; string newParamName = param . ParameterName . Replace ( " " , string . Format ( " " , commandCount ) ) ; commandText = commandText . Replace ( param . ParameterName , newParamName ) ; newParam . ParameterName = newParamName ; newParamList . Add ( newParam ) ; } if ( batchCommand . CommandText . Length > 0 ) { batchCommand . CommandText += " " ; } batchCommand . CommandText += commandText ; commandCount ++ ; } return batchCommand ; }
Combines multiple SELECT commands into a single SqlCommand so that all statements can be executed in a
single roundtrip to the database and return multiple result sets.
Combines multiple SELECT commands into a single SqlCommand so that all statements can be executed in a single roundtrip to the database and return multiple result sets .
Returns a single SqlCommand that executes all SELECT statements at once.
Returns a single SqlCommand that executes all SELECT statements at once .
selectCommands
List<DbCommand>
commandList
Represents a collection of commands to be batched together.
Represents a collection of commands to be batched together .