C# 自动将数据从 SQL table 插入到 Web 应用程序

C# auto-insert data from SQL table to web application

我在将 SQL table (pgadmin) 中的值插入到 Web 应用程序时遇到问题。如果我在 pgadmin 中检查我的 table,一切正常:

但是如果我将它推送到 Azure 上的应用程序中,它只会 returns:

不知道问题出在哪里

display.data.razor:

@page "/displaydata"
@using WebApplication1.Data;
@using WebApplication1.Services;
@inherits OwningComponentBase<DataService>

<h1>Display data</h1>

<table border="1">
    <tr>
        <th>
            id
        </th>
        <th>
            Nazov IMG
        </th>
        <th>
            label
        </th>
    </tr>

    @foreach (WebApplication1.Data.Dataset item in sc)
    {
        <tr>
            <td>@item.relid</td>
            <td>@item.name</td>
            <td>@item.label</td>
        </tr>
    }

</table>

@code {
    public System.Collections.Generic.IList<Dataset> sc;

    protected override void OnInitialized()
    {
        sc = Service.displaydata();
        foreach (var item in sc)
        {
            Console.Write(@item.relid + " " + @item.label);
        }
}
}

Console.Write(@item.relid + " " + @item.label) 的结果:

2 "Gold(99.64134)2 "Gold(99.64134)2 "Gold(99.64134)2 "Gold(99.64134)2 "Gold(99.64134)2 "Gold(99.64134)2 "Gold(99.64134)2 "Gold(99.64134)2 "Gold(99.64134)2 "Gold(99.64134)2 "Gold(99.64134)2 "Gold(99.64134)2 "Gold(99.64134)2 "Gold(99.64134)

数据服务:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApplication1.Data;
namespace WebApplication1.Services
{
    public class DataService
    {
        protected readonly ApplicationDbContext _dbcontext;
        public DataService(ApplicationDbContext _db)
        {
            _dbcontext = _db;
        }
        public List<Dataset> displaydata()
        {
            return _dbcontext.results.ToList();
        }
    }
}

数据:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace WebApplication1.Data
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {
        }
        public DbSet<Dataset> results { get; set; }
    }
}

请按照我的建议进行问题排查

  1. 首先确认在本地运行时,foreach中显示的数据是正确的

  2. 登录scm网站,或者在门户中点击应用服务编辑器,查看web.config文件。或者检查配置中的连接字符串。

    区别:

    如果在Connection strings中设置,web.config中的数据库连接字符串将被覆盖。如果没有,那么web程序会根据web.config.

    中的字符串进行连接

  3. 根据字符串,使用SSMS工具查找数据库,检查数据是否与页面显示的错误数据一致

  4. 如果第一步本地显示的数据不正确,建议您新建一个demo代码上传到github,以便我们进行本地调试。 (小心隐藏您的机密信息)