C#返回数组对象
//返回数组对象
public List<User> GetUser()
{try
{
using (SqlConnection testConnection = new SqlConnection(ConnStr.connectionString))
{
SqlCommand testCommand = testConnection.CreateCommand();
testCommand.CommandText = "select [id],[name] from [user]";
testConnection.Open();
List<User> users = new List<User>();
SqlDataReader rd = testCommand.ExecuteReader();
while (rd.Read())//read next
{
User userback = new User();
userback.ID = rd.GetInt32(0);
userback.Name = rd.GetString(1);
users.Add(userback); //add to list
}
Console.WriteLine("Back all sql User");
return users;
}
}
catch (Exception e1)
{
Console.WriteLine(e1.Message);
return null;
}
}