SqlDataReader返回x行,但SQL查询返回y行

时间:2014-11-19 01:05:06

标签: c# sql sql-server

第一次发帖,因为我有点卡在这里。

我正在使用此代码从SQL Server数据库返回一些行:

public static SqlDataReader SQLSelect(string sqlcommand, string[,] parameters, int length)
{
    try
    {
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
        conn.Open();

        SqlDataReader reader;
        SqlCommand cmd = new SqlCommand(sqlcommand, conn);

        var allLength = parameters.Length;

        for (int i = 0; i < parameters.Length - length; i++)
        {
            string paramid = parameters[i, 0];

            if (paramid == "@date" || paramid == "@Date" || paramid == "@DATE")
            {
                string paramvalue = parameters[i, 1];
                DateTime date = Convert.ToDateTime(paramvalue);
                paramvalue = date.ToString("yyyy-MM-dd HH':'mm':'ss");

                cmd.Parameters.Add(new SqlParameter(paramid, paramvalue));
            }
            else
            {
                string paramvalue = parameters[i, 1];
                cmd.Parameters.Add(new SqlParameter(paramid, paramvalue));
            }
        }

        cmd.CommandType = CommandType.StoredProcedure;

        reader = cmd.ExecuteReader();

        return reader;
    }
    catch
    {
        return null;
    }
}

这个函数就是这样调用的

string[,] parameters = new string[1, 2] { { "@studentid", studentid } };
SqlDataReader reader = Common.SQLSelect(Common.tblstudentprogressselectforprinting, parameters, 1);

现在一切运行正常,但读者只包含13行数据,其中实际查询为

exec sp_tblstudentprogress_selectforprinting @studentid=N'87'

作为示例,返回91行。

我不知道为什么会这样。我唯一注意到的是当使用SQL Server探查器,从SQL Server运行查询时,有一个RPC:已启动和已完成,从使用我的Web应用程序运行时,只有一个RPC:已启动。

对此有何想法?

编辑:

这是我如何枚举读者

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            string[,] parameters = new string[1, 2] { { "@studentid", studentid } };
            SqlDataReader reader = Common.SQLSelect(Common.tblstudentprogressselectforprinting, parameters, 1);

            string firstname = txtFirstName.Text;
            string lastname = txtLastName.Text;
            int i=0;
            string[] heading1 = new string[reader.FieldCount];
            string[] heading2 = new string[reader.FieldCount];
            string[] log = new string[reader.FieldCount];

            try
            {
                while (reader.Read())
                {
                    heading1[i] = "Progress Log for: Block: " + reader["block"].ToString() + " Lesson: " + reader["lesson"].ToString();
                    heading2[i] = "";
                    log[i] =
                        /*"PROGRESS LOG for " + reader["firstname"].ToString() + " " + reader["lastname"].ToString() + " Printed on " + DateTime.Today.ToShortDateString() + Environment.NewLine +*/
                        Environment.NewLine +
                        "Teacher: " + reader["teacher"].ToString() + Environment.NewLine +
                        "Date: " + reader["date"].ToString() + Environment.NewLine +
                        "Year: " + reader["year"].ToString() + Environment.NewLine +
                        "Block: " + reader["block"].ToString() + Environment.NewLine +
                        "Lesson: " + reader["lesson"].ToString() + Environment.NewLine +
                        "Warm Up: " + reader["warmup"].ToString() + Environment.NewLine +
                        "Range: " + reader["range"].ToString() + Environment.NewLine +
                        "Technique Sheet: " + reader["techniquesheet"].ToString() + Environment.NewLine +
                        "Technique Other: " + reader["techniqueother"].ToString() + Environment.NewLine +
                        Environment.NewLine +
                        "Notes: " + reader["notes"].ToString() + Environment.NewLine +
                        Environment.NewLine +
                        "Mark: " + reader["mark"].ToString()+ Environment.NewLine ;

                    i++;
                }
            }
            catch
            {

            }
            finally
            {
                if (Common.conn != null)
                {
                    Common.conn.Close();
                }
            }


            Common.PDFCreateProgressLog("Progress log for: " + firstname + " " + lastname, "Progress log for: " + firstname + " " + lastname, "PDF_" + firstname + " " + lastname + "-" + DateTime.Today.ToString("yyyy-MM-dd") + ".pdf", "Progress log for: " + firstname + " " + lastname, log, heading1, heading2);

        }

1 个答案:

答案 0 :(得分:3)

您混淆了FieldCount属性的含义。它标识的数量,而不是的数量。您无法确定来自流媒体源(如Reader)的行总数,而无需先计算所有行(至少一次,无论如何)。

所以你需要每次扩展你的数组(列表可能更容易)你从Reader读取一行并在没有更多行时测试Reader。