我在Stack Overflow上看到了关于在DBNull和可空类型之间进行转换的这个帖子,但我仍然感到困惑.我编写了这样的代码,带有一些黑客代码,用于处理DateTime和int的可空类型,我将在下面展示但是它很乱,我想使用Nullable类型:
DataTable dt = ds.Tables[0]; Listlist = (from dr in dt.AsEnumerable() select new RON { RONId = dr[0].ToString(), StaffNumber = dr[1].ToString(), CheckInDate = GetCheckInDate(dr[2]), NonMissionIndicator = dr[3].ToString(), Comments = dr[4].ToString(), NonComplyIndicator = dr[5].ToString(), LOAirport = dr[6].ToString(), RONAirport = dr[7].ToString(), PropertyId = GetPropertyId(dr[8]), PropertyChain = dr[9].ToString(), PropertyName = dr[10].ToString(), PropertyStreet = dr[11].ToString(), PropertyStreet2 = dr[12].ToString(), PropertyCity = dr[13].ToString(), PropertyState = dr[14].ToString(), PropertyPostalCode = dr[15].ToString(), PropertyPhone = dr[16].ToString(), FAX = dr[17].ToString(), PropertyCountry = dr[18].ToString(), PropertyLongitude = GetPropertyLongitude(dr[19]), PropertyLatitude = GetPropertyLatitude(dr[20]), PropertyAirport = dr[21].ToString(), ReportedBy = dr[22].ToString(), ReportedDTS= GetReportedByDate(dr[23]), CanceledBy = dr[24].ToString(), CanceledDTS = GetCanceledByDate(dr[25]) }).ToList(); return list; } //TODO: Change Defaukt date private static DateTime GetCheckInDate(object checkInDate) { return checkInDate == DBNull.Value ? new DateTime(2000, 1, 1, 00, 00, 00) : Convert.ToDateTime(checkInDate); } //TODO: Change Defaukt date private static DateTime GetReportedByDate(object reportedByDate) { return reportedByDate == DBNull.Value ? new DateTime(2000, 1, 1, 00, 00, 00) : Convert.ToDateTime(reportedByDate); } //TODO: Change Defaukt date private static DateTime GetCanceledByDate(object canceledByDate) { return canceledByDate == DBNull.Value ? new DateTime(2000, 1, 1, 00, 00, 00) : Convert.ToDateTime(canceledByDate); } private static Int32 GetPropertyId(object propertyId) { return propertyId == DBNull.Value ? 0 : Convert.ToInt32(propertyId); } private static double GetPropertyLongitude(object propertyLongitude) { return propertyLongitude == DBNull.Value ? 0.0 : Convert.ToDouble(propertyLongitude); } private static double GetPropertyLatitude(object propertyLatitude) { return propertyLatitude == DBNull.Value ? 0.0 : Convert.ToDouble(propertyLatitude); }
RON现在定义为:
public class RON { public string RONId { get; set; } public string StaffNumber { get; set; } public DateTime CheckInDate { get; set; } public string NonMissionIndicator { get; set; } public string NonComplyIndicator { get; set; } public string LOAirport { get; set; } public string RONAirport { get; set; } public int PropertyId { get; set; } public string PropertyChain { get; set; } public string PropertyName { get; set; } public string PropertyStreet { get; set; } public string PropertyStreet2 { get; set; } public string PropertyCity { get; set; } public string PropertyState { get; set; } public string PropertyPostalCode { get; set; } public string PropertyCountry { get; set; } public string PropertyPhone { get; set; } public string FAX { get; set; } public double PropertyLongitude { get; set; } public double PropertyLatitude { get; set; } public string PropertyAirport { get; set; } public string ReportedBy { get; set; } public DateTime ReportedDTS { get; set; } public string CanceledBy { get; set; } public DateTime CanceledDTS { get; set; } public string Comments { get; set; }
数据库人员告诉我这是Oracle在DataSet /游标中的返回:
RON_ID NOT NULL VARCHAR2(40) STAFF_NUM NOT NULL VARCHAR2(12) CHECKIN_DATE NOT NULL DATE NONMISSION_IND NOT NULL VARCHAR2(1) COMMENTS VARCHAR2(4000) NONCOMPLY_IND VARCHAR2(4000) PROPERTY_ID NOT NULL NUMBER(38) PROPERTY_CHAIN VARCHAR2(2) PROPERTY_NAME VARCHAR2(255) RON_AIRPORT NOT NULL VARCHAR2(3) PROPERTY_STREET VARCHAR2(255) PROPERTY_STREET2 VARCHAR2(255) PROPERTY_CITY VARCHAR2(255) PROPERTY_STATE VARCHAR2(3) PROPERTY_POSTALCODE VARCHAR2(255) PROPERTY_PHONE VARCHAR2(20) PROPERTY_FAX VARCHAR2(20) PROPERTY_COUNTRY VARCHAR2(2) PROPERTY_LONGITUDE NUMBER PROPERTY_LATITUDE NUMBER PROPERTY_AIRPORT VARCHAR2(3) REPORTED_BY VARCHAR2(50) REPORTED_DTS NOT NULL DATE CANCELED_BY VARCHAR2(50) CANCELED_DTS DATE
如何使用Nullable类型声明RON以及如何使用Nullable Dates进行操作?是否必须检查字符串?
要将值类型(例如DateTime)定义为Nullable,请执行以下操作之一:
DateTime? ReportedDTS
要么
NullableReportedDTS
引用类型(如字符串)已经"可以为空".
要从DataRow中获取可为空的值,您可以使用字段扩展方法:
DateTime? nullableDate = dataRow.Field("ColumnName");
这将自动将DBNull转换为null.
因此,对于您的示例,您可以执行以下操作:
select new RON { RONId = dr.Field("RON_ID"), // snip... CheckInDate = dr.Field ("CHECKIN_DATE"), // snip... };