0
h_header        data type                           cp_retirementPlanningAnalysis        data type
versionId       int                                 signifShortFall                      varchar
serviceId       varchar(10)                         isProjIncAdeq                        boolean
creationTime    date                                howCloseRetire                       varchar
brandCode       varchar                             clientsView                          varchar

My excel file above has a header which i want to be the table name, then the column names below with the data type next to it. Is there any easy way i can generate the tables from each column, plus the properties with datatypes? I have around 70 tables that need to be created.

5
  • I'd use save as .csv and then Notepad++ macros for constructing the queries. Commented Feb 26, 2014 at 15:32
  • are there pre-made macros for this particular job? Commented Feb 26, 2014 at 15:37
  • You don't have to write the macros in Notepad++ You just push the start and it recording your actions, until you push stop. Then you run it as many times as you want. Commented Feb 26, 2014 at 15:40
  • I have no idea what i'm supposed to do once you press record.. Commented Feb 26, 2014 at 15:48
  • @MarcHoward Have you seen my solution? Commented Feb 27, 2014 at 21:23

1 Answer 1

1

Rather than reading directly from Excel, you should be able to easily reformat your Excel tables to two columns instead of four. Then you could save this as a CSV file and manually build the query in some way similar to the following:

using System;
using System.IO;

namespace sample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Read all lines from csv file describing single table
            string query = "CREATE TABLE ";
            using (StreamReader sr = new StreamReader(File.OpenRead(@"C:\test\test.csv"))){
                // Skip the first line
                sr.ReadLine();
                // Get the header from the second line
                query += sr.ReadLine().Split(',')[0] + '(';
                while (!sr.EndOfStream)
                {
                    // Add parameters to query
                    string[] line = sr.ReadLine().Split(',');
                    query += line[0] + ' ' + line[1] + ',';
                }
                query = query.TrimEnd(',');
                query += ");";
                Console.WriteLine(query);
            }
        }
    }
}

This is just an example of an easy way to do it without creating a connection to Excel. However, you could also connect this directly to your Excel file to loop through each sheet, or use Linq to loop through this, etc.

With a CSV file of the following text:

h_header,data type
cp_retirementPlanningAnalysis,data type
versionId,int
signifShortFall,varchar
serviceId,varchar(10)
isProjIncAdeq,boolean
creationTime,date
howCloseRetire,varchar
brandCode,varchar
clientsView,varchar

The query created is:

CREATE TABLE cp_retirementPlanningAnalysis(versionId int,signifShortFall varchar,serviceId varchar(10),isProjIncAdeq boolean,creationTime date,howCloseRetire varchar,brandCode varchar,clientsView varchar);

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

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.