记录来自数据库

时间:2012-06-25 12:20:26

标签: count sqldatareader

我有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();或不。

1 个答案:

答案 0 :(得分: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(); }
}

}