All code after SqlDataReader.ExecuteReader skipped

时光怂恿深爱的人放手 提交于 2019-12-11 05:15:43

问题


I have the following function:

private void populate()
    {
        String connectionString = Properties.Settings.Default.Database;

        String selectString = "select artikelnummer, omschrijving from Artikels";

        SqlDataReader reader = null;

        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand command = new SqlCommand(selectString);

        reader = command.ExecuteReader();
        connection.Open();
        int x = 0;
        while (reader.Read())
        {
            String item = String.Empty;
            item = Convert.ToString(reader["Artikelnummer"]) + "\t" + Convert.ToString(reader["Omschrijving"]);
            x++;
            listboxGeselecteerd.Items.Add(item);
        }            
    }

Everything that follows after reader = command.ExecuteReader(); is skipped.

Is there anything I've done wrong?

UPDATE: Moved the connection.Open(); to the right spot. Now, when I reach that line, my output shows Step into: Stepping over non-user code 'System.Data.SqlClient.SqlConnection.Open

And then skips the rest of the function.


回答1:


My money is on a method higher up in the call stack eating the exception because this should've thrown one because the connection hadn't been opened. That's your biggest problem.

The next problem you have is that your connection is not associated with the SqlCommand so even if it were opened, it wouldn't matter.

Finally, connection.Open(); needs to be before ExecuteReader.

In addition to that, you really ought to be using using blocks.

{
    String connectionString = Properties.Settings.Default.Database;

    String selectString = "select artikelnummer, omschrijving from Artikels";

    SqlDataReader reader = null;

    using (SqlConnection connection = new SqlConnection(connectionString))
    /* you also need to associate the connection with the command */
    using (SqlCommand command = new SqlCommand(selectString, connection))
    {
        connection.Open();
        reader = command.ExecuteReader();
        int x = 0;
        while (reader.Read())
        {
            String item = String.Empty;
            item = Convert.ToString(reader["Artikelnummer"]) + "\t" + Convert.ToString(reader["Omschrijving"]);
            x++;
            listboxGeselecteerd.Items.Add(item);
        }            
    }
}

How about some simple "printf"-style debugging and posting the exception you get?

try
{
   connection.Open();
   ... 
}
//catch (Exception e)
catch (SqlException e)
{
    // at least one of these..
    Console.WriteLine(e);
    MessageBox.Show(e);
    Debug.WriteLine(e);

    var inner = e.InnerException;
    while (inner != null)
    {
         //display / log / view
         inner = inner.InnerException;
    }
}

Given the exception text from the comments (A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll) looks more like the message you'd get just before the real message showed up, I would catch a SqlException and examine the InnerException(s).




回答2:


I'm surprised it's not throwing an exception, but you need to open the connection before you execute your reader.




回答3:


You have to open the connection before you try to use it. Move connection.Open() above your command.ExecuteReader() call.




回答4:


I can only surmise that you need to open your connection before you execute the reader, and that it's being skipping because an exception is being thrown.




回答5:


You need to open the connection before you call ExecuteReader.

Also, you don't assign your connection to the SqlCommand. You need to do it like this:

 using(qlConnection connection = new SqlConnection(connectionString))
 {
  using(SqlCommand command = new SqlCommand(selectString,connection))
  {
     connection.Open();
     reader = command.ExecuteReader(); 

     // rest of your code.
   }
}



回答6:


private void populate(){
    String connectionString = Properties.Settings.Default.Database;
    String commandString = "SELECT artikelnummer, omschrijving FROM Artikels";
    using (SqlConnection cn = new SqlConnection(connectionString)){
        using (SqlCommand cm = new SqlCommand(commandString, cn)){
            cn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            int x = 0;
            while (dr.Read()){
                String item = String.Empty;
                item = Convert.ToString(dr["Artikelnummer"]) + "\t" + Convert.ToString(dr["Omschrijving"]);
                x++;
                listboxGeselecteerd.Items.Add(item);
            }
        }
    }
}

On a side note, what are you doing with the 'int x'? I see you are incrementing it, but not doing anything with it.



来源:https://stackoverflow.com/questions/7891809/all-code-after-sqldatareader-executereader-skipped

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!