The correct way to reset database values

I am working with an older Oracle database, and I believe that is probably the best way to refuse to unpack the values ​​that I retrieve from the database.

I currently have a static class full of various types:

public static int? Int(object o)
{
    try
    {
        return (int?)Convert.ToInt32(o);
    }
    catch (Exception)
    {
        return null;
    }
}

.. etc. for different types, but I believe there should be a better way? If I want to remove a value, I do something line by line ...

int i;
i = nvl.Int(dataRow["column"]); //In this instance, "column" is of a numeric database type

I was thinking of using a generic class to handle all the different types, but I could not figure out how to do this.

Any ideas?

+3
source share
2 answers

, . DBNull , , :

public static MyHelper
{
    public static Nullable<T> ToNullable<T>(object value) where T : struct
    {
        if (value == null) return null;
        if (Convert.IsDBNull(value)) return null;
        return (T) value;
    }

    public static string ToString(object value)
    {
        if (value == null) return null;
        if (Convert.IsDBNull(value)) return null;
        return (string)value;
    }
}

, (int, decimal, double, bool, DateTime).

, , , . NUMERIC (), , int, :

int? myIntValue = (int?) MyHelper.ToNullable<decimal>(reader["MyNumericColumn"]);
+6

.

:

public class Customer
{
   public Customer(DataRow row) 
   {
      Name = row["Name"];
   }
   public Name { get; private set; }
}

, , .

, , NHM- ORM.

0

Source: https://habr.com/ru/post/1725980/


All Articles