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

后端 未结 12 2266
情话喂你
情话喂你 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 11:07

    Your best bet would be to read the information you need into a list and then iterating the list to perform your inserts like so:

            List values = new List();
            using(SqlCommand objCommand = new SqlCommand("SELECT field1, field2 FROM sourcetable", objConn)) {
                using(SqlDataReader objDataReader = objCommand.ExecuteReader()) {
                    while(objDataReader.Read()) {
                        values.Add(objDataReader[0].ToString());
                    }
                }
            }
            foreach(String value in values) {
                using(SqlCommand objInsertCommand = new SqlCommand("INSERT INTO tablename (field1, field2) VALUES (3, '" + value + "')", objConn)) {
                    objInsertCommand.ExecuteNonQuery();
                }
            }
    

提交回复
热议问题