Miner.Data.Access Namespace : DbConvert Class |
'Declaration
<System.Diagnostics.DebuggerStepThroughAttribute()> Public MustInherit NotInheritable Class DbConvert
'Usage
Dim instance As DbConvert
[System.Diagnostics.DebuggerStepThrough()] public static class DbConvert
public class DbConvert abstract; sealed;
System.Diagnostics.DebuggerStepThroughAttribute() public abstract sealed class DbConvert
[System.Diagnostics.DebuggerStepThrough()] public __gc abstract __sealed class DbConvert
[System.Diagnostics.DebuggerStepThrough()] public ref class DbConvert abstract sealed
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 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2