我试图通过控制器从DAL传递列表,我在数据表中收集SQL数据,因此我试图将DataTable更改为控制器中的List,但没有成功。
这是我的DAL方法:
public DataTable findAllUsers()
{
string queryText = "SELECT UserNbr FROM Users";
cmd = new SqlCommand(queryText, con);
con.Open();
da = new SqlDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
con.Close();
return dt;
}
这是我的控制者:
public List<User> allUsers ()
{
List<User> allUsers = DAL.findAllUsers.ToList();
}
答案 0 :(得分:1)
我认为你需要这样的东西:
// This is a class that represents a row of your DataTable.
public class User
{
public int UserNbr { get; set;}
public User(int userNbr)
{
UserNbr = userNbr;
}
}
public List<User> allUsers ()
{
var users = new List<User>();
// Create an instance of the DAL class.
var dal = new Restaurang4.DAL();
// Loop through the datatable's rows and create foreach of them
// a new User and then add it to the users list.
foreach(var dataRow in dal.findAllUsers().Rows)
users.Add(new User(dataRow.Field<int>("UserNbr ")));
return users;
}