从数据库 asp 获取数据时跳过 table 的行。净 C#

skipping rows from table while fetching data from database asp. net C#

fees_structure 有 24 "A" 个类别行。但是当我尝试获取它们并打印时,它只显示 20 或(有时 21 行)

这是我的代码:

string catA = "SELECT id,fees_name,amount,fee_category FROM fees_structure where fee_category='A' ORDER BY id";

using (SqlCommand scom = new SqlCommand(catA, con))
{
    using (SqlDataReader read = scom.ExecuteReader())
    {
        read.Read();

        if (read.HasRows)
        {
            while (read.Read())
            {
                feestableA.Append("<tr>");
                feestableA.Append("<td>" + read["fees_name"] + "</td>");
                feestableA.Append("<td>" + read["amount"] + "</td>");
                feestableA.Append("</tr>");
            }

            plcfeesA.Controls.Add(new Literal { Text = feestableA.ToString() });
            plcfeesA.Dispose();
        }
    }
}

我认为这不是 C# 问题。

试试这个

string catA = "SELECT id,fees_name,amount,fee_category FROM fees_structure where fee_category like '%A%' ORDER BY id";

看看你的结果是否改变

while前的read()可疑。我怀疑是吃一排。

丢失行的另一种可能性是区分大小写的排序规则——如果类别可能是 'a'(或另一种编码的变体)。但是,这取决于用于列、数据库和服务器的默认或显式排序规则。

试试这个。仔细观察,我删除了执行您不想完成的事情的代码行。

string catA = "SELECT id,fees_name,amount,fee_category FROM fees_structure where fee_category='A' ORDER BY id";

using (SqlCommand scom = new SqlCommand(catA, con))
{
    using (SqlDataReader read = scom.ExecuteReader())
    {
        while (read.Read())
        {
            feestableA.Append("<tr>");
            feestableA.Append("<td>" + read["fees_name"] + "</td>");
            feestableA.Append("<td>" + read["amount"] + "</td>");
            feestableA.Append("</tr>");
        }

        plcfeesA.Controls.Add(new Literal { Text = feestableA.ToString() });
    }
}

}