Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# : return dbnull.value or system.type from function?

I want to create a function (c#):

int PutInt (int? x)
{
    if (x.HasValue)
        return x.Value;
    else
        return DBNull.Value;
}

but there is no cast from int to DBNull.Value. Is there any way to create such a function ?

like image 778
dilix Avatar asked Dec 28 '25 16:12

dilix


1 Answers

Lets assume your DBMS is SQLServer, you can transform your function to

object PutInt (int? x)
{
    if (x.HasValue)
        return (object)x.Value;
    else
        return (object)DBNull.Value;
}

Because SQLParamater Value property assumes type object. Hope I answer your question.

like image 200
FIre Panda Avatar answered Dec 30 '25 04:12

FIre Panda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!