Miner.Data.Access Assembly > Miner.Data.Access Namespace : DbConvert Class |
Miner.Data.Access.dll
'Declaration Public NotInheritable Class DbConvert
'Usage Dim instance As DbConvert
public sealed class DbConvert
When working with nullable types, such as any value stored in a object (i.e. a boxed value) you have to handle the situation where the value is null or DBNull. Often times it is much more convenient to convert a null value to some default; especially when the value will not be written back to the database.
The example below shows how to use the DbConvert helper methods. The code is cleaner and easier to understand than the later example that explicitly handles DBNull values.
public void DoSomethingIfStatusIsOne(DataRow row) { int status = DbConvert.ToInt32(row["status"], 0); if (status == 1) { DoSomething(); } }
public void DoSomethingIfStatusIsOne(DataRow row) { object status = row["status"]; if ((status != DBNull.Value) && ((decimal)status == 1)) { DoSomething(); } }
The status column is most likely a decimal value, not an integer because of the way Oracle and SqlServer store numbers in the database. Casting the status value to an int would lead to a runtime exception. The alternatives are to use Convert.ToInt32 or cast to int.System.Object
Miner.Data.Access.DbConvert
Target Platforms: Windows XP SP3 (32-bit and 64-bit), Windows 7 (32-bit and 64-bit)
Not all Operating Systems are supported on all products. Visit the ArcFM Solution Supported Versions page for full details.