我有一个MVC3应用程序.我调用数据库,在数据集中获取数据并将其存储在我的类中.下面是如何从数据集中存储我的数据
var dt = ds.Tables[0];
var i = 0;
foreach (DataRow dr in dt.Rows)
{
wt = new WorkTasksDto
{
Notes = dr["Notes"].ToString(),
DueDate = Convert.ToDateTime(dr["DueDate"])
}
Run Code Online (Sandbox Code Playgroud)
这是我的班级成员:
[DataMember]
public DateTime? DueDate
{ get; set; }
Run Code Online (Sandbox Code Playgroud)
从sql server返回的字段DueDate为null,这很好但是当我尝试将它存储在我的类中的DueDate字段时,我收到以下错误:
"Object cannot be cast from DBNull to other types"
Run Code Online (Sandbox Code Playgroud)
如何修复此错误?谢谢
看一眼 DataRow.IsNull
DueDate = dr.IsNull("DueDate")? (DateTime?)null : (DateTime?)dr["DueDate"]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3826 次 |
| 最近记录: |