Friday, January 4, 2013

How may I route the subject of my using statement into another method and back midway through the using statement?

You cannot do this!

private static DataSet PopulateDataSetFromStoredProcedure(string connectionString)
{
   DataSet dataSet = new DataSet();
   using (SqlConnection connection = new SqlConnection(connectionString))
   {
      using (SqlCommand command = new SqlCommand())
      {
         command.CommandText = "dbo.Whatever";
         command.CommandType = CommandType.StoredProcedure;
         command = AddParametersToSqlCommand(command);
         
command.Connection = connection;
         using (SqlDataAdapter adapter = new SqlDataAdapter())
         {
            adapter.SelectCommand = command;
            adapter.Fill(dataSet);
         }
      }
   }
   return dataSet;
}
   
private static SqlCommand AddParametersToSqlCommand(SqlCommand sqlCommand)
{
   sqlCommand.Parameters.AddWithValue("@Foo", "my");
   sqlCommand.Parameters.AddWithValue("@Bar", "dog");
   sqlCommand.Parameters.AddWithValue("@Baz", "has");
   sqlCommand.Parameters.AddWithValue("@Qux", "fleas");
   return sqlCommand;
}

 
 

The variable in black above will cause this error:

Readonly variable cannot be used as an assignment target

 
 

This is one way you might try to hack around the problem, but this won't work either.

private static DataSet PopulateDataSetFromStoredProcedure(string connectionString)
{
   DataSet dataSet = new DataSet();
   using (SqlConnection connection = new SqlConnection(connectionString))
   {
      using (SqlCommand command = new SqlCommand())
      {
         command.CommandText = "dbo.Whatever";
         command.CommandType = CommandType.StoredProcedure;
         bool refHackWorks = AddParametersToSqlCommand(
command);
         command.Connection = connection;
         using (SqlDataAdapter adapter = new SqlDataAdapter())
         {
            adapter.SelectCommand = command;
            adapter.Fill(dataSet);
         }
      }
   }
   return dataSet;
}
   
private static bool AddParametersToSqlCommand(ref SqlCommand sqlCommand)
{
   sqlCommand.Parameters.AddWithValue("@Foo", "my");
   sqlCommand.Parameters.AddWithValue("@Bar", "dog");
   sqlCommand.Parameters.AddWithValue("@Baz", "has");
   sqlCommand.Parameters.AddWithValue("@Qux", "fleas");
   return true;
}

 
 

The variable in black above will cause this error:

Arguement is 'value' while parameter is declared as 'ref'

 
 

The soultion is to create a new using "loop" inside of the existing one like so:

private static DataSet PopulateDataSetFromStoredProcedure(string connectionString)
{
   DataSet dataSet = new DataSet();
   using (SqlConnection connection = new SqlConnection(connectionString))
   {
      using (SqlCommand dumbCommand = new SqlCommand())
      {
         dumbCommand.CommandText = "dbo.Whatever";
         dumbCommand.CommandType = CommandType.StoredProcedure;
         using (SqlCommand parameterizedCommand =
               AddParametersToSqlCommand(dumbCommand))
         {
            parameterizedCommand.Connection = connection;
            using (SqlDataAdapter adapter = new SqlDataAdapter())
            {
               adapter.SelectCommand = parameterizedCommand;
               adapter.Fill(dataSet);
            }
         }
      }
   }
   return dataSet;
}
   
private static SqlCommand AddParametersToSqlCommand(SqlCommand sqlCommand)
{
   sqlCommand.Parameters.AddWithValue("@Foo", "my");
   sqlCommand.Parameters.AddWithValue("@Bar", "dog");
   sqlCommand.Parameters.AddWithValue("@Baz", "has");
   sqlCommand.Parameters.AddWithValue("@Qux", "fleas");
   return sqlCommand;
}

No comments:

Post a Comment