Tag: dbnull

DBNull检查ExecuteScalar

命令的存储过程可以返回null。是否正确检查返回值是否为null或者是否还要检查obj是否为null? object obj = command.ExecuteScalar(); int id = -1; if (DBNull.Value == obj) { id = Convert.ToInt32(obj ); }

如何安全地将可空结果从sqlreader转换为int?

我有一个包含空值的表,我需要使用SqlDataReader从表中获取数据。 我无法弄清楚如何安全地将DBNull转换为int。 我现在正以这种方式做到这一点: … reader = command.ExecuteReader(); while (reader.Read()) { int y = (reader[“PublicationYear”] != null) ? Convert.ToInt32(reader[“PublicationYear”]) : 0; … } … 但是获取Object cannot be cast from DBNull to other types. 当PublicationYear为null时。 我怎样才能安全地获得价值? 谢谢。

处理DateTime DBNull

我已经在SO上看过很多很多这样的版本,但是它们似乎都不能满足我的需求。 我的数据来自供应商数据库,该数据库允许DateTime字段为null。 首先,我将数据拉入DataTable。 using (SqlCommand cmd = new SqlCommand(sb.ToString(), conn)) using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { da.Fill(dt); } 我正在将DataTable转换为List 进行处理。 var equipment = from i in dt.AsEnumerable() select new Equipment() { Id = i.Field(“ID”), BeginDate = i.Field(“BeginDate”), EndDate = i.Field(“EndDate”), EstimatedLife = i.Field(“EstimatedLife”) } 那么,在这个实例中如何检查DBNull? 我试着写一个方法。 public DateTime CheckDBNull(object dateTime) { if (dateTime == […]

如何管理解析DateTime的空对象以与ADO.NET一起用作DBNULL

我有两个DateTime对象,BirthDate和HireDate。 它们被正确格式化为字符串,当我将它们传递给我的数据访问层时,需要将它们解析为DateTime对象。 DateTime hD = DateTime.Parse(hire); DateTime bD = DateTime.Parse(birth); //incase of a datestring being passed through dateStringPassed = “7/2/1969″; 但有时候,字符串hire和birth是空的或空的”” ,如果代码是这样运行的,我从解析空字符串得到一个FormatException错误。 如何管理空分析并允许DateTime(如果为空或null)被接受为DBNull.Value ? 我仍然无法管理用户没有通过DateTime字符串,然后解析崩溃了我的代码。 我的出生日期参数如下,如果为null则检查变量,然后使用DBNull.Value。

将null转换为某些东西?

今天我和一位同事进行了这次有趣的讨论。 我们在C#中讨论两段代码。 代码片段1: if(!reader.IsDBNull(2)) { long? variable1 = reader.GetInt64(2) } 代码片段2: long variable1 = reader.IsDBNull(2) ? (long?) null : reader.GetInt64(2) 问题是:将null转换为可空的长度是一个好习惯吗? 或者你宁愿使用传统的if语句来避免将null为可空的long。

接线员’??’ 不能应用于’string’和’System.DBNull’类型的操作数

我有以下C#代码: sqlCommand.Parameters.AddWithValue(“@Parameter”, table.Value ?? DBNull.Value); 但它会抛出以下编译错误: 操作员?? 不能应用于string和System.DBNull类型的操作数 为什么编译器不允许这种语法?

C#数据库访问:DBNull与null

我们在这里使用自己的ORM,并为所有db表提供强类型包装器。 我们还允许执行弱类型的临时SQL,但这些查询仍然通过相同的类来从数据读取器中获取值。 在调整该类以使用Oracle时,我们遇到了一个有趣的问题。 使用DBNull.Value或null是否更好? 使用DBNull.Value有什么好处吗? 使用null似乎更“正确”,因为我们已经将自己从数据库世界中分离出来,但是有一些含义(例如,当值为null时,你不能盲目地使用ToString() )所以它绝对是我们需要的东西做出有意识的决定。

如何通过列的名称检查MySqlDataReader中的NULL?

如何在打开的MySqlDataReader检查NULL值? 以下不起作用; 它总是打击else : if (rdr.GetString(“timeOut”) == null) { queryResult.Egresstime = “Logged in”; } else { queryResult.Egresstime = rdr.GetString(“timeOut”); } rdr.IsDbNull(int i)只接受列号,而不是名称。

SqlDataReader检查空值的最佳方法-sqlDataReader.IsDBNull vs DBNull.Value

我想从数据库中检索十进制值,我想知道检查空值的推荐方法。 我在MSDN上看到过- DBNull.Value字段很少使用此检查。 因此,是reader.IsDBNull是否是检查空值的最佳/最有效方法? 我创建了2个示例方法: public static decimal? GetNullableDecimal(SqlDataReader reader, string fieldName) { if (reader[fieldName] == DBNull.Value) { return null; } return (decimal)reader[fieldName]; } public static decimal? GetNullableDecimal_2(SqlDataReader reader, string fieldName) { if (reader.IsDBNull(reader[fieldName])) { return null; } return (decimal)reader[fieldName]; } 大多数情况下,字段将为空。 提前致谢!

DbNull.Value和DbNull.Value.ToString()之间的区别

我想知道哪种用法是正确的? if(!string.IsNullOrEmpty(parentID)) cmd.Parameters.Add(new SqlParameter(“@ParentSesID”, parentID)); else cmd.Parameters.Add(new SqlParameter(“@ParentSesID”, DBNull.Value)); 要么 if(!string.IsNullOrEmpty(parentID)) cmd.Parameters.Add(new SqlParameter(“@ParentSesID”, parentID)); else cmd.Parameters.Add(new SqlParameter(“@ParentSesID”, DBNull.Value.ToString()));