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