首页 > 解决方案 > 使用 C# 中的应用程序在 SQL Server 中丢失数据

问题描述

我在 SQL Server 中有一个数据库,我正在使用 C# 中的应用程序插入数据。

问题是,当我插入数据时,数据没有存储在右列中。

这是我的代码:

    command.CommandText = "INSERT INTO pointagee (Nom_employe, Date_Heure , Matin, Midi, apresmidi, Soir) VALUES ( ? , ? , ? , ?, ?, ?);";
    command.Parameters.AddWithValue("nom", nom_employe);
    command.Parameters.AddWithValue("date", date_heure);

    if (matin == null)
    {
        command.Parameters.AddWithValue("matin", "NULL");
    }
    else
    {
        command.Parameters.AddWithValue("matin", matin);
    }
    if (midi == null)
    {
        command.Parameters.AddWithValue("midi", "NULL");
    }
    else
    {
        command.Parameters.AddWithValue("midi", midi);
    }
    if (Apresmidi == null)
    {
        command.Parameters.AddWithValue("apresmidi", "NULL");
    }
    else
    {
        command.Parameters.AddWithValue("apresmidi", Apresmidi);
    }
    if (Soir == null)
    {
        command.Parameters.AddWithValue("soir", "NULL");
    }
    else
    {
        command.Parameters.AddWithValue("soir", Soir);
    }

这是数据库中的一个示例:

这些是列:

ID_employe   Nom_employe       Date_Heure   Matin          Midi         apresmidi        Soir                               

1           26/02/2020        henry        26/02/2020     12:12:12      NULL             NULL   

由于这个问题,这会丢失一个 NULL 值。

标签: c#sql-server

解决方案


推荐阅读