2

I have a program that has to loop through a Scenarios database, and for each while loop iteration, update a second Results database. Here's the segment of code.

    public void TestScenarios(SqlConnection connection)
    {
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM Scenarios", connection))
        {
            SqlDataReader reader = cmd.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    string Id = reader["ScenarioID"].ToString();
                    string Data = reader["ScenarioData"].ToString();
                    string Url = reader["ScenarioURL"].ToString();
                    webBrowser1.Navigate(Url);
                    InsertResults(connection, Id);
                }
            }

            reader.Close();
        }
    }

    public void InsertResults(SqlConnection conn, string Id)
    {
        using (SqlCommand cmd = new SqlCommand("INSERT INTO Results VALUES(" +
                "@ResultID, @HasSucceeded, @ScenarioID, @Screenshot)", conn))
        {
            cmd.Parameters.AddWithValue("@ResultID", 0);
            cmd.Parameters.AddWithValue("@HasSucceeded", 0);
            cmd.Parameters.AddWithValue("@ScenarioID", Id);
            cmd.Parameters.AddWithValue("@Screenshot", "screenshot.jpeg");
        }
    }

It's not working, and I'm sure I did tons of things wrong, but I'm having trouble finding direction.

4
  • 1
    What, per se, isn't working? Commented Jul 18, 2013 at 21:01
  • 4
    In your InsertResults function, you probably need to do something like cmd.ExecuteNonQuery();. Commented Jul 18, 2013 at 21:02
  • 3
    As a side note, are you sure you want to call webBrowser1.Navigate in a while loop? I imagine the speed of the while loop will be significantly faster than the Navigate method. Commented Jul 18, 2013 at 21:03
  • Good point, I'll change that. Commented Jul 18, 2013 at 21:05

2 Answers 2

5

Your second command is never executing. Add:

cmd.ExecuteNonQuery();
Sign up to request clarification or add additional context in comments.

2 Comments

I get "There is already an open DataReader associated with this Command which must be closed first."
Yes, that's because you are trying to re use the connection while you are iterating over the rows. If you want to perform the insert while keeping your other connection open, you'll have to create a new connection.
2

May I propose you to use SqlBulkCopy? Here is some information on how to copy two database tables , I hope you'll find it clear.

http://www.codeproject.com/Articles/18418/Transferring-Data-Using-SqlBulkCopy

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy%28v=vs.90%29.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.