错误列"列名称"不属于桌子

时间:2015-03-06 16:09:55

标签: c# sql .net sql-server datatable

我正在尝试从表Student中检索数据,并将值存储到Student个对象的列表中。在这样做的时候,我遇到了错误

  

列'Id'不属于表

我真的不明白我哪里出错了。请帮忙。

private List<Student> GetAllStudents
{
    get
    {
        students = new List<Student>();

        com = new SqlCommand("SELECT * FROM Student");
        // com.CommandType = CommandType.StoredProcedure;
        sda = new SqlDataAdapter();
        com.Connection = con;
        sda.SelectCommand = com;
        dt = new DataTable();
        sda.Fill(dt);

        foreach (DataRow dr in dt.Rows)
        {
            st = new Student();

            st.Id = Int32.Parse(dr["Id"].ToString());
            st.Name = dr["Name"].ToString();
            st.Year = Int32.Parse(dr["Year"].ToString());
            st.Dept = dr["Dept"].ToString();
            st.Place = dr["Place"].ToString();

            students.Add(st);
        }

        return students;
    }
}

这就是我的表架构的样子

public class Student {
     public int Id { get; set; }
     public string Name { get; set; }
     public int Year { get; set; }
     public string Dept { get; set; }
     public string Place { get; set; }
}

0 个答案:

没有答案