[英]c# Write null field to a datatable
`I'm having a problem writing a null result to a datatable. `我在向数据表写入null结果时遇到问题。 My linq query is returning values with which I'm populating a new instance of a class.
我的linq查询返回值,我正在填充一个类的新实例。 My datatable is being created generically and being populated with a generically created datarow.
我的数据表通常被创建并填充了一般创建的数据行。
What happens is my datatable is being created succesfully, the query runs, but when I hit the VAR statement it fails because one of the decimal fields is null. 发生的事情是我的数据表是成功创建的,查询运行,但是当我点击VAR语句时它会失败,因为其中一个十进制字段为空。 I cannot change this in the class because then I cannot create the datatable.
我不能在类中更改它,因为那时我无法创建数据表。
I need to change this one line I think to make it accept a null value: 我需要改变这一行,我想让它接受一个空值:
moneyvalue = result.moneyvalue,
This is my table definition: 这是我的表定义:
[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;
}
This is my class 这是我的班级
public class traded_product
{
public string Deal { get; set; }
public decimal moneyvalue { get; set; }
public string InvolvedPartyId { get; set; }
}
This is my query 这是我的查询
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
}
Here is how I create my datatable and datarow 以下是我创建数据表和数据行的方法
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;
}
Now as soon as it hits this part of the code after the "var query" I get the problem: 现在,只要在“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);
}
It fails as soon as it hits the foreach, because the value returned from the database for moneyvalue is null. 它一旦到达foreach就会失败,因为从moneyvalue数据库返回的值为null。
I cannot change the class piece to decimal? 我不能将类片改为十进制? otherwise the CreateDatable method fails because it says DataTable cannot have a nullable value.
否则CreateDatable方法失败,因为它说DataTable不能具有可空值。
If it is allowed to write NULL values to the database you should make your variable types nullable, for example 例如,如果允许将NULL值写入数据库,则应使变量类型为可为空
[Column]
public decimal? moneyvalue;
instead of 代替
[Column]
public decimal moneyvalue;
I think your issue is in 我认为你的问题在于
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
}
* Update * *更新*
Why not construct your datatable
using traded_product
and as @user65439 mentioned change your DB class ( t_sdi_traded_product
) to have a nullable column 为什么不建立你的
datatable
使用traded_product
和@ user65439提到改变你的DB类( t_sdi_traded_product
)有一个空列
[Column]
public decimal? moneyvalue;
Then you just have to handle nulls being returned and converting them to 0 for your not-nullable decimal in your traded_product
class 然后你只需要处理返回的空值并将它们转换为0,以获得
traded_product
类中不可为空的小数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.