dbnull

What should I use to compare DBNull ? Using DBNull.Value or ToString().IsNullOrEmpty()

泪湿孤枕 提交于 2019-11-28 02:29:44
问题 I can check for a DBnull on a data row using any of the methods. Either by using if(dr[0][0]==DBNull.Value) //do somethin or by doing if(dr[0][0].ToString().IsNullOrEmpty()) //do something In Both Cases I will be getting same result. But Which one is conecptually right approach. Which was will use less resources 回答1: The first way is somewhat correct. However, more accepted way is: if ( dr[0][0] is DBNull ) And the second way is definitely incorrect. If you use the second way, you will get

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

青春壹個敷衍的年華 提交于 2019-11-28 02:00:58
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\"" I found some documentation on Microsoft's website that applies to your scenario. They recommend converting all the numerics to strings. http://support

C# ADO.NET: nulls and DbNull — is there more efficient syntax?

血红的双手。 提交于 2019-11-27 11:53:08
问题 I've got a DateTime? that I'm trying to insert into a field using a DbParameter . I'm creating the parameter like so: DbParameter datePrm = updateStmt.CreateParameter(); datePrm.ParameterName = "@change_date"; And then I want to put the value of the DateTime? into the dataPrm.Value while accounting for null s. I thought initially I'd be clever: datePrm.Value = nullableDate ?? DBNull.Value; but that fails with the error Operator '??' cannot be applied to operands of type 'System.DateTime?' and

DBNull if statement

徘徊边缘 提交于 2019-11-27 09:00:06
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 whether the SQL connection is returning just a value and if so then populating my string? I'm used to being

ExecuteScalar returns null or DBNull (development or production server)

走远了吗. 提交于 2019-11-27 05:59:35
问题 I'm trying to add a column to an existing DataRow in C#. Afterwards the column will be filled with a single value from my database. DataRow dr already exists and column "COLNAME" also exists. comTBP is my SqlCommand . dr["COLNAME"] = Convert.ToInt32(comTBP.ExecuteScalar()); This all works fine if there is a value in my database and ExecuteScalar() can get that value. If I test this code on my development server (local) it also works if ExecuteScalar() return null or DBNull and the value of my

Dealing with System.DBNull in PowerShell

扶醉桌前 提交于 2019-11-27 05:59:17
问题 EDIT: As of PowerShell 7 Preview 2, -not [System.DBNull]::Value evaluates to $true , thanks to Joel Sallow via pull request 9794 Spending more time pulling SQL data in PowerShell. Running into issues with [System.DBNull]::Value and how PowerShell behaves with this during comparisons. Here's an example of the behavior I see, along with workarounds #DBNull values don't evaluate like Null... if([System.DBNull]::Value){"I would not expect this to display"} # The text displays. if([string][System

Handle DBNull in C#

有些话、适合烂在心里 提交于 2019-11-27 03:38:51
Is there a better/cleaner way to do this? int stockvalue = 0; if (!Convert.IsDBNull(reader["StockValue"])) stockvalue = (int)reader["StockValue"]; The shortest (IMHO) is: int stockvalue = (reader["StockValue"] as int?) ?? 0; Explanation: If reader["StockValue"] is of type int , the value will be returned, and the "??" operator will return the result If reader["StockValue"] is NOT of type int (e.g. DBNull), null will be returned, and the "??" operator will return the value 0 (zero). Chris Marisic The way I handle this is int? stockvalue = reader["StockValue"] as int?; Very simple, clean and one

SQLite equivalent to ISNULL(), NVL(), IFNULL() or COALESCE()

随声附和 提交于 2019-11-27 03:36:57
I'd like to avoid having many checks like the following in my code: myObj.someStringField = rdr.IsDBNull(someOrdinal) ? string.Empty : rdr.GetString(someOrdinal); I figured I could just have my query take care of the nulls by doing something like this: SELECT myField1, [isnull](myField1, '') FROM myTable1 WHERE myField1 = someCondition I'm using SQLite though and it doesn't seem to recognize the isnull function. I've also tried some equivalent ones recognized in other databases ( NVL() , IFNULL() and COALESCE() ), but SQLite doesn't seem to recognize any of them. Does anyone have any

Why is this code invalid in C#?

亡梦爱人 提交于 2019-11-26 22:12:44
The following code will not compile: string foo = "bar"; Object o = foo == null ? DBNull.Value : foo; I get: Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string' To fix this, I must do something like this: string foo = "bar"; Object o = foo == null ? DBNull.Value : (Object)foo; This cast seems pointless as this is certainly legal: string foo = "bar"; Object o = foo == null ? "gork" : foo; It seems to me that when the ternary branches are of different types, the compiler will not autobox the values to the type

mysqli prepared statements, insert NULL using bind params

拟墨画扇 提交于 2019-11-26 22:05:49
问题 Does anyone know if it is possible to insert NULL into a column with MYSQLI bind_param. I have a situation where sometimes I want to set a column to null in bind_param. Like so... $column2 = "NULL"; $insert_data->bind_param('ss', $column1,$column2); Obviously, this just writes NULL to the column as a string. I've tried null and \0 but they don't work. I would like to know if there is some value I can make $column2 equal that will cause it to submit and actual null value instead. Otherwise, I