SqlDataReader and SqlCommand

前端 未结 3 1061
感动是毒
感动是毒 2021-01-14 20:21

I have the following code.

using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[\"ConnectionString\"].ConnectionString)         


        
相关标签:
3条回答
  • 2021-01-14 20:58

    You need to either create multiple instances of you connection.
    As only one command can be excuted against a connection in general
    or

    do as suggested by @grantThomas
    Or you can use multiple connection as follows

    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
    {
       connection.Open();
       SqlCommand select = new SqlCommand("SELECT RTRIM(LTRIM(PART_NO)) AS PART_NO, record FROM [RMAData].[dbo].[IMPORTING_ORDER_EDI] WHERE sessionID = '" + Session.SessionID + "'", connection);
    
       SqlDataReader reader = select.ExecuteReader();
    
       if (reader.HasRows)
       {
          while (reader.Read())
          {
             if (!currentPart.IsActive)
             {
                // this part is not active, set the active flag in sql to 0
                using (SqlConnection connection1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
               {
                   SqlCommand update = new SqlCommand("UPDATE [RMAData].[dbo].[IMPORTING_ORDER_EDI] SET valid = 0, active = 0 WHERE record = " + reader["record"].ToString() + ";", connection1);
    
    
                update.ExecuteNonQuery();
               }
             }
             else
             {
                ///blah
             }
          }
    
          reader.Close();
       }
    }
    
    0 讨论(0)
  • 2021-01-14 20:59

    An alternative is not add MultipleActiveResultSets=True - there is a small performance penalty for doing so - and so something like this:

    using (SqlConnection connection = new ...))
    {
       connection.Open();
       SqlCommand select = new SqlCommand(...);
    
       SqlDataReader reader = select.ExecuteReader();
    
       var toInactivate = new List<string>();
    
       if (reader.HasRows)
       {
          while (reader.Read())
          {
             if (!currentPart.IsActive)
             {
                toInactivate.Add(reader["record"].ToString());
             }
             else
             {
                ///blah
             }
          }
    
          reader.Close();
       }
    
       SqlCommand update = new SqlCommand("UPDATE ... SET valid = 0, active = 0 " +
           "WHERE record IN(" + string.Join(",", toInactivate) +  ");", connection);
    
       update.ExecuteNonQuery();
    }
    

    which has the advantage of updating all the required records in a single SQL statement.

    And of course the whole thing would be so much neater using EF and Linq.

    0 讨论(0)
  • 2021-01-14 21:10

    Could be as simple as amending your connection string:

    add MultipleActiveResultSets=True to connection string

    0 讨论(0)
提交回复
热议问题