Mono Class Library: System NamespaceSystem.DBNull Class |
See Also: DBNull Members
|
When a SQL query returns null, it is converted to a System.DBNull value, specifically the DBNull.Value instance, which is the only instance of the type ever used. Thus, one may compare values t DBNull using reference equality to DBNull.Value (== DBNull.Value). You then do not need to create a new instance of this type.
You can convert this to a blank string by using Convert.ToString(). See example below.
[Edit]
C# Example SqlConnection dbConn = new SqlConnection("server=myserver;database=mydb;uid=myuserid;pwd=mypassword"); dbConn.Open(); SqlCommand command = new SqlCommand("select something that returns a null value", dbConn); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { // if it's a DBNull, myString will contain a blank String string myString = Convert.ToString(reader.GetValue(0)); // if you prefer to check for the DBNull try this object possibleNull = reader.GetValue(0); if (possibleNull == DBNull.Value) { // do something } } reader.Close(); dbConn.Close();
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0