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

后端 未结 12 2299
情话喂你
情话喂你 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:02

    How about pulling the data into a DataSet via Fill and then iterate through that to perform your insert via NonQuery?

    IDbDataAdapter da;
    IDbCommand selectCommand = connection.CreateCommand();
    selectCommand.CommandType = CommandType.Text;
    selectCommand.CommandText = "SELECT field1, field2 FROM sourcetable";
    connection.Open();
    DataSet selectResults= new DataSet();
    da.Fill(selectResults); // get dataset
    selectCommand.Dispose();
    IDbCommand insertCommand;
    
    foreach(DataRow row in selectResults.Tables[0].Rows)
    {
        insertCommand = connection.CreateCommand();
        insertCommand.CommandType = CommandType.Text;
        insertCommand.CommandText = "INSERT INTO tablename (field1, field2) VALUES (3, '" + row["columnName"].ToString() + "'";   
    }
    insertCommand.Dispose();
    connection.Close();
    

提交回复
热议问题