2012-06-25 178 views
0

指望我有8個選項卡中的每一個函數包含的記錄數應該算在每個選項卡中的記錄數,並把它放在選項卡的標題名稱類似如下:記錄從數據庫

public void count_records(object sender, EventArgs e) 
    { 
     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Test"].ConnectionString); 
     string[] commands = { 
           "SELECT * FROM myTable", 
           "SELECT * FROM myTable WHERE Status=2", 
           "SELECT * FROM myTable WHERE Status=3", 
           "SELECT * FROM myTable WHERE Status=8", 
           "SELECT * FROM myTable WHERE Status=4", 
           "SELECT * FROM myTable WHERE Status=7", 
           "SELECT * FROM myTable WHERE Status=1", 
           "SELECT * FROM myTable WHERE Status=5" 
          }; 
     int[] LLCount = new int[commands.Length]; 
     try 
     { 
      for (int i = 0; i < commands.Length; i++) 
      { 
       SqlCommand cmd = new SqlCommand(commands[i], con); 
       SqlDataReader reader; 
       int count = 0; 
       con.Open(); 
       reader = cmd.ExecuteReader(); 
       while (reader.Read()) 
       { 
        count++; 
       } 
       LLCount[i] = count; 
       myTab1.HeaderText += " (" + LLCount[0] + ")"; 
       myTab2.HeaderText += " (" + LLCount[1] + ")"; 
       myTab3.HeaderText += " (" + LLCount[2] + ")"; 
       myTab4.HeaderText += " (" + LLCount[3] + ")"; 
       myTab5.HeaderText += " (" + LLCount[4] + ")"; 
       myTab6.HeaderText += " (" + LLCount[5] + ")"; 
       myTab7.HeaderText += " (" + LLCount[6] + ")"; 
       myTab8.HeaderText += " (" + LLCount[7] + ")"; 
      } 
     } 
     catch (Exception ex) { string ee = ex.Message; } 
     finally { con.Close(); } 
    } 

現在,我面臨的問題是,讀者正確獲取第一個命令字符串的記錄數「LLCount [0]」但其餘的都是零。

編輯:

我添加reader.Close();並將任務移到循環外部,但它甚至沒有顯示零或任何以前的任何東西。所以,我認爲 不管你把reader.Close();或不。

+0

使用reader.Close();在while循環之後。當您在關閉先前的閱讀器之前嘗試第二次打開閱讀器時,我認爲會引發異常並抓住catch catch。建議 - 在for循環之外分配標籤的HeaderText屬性。您可以使用block來打開閱讀器。 –

+0

我加了reader.Close();並將任務移到循環外部,但它甚至沒有像以前那樣顯示零或任何東西。所以我認爲它不關你是否把reader.Close();或不。 – Kemoid

+0

爲什麼你在查詢中沒有使用Count(*)。 –

回答

1

尋找// *在代碼中找出差異。主要的原因是我把con.Open();放在for loop之前。當連接未關閉時,再次打開相同的連接會引發錯誤。漸漸陷入catch塊這個錯誤,

public void count_records(object sender, EventArgs e) 
{ 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Test"].ConnectionString); 
    //*** See Count(*) in the sql 
    string[] commands = { 
         "SELECT count(*) FROM myTable", 
         "SELECT count(*) FROM myTable WHERE Status=2", 
         "SELECT count(*) FROM myTable WHERE Status=3", 
         "SELECT count(*) FROM myTable WHERE Status=8", 
         "SELECT count(*) FROM myTable WHERE Status=4", 
         "SELECT count(*) FROM myTable WHERE Status=7", 
         "SELECT count(*) FROM myTable WHERE Status=1", 
         "SELECT count(*) FROM myTable WHERE Status=5" 

        }; 
    int[] LLCount = new int[commands.Length]; 
    try 
    { 
     //*****This is the change I made 
     con.Open(); 

     for (int i = 0; i < commands.Length; i++) 
     { 
      SqlCommand cmd = new SqlCommand(commands[i], con); 

      int count = 0; 
      //*** Se the use of ExecuteScalar 
      count =Convert.ToInt32( cmd.ExecuteScalar()); 
      LLCount[i] = count; 
     } 

     //***Now Assign the Tab Headers 

    } 
    catch (Exception ex) { string ee = ex.Message; } 
    finally { con.Close(); } 
} 

}

+0

謝謝...... 現在起作用^ _ ^ – Kemoid