【问题标题】:c# Write null field to a datatablec# 将空字段写入数据表
【发布时间】:2013-09-18 02:15:29
【问题描述】:

`我在将空结果写入数据表时遇到问题。 我的 linq 查询正在返回用于填充类的新实例的值。 我的数据表是通用创建的,并使用通用创建的数据行填充。

发生的情况是我的数据表已成功创建,查询运行,但是当我点击 VAR 语句时它失败,因为其中一个十进制字段为空。我无法在课堂上更改它,因为这样我就无法创建数据表。

我需要更改我认为的这一行以使其接受空值:

moneyvalue = result.moneyvalue,

这是我的表定义:

[Table(Name = "t_sdi_traded_product")]
public class t_sdi_traded_product
{
    [Column]
    public string deal_position_id;
    [Column]
    public decimal moneyvalue;
    [Column]
    public string cost_centre;
}

这是我的课

 public class traded_product
{
    public string Deal { get; set; }
    public decimal moneyvalue { get; set; }
    public string InvolvedPartyId { get; set; }
}

这是我的查询

 var query =
            from result in t_sdi_traded_product_hsbc.AsQueryable()
            where result.sdi_control_id == current_control_id
            select new traded_product() 
            { 
                Deal = result.deal_position_id, 
                moneyvalue = result.moneyvalue,
                InvolvedPartyId = result.involved_party_id
             }

这是我创建数据表和数据行的方式

public static DataTable CreateDataTable(Type animaltype)
    {
        DataTable return_Datatable = new DataTable();
        foreach (PropertyInfo info in animaltype.GetProperties())
        {
            return_Datatable.Columns.Add(new DataColumn(info.Name, info.PropertyType));
        }
        return return_Datatable;
    }

    public static DataRow makeRow(object input, DataTable table)
    {
        Type inputtype = input.GetType();
        DataRow row = table.NewRow();
        foreach (PropertyInfo info in inputtype.GetProperties())
        {
            row[info.Name] = info.GetValue(input, null);
        }
        return row;
    }

现在只要在“var query”之后遇到这部分代码,我就会遇到问题:

foreach (var results in query)
        {
            foreach (PropertyInfo result in results.GetType().GetProperties())
            {
                string name = result.Name;

                foreach (PropertyInfo info in used.GetType().GetProperties())
                {
                    if (result.Name == info.Name)
                    {
                        try
                        {
                            info.SetValue(used, result.GetValue(results, null), null);
                        }
                        catch (NoNullAllowedException e)
                        {
                        }
                        finally
                        {
                            info.SetValue(used, DBNull.Value, null);
                        }
                        //Console.WriteLine("Result {0} matches class {1} and the value is {2}", result.Name, info.Name, result.GetValue(results,null));
                    }
                }
            }
            tp_table.Rows.Add(used, tp_table);
        }

一碰到foreach就失败了,因为从数据库返回的moneyvalue值是空的。

我无法将班级部分更改为十进制?否则 CreateDatable 方法会失败,因为它说 DataTable 不能有可为空的值。

【问题讨论】:

    标签: c# linq generics datatable nullable


    【解决方案1】:

    如果允许将 NULL 值写入数据库,则应使变量类型可为空,例如

    [Column]
    public decimal? moneyvalue;
    

    而不是

    [Column]
    public decimal moneyvalue;
    

    【讨论】:

      【解决方案2】:

      我认为你的问题在

      select new traded_product() 
      { 
          Deal = result.deal_position_id, 
          moneyvalue = result.moneyvalue, <-- here you need some handling for DBNULL.Value
          InvolvedPartyId = result.involved_party_id
      }
      
      select new traded_product() 
      { 
          Deal = result.deal_position_id, 
          moneyvalue = result.moneyvalue == DBNull.Value ? 0m : result.moneyvalue,
          InvolvedPartyId = result.involved_party_id
      }
      

      * 更新 *

      为什么不使用traded_product 构造您的datatable,正如@user65439 所提到的,将您的数据库类(t_sdi_traded_product)更改为可以为空的列

      [Column]
      public decimal? moneyvalue;
      

      然后您只需要处理返回的空值并将它们转换为 0 以在您的 traded_product 类中为您的不可为空的十进制数

      【讨论】:

      • 我也在尝试。我将 [COLUMN] 设置为十进制?我有以下内容 outval(result.funding_rate.ToString()) ? (object)result.funding_rate : (object)DBNull.Value, 其中 outval 是一个布尔方法来检查它是否是小数仍然得到一个错误说不能将类型对象转换为十进制 @SecretSquirrel @user65439
      • 那是因为你正在投射到一个对象。
      • outval = result.funding_rate == DBNull.Value;
      猜你喜欢
      • 2019-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 2022-01-17
      • 1970-01-01
      相关资源
      最近更新 更多