试图通过控制器从 DAL 传递列表
Trying to pass a list from DAL through Controller
我试图通过控制器从 DAL 传递一个列表,我在数据表中收集了 SQL-数据,所以我试图将数据表更改为控制器中的列表,但是没有成功。
这是我的 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();
}
我认为你需要这样的东西:
// 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;
}
我试图通过控制器从 DAL 传递一个列表,我在数据表中收集了 SQL-数据,所以我试图将数据表更改为控制器中的列表,但是没有成功。
这是我的 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();
}
我认为你需要这样的东西:
// 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;
}