ASP.NET 核心在页面上显示数据库中的所有行
ASP.NET Core display all rows in database on page
我正在尝试使用 ASP.NET Core MVC 在网站上显示数据库中的每一行,但我找不到任何关于如何做到这一点的资源。这就是我尝试做的,但我得到了卡住了:
public IActionResult Index()
{
connection.Open();
command.Connection = connection;
command.CommandText = "SELECT COUNT(*) FROM Users;";
var rows = Convert.ToInt32(command.ExecuteReader());
command.Dispose();
List<UserModel> users = new List<UserModel>();
for(int i = 0; i <= rows; i++)
{
users.Add(new UserModel(ID, "", ""));
}
command.CommandText = "SELECT * FROM Users";
dataReader = command.ExecuteReader();
return View();
}
我的数据库结构如下:Id、用户名、密码、PasswordHash,但我只想显示用户名开头。
如果您有任何消息来源或想法,我们将不胜感激!提前致谢!
最好的问候 Max
我推荐你使用 .NET 最大的 ORM,Entity Framework。
创建这个
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options): base(options)
{
}
public DbSet<UserModel> Users { get; set; }
}
添加到 Startup.cs
中的 ConfigureServices 方法
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(
options => options.UseSqlServer({"your_connection_string"}));
}
在你的控制器上
public class YourController : Controller
{
private ApplicationDbContext ApplicationDbContext { get; }
public YourController(ApplicationDbContext applicationDbContext)
{
ApplicationDbContext = applicationDbContext;
}
public async Task<IActionResult> Index()
{
var users = await ApplicationDbContext.Users.ToListAsync();
return View(users);
}
}
那么,在你看来
@model List<YourNamespace.UserModel>
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
@foreach (var user in Model)
{
<tr>
<th>@user.Name</th>
</tr>
}
</tbody>
</table>
参考资料
https://docs.microsoft.com/pt-br/ef/core/dbcontext-configuration/
如果你真的想使用raw ADO.NET,那好吧,我给你举个例子。
public IActionResult Index()
{
using var connection = new SqlConnection(_connectionString);
connection.Open();
using var command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SELECT Username FROM Users;";
using var reader = command.ExecuteReader();
List<UserModel> users = new List<UserModel>();
while (reader.Read())
{
string name = reader.GetString(0);
users.Add(new UserModel { Name = name });
}
return View(users);
}
您不需要向数据库发出两次请求 - 这非常浪费。
您只想显示用户名,所以只请求它就足够了。
我正在尝试使用 ASP.NET Core MVC 在网站上显示数据库中的每一行,但我找不到任何关于如何做到这一点的资源。这就是我尝试做的,但我得到了卡住了:
public IActionResult Index()
{
connection.Open();
command.Connection = connection;
command.CommandText = "SELECT COUNT(*) FROM Users;";
var rows = Convert.ToInt32(command.ExecuteReader());
command.Dispose();
List<UserModel> users = new List<UserModel>();
for(int i = 0; i <= rows; i++)
{
users.Add(new UserModel(ID, "", ""));
}
command.CommandText = "SELECT * FROM Users";
dataReader = command.ExecuteReader();
return View();
}
我的数据库结构如下:Id、用户名、密码、PasswordHash,但我只想显示用户名开头。
如果您有任何消息来源或想法,我们将不胜感激!提前致谢!
最好的问候 Max
我推荐你使用 .NET 最大的 ORM,Entity Framework。
创建这个
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options): base(options)
{
}
public DbSet<UserModel> Users { get; set; }
}
添加到 Startup.cs
中的 ConfigureServices 方法public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(
options => options.UseSqlServer({"your_connection_string"}));
}
在你的控制器上
public class YourController : Controller
{
private ApplicationDbContext ApplicationDbContext { get; }
public YourController(ApplicationDbContext applicationDbContext)
{
ApplicationDbContext = applicationDbContext;
}
public async Task<IActionResult> Index()
{
var users = await ApplicationDbContext.Users.ToListAsync();
return View(users);
}
}
那么,在你看来
@model List<YourNamespace.UserModel>
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
@foreach (var user in Model)
{
<tr>
<th>@user.Name</th>
</tr>
}
</tbody>
</table>
参考资料 https://docs.microsoft.com/pt-br/ef/core/dbcontext-configuration/
如果你真的想使用raw ADO.NET,那好吧,我给你举个例子。
public IActionResult Index()
{
using var connection = new SqlConnection(_connectionString);
connection.Open();
using var command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SELECT Username FROM Users;";
using var reader = command.ExecuteReader();
List<UserModel> users = new List<UserModel>();
while (reader.Read())
{
string name = reader.GetString(0);
users.Add(new UserModel { Name = name });
}
return View(users);
}
您不需要向数据库发出两次请求 - 这非常浪费。
您只想显示用户名,所以只请求它就足够了。