返回webservice中的最后一行

时间:2015-11-28 10:59:24

标签: c# sql web-services

我试图使用webservice获取所有表记录。我试过这个:

         public class Student
    {
        public int id;
        public string name;
        public string grade;
    }
 [WebMethod]
    public Student[] getall()
    {
        Student objStd = new Student();
        Student[] stds = new Student[400];


        SqlConnection conn;
        conn = Class1.ConnectionManager.GetConnection();
        conn.Open();

        SqlCommand newCmd = conn.CreateCommand();

        newCmd.CommandType = CommandType.Text;
        newCmd.CommandText = "select * from dbo.tblUser";
        SqlDataReader sdr = newCmd.ExecuteReader();

        for (int runs = 0; sdr.Read(); runs++)
        {
            objStd.id = Int32.Parse(sdr["Id"].ToString());
            objStd.name = sdr["name"].ToString();
            objStd.grade = sdr["grade"].ToString();
            stds[runs] = objStd;
        }
        conn.Close();
        sdr.Close();
        return stds;
    }

但结果如下:

 <?xml version="1.0" encoding="UTF-8"?>

  <ArrayOfStudent xmlns="http://tempuri.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 -<Student>
 <id>8</id>
 <name>hhhhh</name>
 <grade>76</grade>
 </Student>
 -<Student>
 <id>8</id>
 <name>hhhhh</name>
 <grade>76</grade>
 </Student>
 -<Student>
 <id>8</id>
 <name>hhhhh</name>
 <grade>76</grade>
 </Student>
 -<Student>
 <id>8</id>
 <name>hhhhh</name>
 <grade>76</grade>
 </Student>
  ....

这将一次又一次地返回最后一条记录,为什么?

我的代码应该更正哪些内容?

3 个答案:

答案 0 :(得分:2)

添加List并返回,现在只返回最后一个对象。

在For循环

中创建Student对象实例
 List<Student> stds = new List<Student>();
 for (int runs = 0; sdr.Read(); runs++)
        {
            Student objStd = new Student();
            objStd.id = Int32.Parse(sdr["Id"].ToString());
            objStd.name = sdr["name"].ToString();
            objStd.grade = sdr["grade"].ToString();
            stds.Add(objStd);
        }

答案 1 :(得分:0)

在循环中创建objStudent,这样您就不会继续更新同一个学生

 [WebMethod]
  public Student[] getall()
 {

    Student[] stds = new Student[400];


    SqlConnection conn;
    conn = Class1.ConnectionManager.GetConnection();
    conn.Open();

    SqlCommand newCmd = conn.CreateCommand();

    newCmd.CommandType = CommandType.Text;
    newCmd.CommandText = "select * from dbo.tblUser";
    SqlDataReader sdr = newCmd.ExecuteReader();

    for (int runs = 0; sdr.Read(); runs++)
    {
        Student objStd = new Student();
        objStd.id = Int32.Parse(sdr["Id"].ToString());
        objStd.name = sdr["name"].ToString();
        objStd.grade = sdr["grade"].ToString();
        stds[runs] = objStd;
    }
    conn.Close();
    sdr.Close();
    return stds;
 }

答案 2 :(得分:0)

如果您的学生数是动态使用列表而不是静态长度的数组。当您从Reader阅读新记录时,您还必须创建学生对象。

[WebMethod]
  public Student[] getall()
 {
    List<Student> studentList=new List<Student>();
    SqlConnection conn;
    conn = Class1.ConnectionManager.GetConnection();
    conn.Open();

    SqlCommand newCmd = conn.CreateCommand();

    newCmd.CommandType = CommandType.Text;
    newCmd.CommandText = "select * from dbo.tblUser";
    SqlDataReader sdr = newCmd.ExecuteReader();

    while(sdr.Read()){
        Student student= new Student();
        student.id = Int32.Parse(sdr["Id"].ToString());
        student.name = sdr["name"].ToString();
        student.grade = sdr["grade"].ToString();
        studentList.Add(student);
    }
    conn.Close();
    sdr.Close();
    return studentList.ToArray();
 }