dbnull

DBNull in non-empty cell when reading Excel file through OleDB

随声附和 提交于 2019-11-26 22:04:09
问题 I use OleDB to read an Excel file. One of the columns has a "Common" format and contains both strings with letters and values consisting of numbers only. String values are retrieved without problem, but pure numerical values are retrieved as DBNull . How to solve? I use the following connection string to open Excel 2003 file (xls): "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\file.xls; Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"" 回答1: I found some documentation on Microsoft's

DBNull if statement

时间秒杀一切 提交于 2019-11-26 14:25:52
问题 I'm trying to execute a stored procedure and then use an if statement to check for null values and I'm coming up short. I'm a VB guy so please bear with me if I'm making a schoolboy syntax error. objConn = new SqlConnection(strConnection); objConn.Open(); objCmd = new SqlCommand(strSQL, objConn); rsData = objCmd.ExecuteReader(); rsData.Read(); if (!(rsData["usr.ursrdaystime"].Equals(System.DBNull.Value))) { strLevel = rsData["usr.ursrdaystime"].ToString(); } Would this allow me to check

Most efficient way to check for DBNull and then assign to a variable?

☆樱花仙子☆ 提交于 2019-11-26 11:04:06
This question comes up occasionally, but I haven't seen a satisfactory answer. A typical pattern is (row is a DataRow ): if (row["value"] != DBNull.Value) { someObject.Member = row["value"]; } My first question is which is more efficient (I've flipped the condition): row["value"] == DBNull.Value; // Or row["value"] is DBNull; // Or row["value"].GetType() == typeof(DBNull) // Or... any suggestions? This indicates that .GetType() should be faster, but maybe the compiler knows a few tricks I don't? Second question, is it worth caching the value of row["value"] or does the compiler optimize the

What is the difference between null and System.DBNull.Value?

做~自己de王妃 提交于 2019-11-26 03:16:09
问题 Is there any difference between null and System.DBNull.Value? If yes, what is it? I noticed this behavior now - while (rdr.Read()) { if (rdr[\"Id\"] != null) //if (rdr[\"Id\"] != System.DBNull.Value) { int x = Convert.ToInt32(rdr[\"Id\"]); } } While I retrieve data from the database using a sql datareader, though there is no value returned if(rdr[\"Id\"] != null) returned true and eventually threw an exception for casting a null as integer. But, this if I use if (rdr[\"Id\"] != System.DBNull

Most efficient way to check for DBNull and then assign to a variable?

巧了我就是萌 提交于 2019-11-26 02:16:41
问题 This question comes up occasionally, but I haven\'t seen a satisfactory answer. A typical pattern is (row is a DataRow ): if (row[\"value\"] != DBNull.Value) { someObject.Member = row[\"value\"]; } My first question is which is more efficient (I\'ve flipped the condition): row[\"value\"] == DBNull.Value; // Or row[\"value\"] is DBNull; // Or row[\"value\"].GetType() == typeof(DBNull) // Or... any suggestions? This indicates that .GetType() should be faster, but maybe the compiler knows a few

What is the point of DBNull?

百般思念 提交于 2019-11-26 01:56:40
In .NET there is the null reference, which is used everywhere to denote that an object reference is empty, and then there is the DBNull , which is used by database drivers (and few others) to denote... pretty much the same thing. Naturally, this creates a lot of confusion and conversion routines have to be churned out, etc. So why did the original .NET authors decide to make this? To me it makes no sense. Their documentation makes no sense either: The DBNull class represents a nonexistent value. In a database, for example, a column in a row of a table might not contain any data whatsoever.

Assign null to a SqlParameter

岁酱吖の 提交于 2019-11-26 01:06:43
问题 The following code gives an error - \"No implicit conversion from DBnull to int.\" SqlParameter[] parameters = new SqlParameter[1]; SqlParameter planIndexParameter = new SqlParameter(\"@AgeIndex\", SqlDbType.Int); planIndexParameter.Value = (AgeItem.AgeIndex== null) ? DBNull.Value : AgeItem.AgeIndex; parameters[0] = planIndexParameter; 回答1: The problem is that the ?: operator cannot determine the return type because you are either returning an int value or a DBNull type value, which are not

What is the point of DBNull?

我是研究僧i 提交于 2019-11-26 00:59:57
问题 In .NET there is the null reference, which is used everywhere to denote that an object reference is empty, and then there is the DBNull , which is used by database drivers (and few others) to denote... pretty much the same thing. Naturally, this creates a lot of confusion and conversion routines have to be churned out, etc. So why did the original .NET authors decide to make this? To me it makes no sense. Their documentation makes no sense either: The DBNull class represents a nonexistent

Assign null to a SqlParameter

匆匆过客 提交于 2019-11-26 00:57:36
The following code gives an error - "No implicit conversion from DBnull to int." SqlParameter[] parameters = new SqlParameter[1]; SqlParameter planIndexParameter = new SqlParameter("@AgeIndex", SqlDbType.Int); planIndexParameter.Value = (AgeItem.AgeIndex== null) ? DBNull.Value : AgeItem.AgeIndex; parameters[0] = planIndexParameter; Chris Taylor The problem is that the ?: operator cannot determine the return type because you are either returning an int value or a DBNull type value, which are not compatible. You can of course cast the instance of AgeIndex to be type object which would satisfy