2011-10-25 38 views
1

我有以下功能:SqlDataReader.ExecuteReader后,所有代码跳过

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); 
     }    
    } 

reader = command.ExecuteReader();被跳过后,下面的一切。

有什么我做错了吗?

更新:将connection.Open();移到了正确的位置。现在,当我到达该线时,我的输出显示为Step into: Stepping over non-user code 'System.Data.SqlClient.SqlConnection.Open

然后跳过该函数的其余部分。

回答

1

我的钱在调用堆栈中吃掉异常的方法更高,因为它应该抛出一个,因为连接没有打开。这是你最大的问题。

你的下一个问题是你的连接没有与SqlCommand关联,所以即使打开它也没关系。

最后,connection.Open();需要在ExecuteReader之前。

除此之外,你真的应该使用using块。

{ 
    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); 
     }    
    } 
} 

对于一些简单的“printf”式调试和发布异常,你会得到什么?

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; 
    } 
} 

鉴于从评论的异常文本(A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll)看起来更像消息你会得到的只是真实的信息才露面,我会赶上一个SQLEXCEPTION并检查的InnerException(S)。

+0

试过这个,它仍然在'connection.Open();'之后跳过所有东西。没有例外被调用。输出确实说'进入:跨越非用户代码'系统。Data.SqlClient.SqlConnection.Open'' –

+0

@SimonVerbeke:必须有一个异常_somewhere _... –

+0

确实.. System.Data.dll中出现'System.Data.SqlClient.SqlException'类型的第一个机会异常。我怎么知道究竟出了什么问题? –

1

我很惊讶它没有抛出异常,但是在执行读者之前需要打开连接。

0

您必须在尝试使用它之前打开连接。在你的command.ExecuteReader()调用之上移动connection.Open()。

0

您需要在调用ExecuteReader之前打开连接。

另外,您不要将连接分配给SqlCommand。你需要做的是这样的:

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

    // rest of your code. 
    } 
} 
+0

SqlConnection&SqlCommand是一次性的。有些人在这里认为不要把它们用于封锁值得冒犯的错误。 –

+0

@AustinSalonen我同意。我只是在指出他的错误。我想我是第一个真正发现他没有为他的'SqlCommand'分配连接的人。我已经编辑了我的答案,包括在连接和命令周围使用语句。 – Icarus

0

我只能猜测,您需要在执行在读者面前打开你的连接,而且它被跳过,因为一个异常被抛出。

0
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); 
      } 
     } 
    } 
} 

在旁注中,你在做什么'int x'?我看到你正在增加它,但没有做任何事情。

+1

http://davidhayden.com/blog/dave/archive/2005/01/13/773.aspx对理解'使用'语句是一个有趣的阅读。如果你想明白为什么。 – JClaspill

+0

'int x'是我用于数组的东西的余数。一定忘记删除它。 –