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(); 并不重要。或不。

4

1 回答 1

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

}

于 2012-06-26T10:05:52.153 回答