1

I stumbled across this thing today:

I have a little helper method with the following:

private static T GetValOrDefault<T>(this IDataRecord rdr, string name)
{
    return rdr[name] is T ? (T) rdr[name] : default(T);
}

and all my models use public long Id { get; set; }

SQL server columns are BIGINT

Somehow the GetValOrDefault<long>("Id") returns '0', and i went ahead and used the Immediate Window there, and had a look at rdr["Id"].GetType()

It was Int32... any ideas why that happens? Everywhere i look, it says BIGINT = INT64 = long... and somehow the SqlDataReader gives me int32...

Edit:
That is my Sql Query:

    SELECT
        Id,
        Created,
        CreatedById,
        LastModified,
        LastModifiedById,
        Deleted,
        DeletedById
    FROM dbo.MyTable
    WHERE Id = @id

2 Answers 2

2

A BIGINT column should be returned as a long. I suspect a bug in your code - perhaps you're not getting the column you think you are, from the database you think you are using, or perhaps your SQL query is casting the column value.

Sign up to request clarification or add additional context in comments.

1 Comment

you were right, i used a temp table with DECLARE @temp AS TABLE(Id int
1

your can use Convert.ChnageType implement

public T Change<T>(IDataReader rd,string fieldName)
{
    return (T)Convert.ChangeType(rd[fieldName], typeof(T));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.