【问题标题】:C# auto-insert data from SQL table to web applicationC# 自动将数据从 SQL 表插入到 Web 应用程序
【发布时间】:2021-05-07 09:46:22
【问题描述】:

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

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

我不知道问题出在哪里。

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; }
    }
}

【问题讨论】:

  • 调试的时候可以查看sc的数据源吗?或者让我们看看你的方法在运行时的查询语句。
  • 我编辑了它,也许现在? @JasonPan

标签: c# html sql azure pgadmin


【解决方案1】:

请按照我的建议进行故障排除

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

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

    区别:

    如果在连接字符串中设置,web.config中的数据库连接字符串将被覆盖。如果没有,则web程序将根据web.config中的字符串进行连接。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多