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 328723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:31:18+00:00 2026-05-12T09:31:18+00:00

I am writing a stored procedure generator and I need to map CLR types

  • 0

I am writing a stored procedure generator and I need to map CLR types to their SQL Server types.

MSDN lists the type mappings at: http://msdn.microsoft.com/en-us/library/ms131092.aspx but I don’t want to use a big switch statement to handle the mappings.

Is there a simple way to retrieve the SQL Server type as a string using whatever process is used by System.Data.SqlTypes?

I’d like a method signature like so:

static string GetSqlType(Type clrType)
{
    ...
    return sqlType;
}

So given the following call:

string sqlType = GetSqlType(1.GetType());

sqlType should contain: “int”.

  • 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-05-12T09:31:19+00:00Added an answer on May 12, 2026 at 9:31 am

    This is made rather more complex since it depends which version of sql server you are willing to target.

    • With a 2008 target you would probably want instances of DateTime to map to the improved datetime2 rather than datetime.
      • TimeSpan is even worse as your fallback option is using DateTime or varchar.

    Here is a guide to how sql server will deal with datatypes in replication scenarios

    SQL Server Compact Edition adds further confusion as it doesn’t support the varchar(max), varbinary(max) column types (you are limited to explicit ~4K length columns).

    At some point you will also need to make a heuristic decision on areas with multiple options.

    • textual information may be varchar(n) or varchar(max).
      • or XML from 2005 onwards
      • or TEXT but that is deprecated
    • decimal will map nicely to numeric for input but is risky on output as numeric has a greater scale
    • should bitmaps be image or varbinary?
      • again image is deprecated but this may annoy people still using it.
    • Unsigned integers
      • uint can be safely put into bigint
      • ulong is rather more problematic
      • bigints will be even more fun.

    Given all this going with a nice simple switch statement in a utility function will make life much easier than attempting to rely on some opaque BCL library intended only for type translation rather than textual sql creation.
    It also makes it clear via a default throw or default varchar(max) what your ‘not yet defined’ behaviour will be which will remain under your control.

    Returning a simple immutable class from the method of the form:

    public sealed class SqlServerTypeDescription
    {
        // for sql text construction
        public readonly string SqlServerName;
        // for code construction
        public readonly SqlDbType SqlDbType;
        // for code construction
        public readonly Type ClrType;
    
        // constructor etc.
    }
    

    You may want to add optional precision/size value too though that might be something you choose to leave to the user.

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

Sidebar

Related Questions

I am writing a Stored procedure in SQL Server 2008. I need to check
I am writing a stored procedure in SQL Server 2008. Here I have a
I am writing a stored procedure in MySQL Community Server 5.5.16 and I need
I'm writing a stored procedure on SQL Server 2000. I've written a complicated select
I'm writing a stored procedure in SQL Server 2008, interatively. When using SQL Server
I'm writing a stored procedure in SQL Server 2008. It's a really long query
Writing a stored procedure in MS SQL Server 2008 R2, I want to avoid
I am writing a stored procedure in SQL Server Management Studio 2005 to return
I'm writing a stored procedure where I need to dynamically construct a SQL statement
I am writing a stored procedure in PlPython with a user defined type. I

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.