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

  • Home
  • SEARCH
  • 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 276725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:57:59+00:00 2026-05-12T00:57:59+00:00

Does anyone know the best way to create a SQL Server CE (Compact 3.5)

  • 0

Does anyone know the best way to create a SQL Server CE (Compact 3.5) table based on the schema of a DataTable at runtime? I don’t want to have to formulate a CREATE TABLE statement based on all the different possible datatypes, etc.

As a bonus – do you then know how to fill it directly from a datatable?

  • 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-12T00:58:00+00:00Added an answer on May 12, 2026 at 12:58 am

    I coded a reasonable solution, but was hoping to avoid case statements for the SQL types:

    Firstly a neat trick to convert from a .NET type to a SqlDBType:

    /// <summary>
    /// Gets the correct SqlDBType for a given .NET type. Useful for working with SQL CE.
    /// </summary>
    /// <param name="type">The .Net Type used to find the SqlDBType.</param>
    /// <returns>The correct SqlDbType for the .Net type passed in.</returns>
    public static SqlDbType GetSqlDBTypeFromType(Type type)
    {
        TypeConverter tc = TypeDescriptor.GetConverter(typeof(DbType));
        if (/*tc.CanConvertFrom(type)*/ true)
        {
            DbType dbType = (DbType)tc.ConvertFrom(type.Name);
            // A cheat, but the parameter class knows how to map between DbType and SqlDBType.
            SqlParameter param = new SqlParameter();
            param.DbType = dbType;
            return param.SqlDbType; // The parameter class did the conversion for us!!
        }
        else
        {
            throw new Exception("Cannot get SqlDbType from: " + type.Name);
        }
    }
    

    A case statement for the types for use in SQL Statements:

        /// <summary>
                /// The method gets the SQL CE type name for use in SQL Statements such as CREATE TABLE
                /// </summary>
                /// <param name="dbType">The SqlDbType to get the type name for</param>
                /// <param name="size">The size where applicable e.g. to create a nchar(n) type where n is the size passed in.</param>
                /// <returns>The SQL CE compatible type for use in SQL Statements</returns>
                public static string GetSqlServerCETypeName(SqlDbType dbType, int size)
                {
                    // Conversions according to: http://msdn.microsoft.com/en-us/library/ms173018.aspx
                    bool max = (size == int.MaxValue) ? true : false;
                    bool over4k = (size > 4000) ? true : false;
    
                    switch (dbType)
                    {
                        case SqlDbType.BigInt:
                            return "bigint";
                        case SqlDbType.Binary:
                            return string.Format("binary ({0})", size);
                        case SqlDbType.Bit:
                            return "bit";
                        case SqlDbType.Char:
                            if (over4k) return "ntext";
                            else return string.Format("nchar({0})", size);
    ETC...
    

    Then finally the CREATE TABLE statement:

        /// <summary>
        /// Genenerates a SQL CE compatible CREATE TABLE statement based on a schema obtained from
        /// a SqlDataReader or a SqlCeDataReader.
        /// </summary>
        /// <param name="tableName">The name of the table to be created.</param>
        /// <param name="schema">The schema returned from reader.GetSchemaTable().</param>
        /// <returns>The CREATE TABLE... Statement for the given schema.</returns>
        public static string GetCreateTableStatement(string tableName, DataTable schema)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(string.Format("CREATE TABLE [{0}] (\n", tableName));
    
            foreach (DataRow row in schema.Rows)
            {
                string typeName = row["DataType"].ToString();
                Type type = Type.GetType(typeName);
    
                string name = (string)row["ColumnName"];
                int size = (int)row["ColumnSize"];
    
                SqlDbType dbType = GetSqlDBTypeFromType(type);
    
                builder.Append(name);
                builder.Append(" ");
                builder.Append(GetSqlServerCETypeName(dbType, size));
                builder.Append(", ");
            }
    
            if (schema.Rows.Count > 0) builder.Length = builder.Length - 2;
    
            builder.Append("\n)");
            return builder.ToString();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 173k
  • Answers 173k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try in here. Customizing XML Comments May 12, 2026 at 2:37 pm
  • Editorial Team
    Editorial Team added an answer I highly recommend reading http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/04/24/how-we-do-mvc.aspx Here's my incomplete list of… May 12, 2026 at 2:37 pm
  • Editorial Team
    Editorial Team added an answer Update: this answer is out of date in 2011. Unless… May 12, 2026 at 2:37 pm

Related Questions

I am writing a program that generates a single large table of information. The
I'm having a problem with some T-SQL in a SP on SQLServer 2005 comparing
I am trying to write a test and need to create a Message object
I'm looking for a way to insert a <style> tag into an HTML page

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.