error 'there is already an open datareader associated with this command which must be closed first'

后端 未结 12 2198
情话喂你
情话喂你 2020-12-05 10:15

runtime error \'there is already an open datareader associated with this command which must be closed first\'

objCommand = new SqlCommand(\"SELECT field1, fi         


        
相关标签:
12条回答
  • 2020-12-05 10:49

    Option 1: Must execute query and load data before running another query.

    Option 2: Add MultipleActiveResultSets=true to the provider part of your connection string. See the example below:

    <add name="DbContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=dbName;Persist Security Info=True;User ID=userName;Password=password;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
    
    0 讨论(0)
  • 2020-12-05 10:49

    Best Solution: There is only problem with your "CommandText" value. Let it be SP or normal Sql Query.

    • Check 1: The parameter value which you are passing in your Sql Query is not changing and going same again and again in your ExecuteReader.

    • Check 2: Sql Query string is wrongly formed.

    • Check 3: Please create simplest code as follows.

      string ID = "C8CA7EE2";
      string myQuery = "select * from ContactBase where contactid=" + "'" + ID + "'";
      string connectionString = ConfigurationManager.ConnectionStrings["CRM_SQL_CONN_UAT"].ToString(); 
      SqlConnection con = new SqlConnection(connectionString);
      con.Open();
      SqlCommand cmd = new SqlCommand(myQuery, con);
      DataTable dt = new DataTable();
      dt.Load(cmd.ExecuteReader());
      con.Close();
      
    0 讨论(0)
  • 2020-12-05 10:51

    Try something like this:

    //Add a second connection based on the first one
    SqlConnection objConn2= new SqlConnection(objConn.connectionString))
    
    SqlCommand objInsertCommand= new SqlCommand();
    objInsertCommand.CommandType = CommandType.Text;
    objInsertCommand.Connection = objConn2;
    
    while (objDataReader.Read())
    {
        objInsertCommand.CommandText = "INSERT INTO tablename (field1, field2) VALUES (3, '" + objDataReader[0] + "')";
        objInsertCommand.ExecuteNonQuery();
    }
    
    0 讨论(0)
  • 2020-12-05 10:52

    No need to do all that, just turn on MARS and your problem will get solved. In your connection string just add MultipleActiveResultSets=True;

    0 讨论(0)
  • 2020-12-05 10:55

    You can't perform an action on that connection while it's still working on reading the contents of a data reader - the error is pretty descriptive.

    Your alternatives are:

    1) Retrieve all your data first, either with a DataSet or use the reader to populate some other collection, then run them all at once after the initial select is done.

    2) Use a different connection for your insert statements.

    0 讨论(0)
  • 2020-12-05 11:00

    In order for it to be disposed easily i use the following coding-template :

    `using (SqlConnection connection = new SqlConnection("your connection string"))
            {
                connection.Open();
                using (SqlCommand cmd = connection.CreateCommand())
                {
                    cmd.CommandText = "Select * from SomeTable";
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
    
                        if(reader.HasRows)
                        { 
                           while(reader.Read()){
                           // assuming that we've a 1-column(Id) table 
                           int id = int.Parse(reader[0].ToString()); 
    
                           }
                        }
                    }
                } 
                connection.Close()
            }`
    
    0 讨论(0)
提交回复
热议问题