Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8638545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:45:27+00:00 2026-06-12T10:45:27+00:00

Often use SQL Identity as the ID of .NET objects. Start the ID at

  • 0

Often use SQL Identity as the ID of .NET objects.
Start the ID at 0 or 1 as don’t want the objects to have negative IDs.
The ID is presented to users so it needs to be logical to humans.

Also often use something like ((Int32)key1 << 32) + key2; for hash
If unsigned can so with faster (UInt32)key1 << 32 | key2;

Since SQL does not have unsigned data type half the range is lost.

What is a good practice for converting from SQL signed to unsigned in .NET?

Where the convert goes from 0 to max.
A straight cast does not not work as (UInt16)Int16.Min = Int16.Max +1.
Need to convert Int16.Min to 0 and Int16.Max to UInt16.Max.

In SQL will set the Identity seed to minimum (or minimum +1) for the data type.

Currently have a situation where going to blow through Int32 max but doubt it will ever get past UInt32 max. If it does get past UInt32 max the app and database will need a full review.

Tried extensions to SqlDataReader and it seems to work.
Will post as an answer.
Not yet gone into production so checking SO for something better or warnings about the approach.

Don’t need DataTable compatibility as this app uses zero and never will.

We don’t use Entity Framework nor SQL LINQ. Grunge TSQL and SP.

public static class MyExtensions
{
    public static UInt16 GetUInt16(this SqlDataReader rdr, int i)
    {
        //return (UInt16)rdr.GetInt16(i);  // wrong answer
        Int16 int16 = rdr.GetInt16(i);
        UInt16 uint16 = (UInt16)(int16 + 32768);
        return uint16;
    }
    public static UInt32 GetUInt32(this SqlDataReader rdr, int i)
    {
        Int32 int32 = rdr.GetInt32(i);
        UInt32 uint32 = (UInt32)(int32 + 2147483648);
        return uint32;
    }
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:45:29+00:00Added an answer on June 12, 2026 at 10:45 am

    In the end what I did way make the object responsible for the translation.

    A read only signed property of sqlID.
    For updates back to the database decided that should not mess with that value.

    The objects also expose an unsigned ID that is used by other objects, hash, and UI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't use SQL Server Cursors often but when I do, I always have
I often use the net user command to have a look at AD groups
I have some e-commerce code that I use often that uses Linq To SQL
I quite often use Drupal's Views Module to build SQL that I paste into
I have a (dump) question regarding VB/C# I often use third party classes where
I often make small websites and use the built in ASP.NET membership functionality in
I quite often use the regular expression search and replace in SQL Server Management
This often comes up. I have a List and I want to go to
I have an error message that occurs every time I use SQL Server 2012
When writing a T-SQL script that I plan on re-running, often times I use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.