无法在 fiddler 中显示从 table 中提取的数据

Unable to show extracted data from the table in fiddler

我编写了以下代码来从 SQL 服务器中的 table 中提取所有记录。代码中没有错误,但是当我在 fiddler 中 运行 时,我得到了空对象。

    [HttpGet]
    public List<Student> Get()
    {
        SqlDataReader reader = null;

        SqlConnection myConnection = new SqlConnection();

        myConnection.ConnectionString = @"Data Source=PALLAVI-PC\SQLEXPRESS;Initial Catalog=StudentDB;Integrated Security=True;MultipleActiveResultSets=True;";

        SqlCommand sqlCmd = new SqlCommand();
        sqlCmd.CommandType = CommandType.Text;
        sqlCmd.CommandText = "Select * from Tbl_Students;";
        sqlCmd.Connection = myConnection;

        myConnection.Open();

        reader = sqlCmd.ExecuteReader();

        int rowCount = 0;

        while (reader.Read())
        {
            rowCount++;
        }

        Student[] student = new Student[rowCount];
        for(int i=0;i<rowCount;i++)
        {
            student[i] = new Student();
        }

        int j = 0;

        while (reader.Read())
        {
          //  student[j] = new Student();
            student[j].Roll_Number = Convert.ToInt32(reader.GetValue(0));
            student[j].FirstName = reader.GetValue(1).ToString();
            student[j].LastName = reader.GetValue(2).ToString();
            student[j].Class = Convert.ToInt32(reader.GetValue(3));
            student[j].Gender = reader.GetValue(4).ToString();

            j++;

        }

        return student.ToList();
        myConnection.Close();
    }

我在 table 中有 5 条记录。我能够获得 5 json 个对象,但没有内容。

附加来自 Fiddler 的图像:

Student.cs

namespace WebAPIDemo.Models
{
   public class Student
   {
      public int Roll_Number { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public int Class { get; set; }
      public string Gender { get; set; }
   }
 }

试试这个,不计算记录数。我没有使用数组,我为每条记录创建了新的学生实例。

 public static List<Student> GetData()
    {
        List<Student> lstStudent = new List<Student>();
        using (SqlConnection con = new SqlConnection(@"Data Source=PALLAVI-PC\SQLEXPRESS;Initial Catalog=StudentDB;Integrated Security=True;MultipleActiveResultSets=True;"))
        {
            using (SqlCommand cmd = new SqlCommand("Select * from Tbl_Students;", con))
            {
                cmd.CommandType = CommandType.Text;
                if (con.State == ConnectionState.Closed)
                    con.Open();
                SqlDataReader reader = cmd.ExecuteReader();                   

                while(reader.Read())
                {
                    Student student =new Student();

                    student.Roll_Number = Convert.ToInt32(reader.GetValue(0));
                    student.FirstName = reader.GetValue(1).ToString();
                    student.LastName = reader.GetValue(2).ToString();
                    student.Class = Convert.ToInt32(reader.GetValue(3));
                    student.Gender = reader.GetValue(4).ToString();

                    lstStudent.Add(student);
                }                   

            }
        }
        return lstStudent;
    }

您的第一个 while(reader.Read()) 是带有数据的实际 reader 对象。 DataReader 允许向前只读访问数据 row.The 下一个 while 循环甚至不会执行,因为 Read() 已经 completed.So 你得到一个用默认值初始化的 5 Student 个对象的数组。

而不是初始化 Student 的数组填充实例并添加到 while 循环内的 List<Student>