SQL Server 数据库中的 NULL 值

问题描述 投票:0回答:1

我有一个 SQL Server 数据库,其中有时日期列包含

NULL
。现在我将该数据读入 ASP.NET Core 页面,当它遇到第一个
NULL
列时它停止读取。

NULL
应该用空格替换,因为稍后该字段将获得日期。谁能帮帮我?

这是我的代码:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System;
using System.Data.SqlClient;

namespace Rectusoft_Tournament_organizer.Pages
{
    public class TeamsModel : PageModel
    {
        public List<TeamInfo1> listTeams = new();
        public string? sessionvariable;

        //Dit moeten we hierzetten en de variabele instellen
        
        public void OnGet()
        {
            try
            {
                String connectionString = "D********************";
                using SqlConnection connection = new(connectionString);
                connection.Open();

                String sql = "select team.naam as teamnaam, land.naam as landnaam, land.vlag, dbo.fnBetalingsdatum(team.id) as datum, division.naam , multiplificator_divisie from team, land, division where team.id_land = land.id and team.id_division = division.Id and team.id_toernooi = '86188502-2c57-4931-addc-9813959bbd97' and team.aktief = 'J' order by multiplificator_divisie desc, CASE WHEN dbo.fnBetalingsdatum(team.id) IS NULL THEN 1 ELSE 0 END, dbo.fnBetalingsdatum(team.id), id_division";

                using SqlCommand command = new(sql, connection);
                using SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    TeamInfo1 teamInfo1 = new()
                    {
                        naam = reader.GetString(0),
                        land = reader.GetString(1),
                        vlag = reader.GetString(2),
                    
                        if (!DBNull.Value.Equals(reader.GetString(3)))
                        {
                            //IServerSideBlazorBuilder not null
                            teamInfo1.betaling = reader.GetDateTime(3).ToString("dd-MMM-yyyy")!= null)
                        }                   
                        else
                        {
                            //null
                            teamInfo1.betaling = reader.GetDateTime(3).ToString("---"),
                        };
                    };
                    
                    listTeams.Add(teamInfo1);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("NOT Connected");  // something like this
                Console.WriteLine("Exception: connectie NIET ok " + ex.Message);
            }
        }
    }

    public class TeamInfo1
    {
        public String? naam;
        public String? land;
        public String? vlag;
        public String? betaling;
    }
}
c# sql-server ado.net sqlclient dbnull
1个回答
1
投票

如果它返回

DBNull
那么你不能用
GetString
阅读它。相反,使用
IsDBNull
来检查它

betaling = reader.IsDBNull(3) ?
    "---" :
    reader.GetDateTime(3).ToString("dd-MMM-yyyy"),

或者,在阅读器上使用索引器属性,并将其安全转换为最新

betaling = (reader[3] as DateTime)?.ToString("dd-MMM-yyyy") ?? "---",
© www.soinside.com 2019 - 2024. All rights reserved.