c#将空字段写入数据表

大众汽车

`我在将空结果写入数据表时遇到问题。我的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值为null。

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

秘密松鼠

我想你的问题在

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
}

* 更新 *

为什么不构造您的datatableusingtraded_product并且如@ user65439所述将您的数据库类(t_sdi_traded_product更改为具有可为空的列

[Column]
public decimal? moneyvalue;

然后,您只需要处理返回的空值并将其转换为0,即可将您的traded_product类中的空值保留为十进制

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章