【问题标题】:ASP.NET Core display all rows in database on pageASP.NET Core 在页面上显示数据库中的所有行
【发布时间】:2021-06-09 21:58:01
【问题描述】:

我正在尝试使用 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、用户名、密码、密码哈希,但我只想显示用户名开头。

如果您有任何来源或想法,将非常感激!先谢谢了!

最好的问候马克斯

【问题讨论】:

    标签: c# sql asp.net-core asp.net-core-mvc


    【解决方案1】:

    如果你真的想使用原始的 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);
    }
    

    您不需要向数据库发出两次请求 - 这非常浪费。

    您只想显示用户名,所以只请求它就足够了。

    【讨论】:

    • 谢谢!现在我更好地理解了从数据库中读取的方式,我最终使用了带有 EntityFramework 的 DbContext 并从那里获取值。
    【解决方案2】:

    我建议你使用 .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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-30
      • 2018-08-23
      • 2017-03-14
      • 1970-01-01
      • 2018-08-28
      • 2018-07-16
      • 1970-01-01
      相关资源
      最近更新 更多