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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:02:47+00:00 2026-06-18T10:02:47+00:00

I am having trouble determining the best way to do this. We are maintaining

  • 0

I am having trouble determining the best way to do this. We are maintaining a complex DB which the average business analyst will have a lot of trouble dealing with. Another developer created a couple of stored procedures as reports which we had been exporting for the users on demand into excel spreadsheets.

The client requested that these results be exported into an access database.

If this were 2 or 3 columns, I would just create a “template” accdb file and shove the results of the stored procedure in there. This query, however, brings back over 100 columns right now, and knowing the clients, it will be expanded in the future. So I am trying to create the table on the fly from the result set…whatever it is.

I /think/ the right way to do this is with a SqlDataReader but I’m stuck on how to get the proper “SQL”-ish field types. My current code looks like:

static void CreateAccessTableFromReader(string tableName, SqlDataReader reader) {
  List<string> columns = new List<string>();
  string createTable = @"CREATE TABLE {0} ({1})";

  int fieldCount = reader.FieldCount;
  for (int i = 0; i < fieldCount; i++) {
    columns.Add(String.Format("[{0}] {1}({2})",
      //column name (this seems to be right)
      reader.GetName(i), 
      //column type __(this is wrong.. I want 'VARCHAR, INT, etc')__
      reader.GetProviderSpecificFieldType(i),
      //column size (e.g. the numeric part of VARCHAR(15))
      reader.???
    ));
    //right now I'm getting results from the above line like:
    //[ColumnName] System.Data.SqlTypes.SqlString
    //and I'm looking for something like:
    //[ColumnName] VARCHAR(15)
  }
  using (var cmd = conn_Access.CreateCommand()) {
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = String.Format(createTable, tableName, String.Join(", ", columns));
    cmd.ExecuteNonQuery();
  }
}

Worth noting:

  • The only types that will exist should be exact match between MSSQL and Access according to this link : http://msdn.microsoft.com/en-us/library/bb177899.aspx

EDIT:

This is my “final solution”. The comments suggesting using a linked server certainly would have been an easier approach, but there was one thing preventing me from doing that which didn’t occur to me at first, the data needs to exist in the Access DB as the users may consume the data away from where they would have network access to the underlying DB. As such, I kept banging away at this approach and here’s what I came up with:

Note that I’d still be open to suggestions for a “better way”, but this solves my current problem

static void CreateTableFromReader(string tableName, SqlDataReader reader) {
  List<string> columns = new List<string>();
  string createTable = @"CREATE TABLE {0} ({1})";

  var dt = reader.GetSchemaTable();
  foreach (DataRow dr in dt.Rows) {
    switch (dr["DataTypeName"].ToString().ToLower()) {
      case "varchar":
        columns.Add(String.Format("[{0}] {1}({2})",
          dr["ColumnName"],
          dr["DataTypeName"],
          dr["ColumnSize"]
        ));
        break;
      case "money": //i know this is redundant but being explicit helps clarity sometimes
      case "date":
      case "integer":
      default:
        columns.Add(String.Format("[{0}] {1}",
          dr["ColumnName"],
          dr["DataTypeName"]
        ));
        break;
    }
  }
  using (var cmd = conn_Access.CreateCommand()) {
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = String.Format(createTable, tableName, String.Join(", ", columns));
    cmd.ExecuteNonQuery();
  }
}
  • 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-18T10:02:48+00:00Added an answer on June 18, 2026 at 10:02 am

    This is my “final solution”. The comments suggesting using a linked server certainly would have been an easier approach, but there was one thing preventing me from doing that which didn’t occur to me at first, the data needs to exist in the Access DB as the users may consume the data away from where they would have network access to the underlying DB. As such, I kept banging away at this approach and here’s what I came up with:

    Note that I’d still be open to suggestions for a “better way”, but this solves my current problem

    static void CreateTableFromReader(string tableName, SqlDataReader reader) {
      List<string> columns = new List<string>();
      string createTable = @"CREATE TABLE {0} ({1})";
    
      var dt = reader.GetSchemaTable();
      foreach (DataRow dr in dt.Rows) {
        switch (dr["DataTypeName"].ToString().ToLower()) {
          case "varchar":
            columns.Add(String.Format("[{0}] {1}({2})",
              dr["ColumnName"],
              dr["DataTypeName"],
              dr["ColumnSize"]
            ));
            break;
          case "money": //i know this is redundant but being explicit helps clarity sometimes
          case "date":
          case "integer":
          default:
            columns.Add(String.Format("[{0}] {1}",
              dr["ColumnName"],
              dr["DataTypeName"]
            ));
            break;
        }
      }
      using (var cmd = conn_Access.CreateCommand()) {
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = String.Format(createTable, tableName, String.Join(", ", columns));
        cmd.ExecuteNonQuery();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble determining the best way to read some input in for a
I have a site which uses HTML5 App Cache and I'm having trouble determining
I'm building a sample app for practice and am having trouble determining the best
I'm having trouble determining a way to parse a given text file. Here is
I'm having trouble determining from this research paper exactly how I can reproduce the
I'm having some trouble determining the correct context for a node set. I have
I'm having trouble determining if it's possible, based on the amount of access I
After my page loads my text disappears and I am having trouble determining why.
I am having some trouble determining if two line segments are collinear because of
Having trouble with each function... Will try to explain by example... In my code,

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.