我有一个SQL Server数据库,其中一个日期列有时包含NULL.现在,我将该数据读入ASP.NET核心页,当遇到前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;
    }
}

推荐答案

如果它返回DBNull,那么你不能用GetString来阅读它.取而代之的是使用IsDBNull进行判断

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

或者,使用读取器上的indexer属性,并将其安全强制转换为最新

betaling = (reader[3] as DateTime)?.ToString("dd-MMM-yyyy") ?? "---",

Csharp相关问答推荐

WPF文本框点击鼠标 Select 内容

.NET框架4.7.2项目如何引用.NET Core 2.2库?

我无法在Ubuntu下编译使用microsoft.extension.configurationbuilder jsonapi和mono mcs的c#应用程序

如果存在对CodeAnalysis.CSharp的引用,则不能引用netStandard2.0库

在C#中,DirectoryEntry返回空AuditRules集合,即使审计规则确实存在

可为空的泛型属性

不带身份的Blazor服务器.Net 8 Cookie身份验证

C# CompareTo()和Compare()可以返回除-1和1以外的整数吗?

如何在用户在线时限制令牌生成?

Linux Docker上的.NET6在某个时间抛出后,在加密操作期间发生System.Security.Cryptography.CryptographicException:错误

为什么@rendermode Interactive Auto不能在.NET 8.0 Blazor中运行?

如何在microsoft.clearscript.v8的jsondata中使用Linq

Celler ArgumentExpression是否期望在所有情况下都捕获允许空值的运算符?

将FileStream的特定部分作为字节数组读取

.NET Google Workspace API获取错误CS0266

C# Winforms:从对象树到TreeView的递归转换重复条目

从GRPC连接创建ZipArchive

流畅的验证--如何为属性重用规则?

如何在Cake脚本中设置MSBuild.exe的绝对路径

Xamarin中出错.表单:应用程序的分部声明不能指定不同的基类