Why is the column nullable if you require a default value to be returned?

-----Ursprüngliche Nachricht-----
Von: Random Coder [mailto:random.co...@gmail.com]
Gesendet: Dienstag, 15. Juli 2014 03:50
An: General Discussion of SQLite Database
Betreff: Re: [sqlite] Preferred cast in C#

Could you not do something like this to handle the nullable types?

    T GetValue<T>(string field)
    {
        object obj = reader[field];

        if (obj is System.DBNull)
            return default(T);
        else
            return (T)obj;
    }

Assuming the type is nullable, it should do the right thing, and if it's an 
unexpected type, it'll throw an exception when casting to T.



On Mon, Jul 14, 2014 at 4:07 PM, Edward Ned Harvey (sqlite) < 
sql...@nedharvey.com> wrote:

> I understand there are only 5 data types in Sqlite, and that the
> column type isn't necessarily the type of object returned in a query.
> Is there a more seamless way to cast responses than this?
>
> I would really love to have an easy way of putting a long? into the
> database, and then getting a long? back out.  Maybe it exists and I'm
> just doing it the hard way right now...
>
>     string employeeName;
>
>     object myObj = reader["employeeName"];
>     if (myObj is System.DBNull)
>         employeeName = null;
>     else if (myObj is string)
>         employeeName = (string)myObj;
>     else
>         throw new Exception("Unexpected object type");
> _______________________________________________
> sqlite-users mailing list
> sqlite-users@sqlite.org
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


-----------------------------------------------------------------------
Gunter Hick
Software Engineer

Scientific Games International GmbH
Klitschgasse 2 – 4, A - 1130 Vienna,
Austria
FN 157284 a, HG Wien
Tel: +43 1 80100 0
E-Mail: h...@scigames.at

This e-mail is confidential and may well also be legally privileged. If you 
have received it in error, you are on notice as to its status and accordingly 
please notify us immediately by reply e-mail and then
delete this message from your system. Please do not copy it or use it for any 
purposes, or disclose its contents to any person as to do so could be a breach 
of confidence. Thank you for your cooperation.
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to