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

The Archive Base Latest Questions

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

I’ve got to write a horrible interface to import data into a new database

  • 0

I’ve got to write a horrible interface to import data into a new database from hundreds of data files from our old application that has everything hard coded (the data displayed resemble Excel spreadsheets, and it allows us to export the data to Comma Delimited Values).

I can read it all in, with the header names.

From that, I can generate a name for the column to be used in a Sql CE Database.

The data currently consists of float, int, DateTime, bit, char and string.

I’ve come up with a way to do this (untested on all of our data), but any help from someone that knows how to code this better would be greatly appreciated.

The code below is not necessary to read through unless someone just doesn’t understand what I’m asking.

public enum MyParameterType { NA, Float, Bool, Char, Date, Int, String }

class MyParameter {

public MyParameter(string name, string value) {
  if (String.IsNullOrEmpty(name) || String.IsNullOrEmpty(value)) {
    throw new NotSupportedException("NULL values are not allowed.");
  }
  Name = name.Trim();
  Value = value.Trim();
  Type = MyParameterType.NA;
  if (-1 < Value.IndexOf('.')) { // try float
    float f;
    if (float.TryParse(Value, out f)) {
      string s = f.ToString();
      if (s == Value) {
        Parameter = new SqlCeParameter(AtName, SqlDbType.Float) { Value = f };
        Type = MyParameterType.Float;
      }
    }
  }
  if (Type == MyParameterType.NA) {
    bool b;
    if (bool.TryParse(Value, out b)) {
      Parameter = new SqlCeParameter(AtName, SqlDbType.Bit) { Value = b };
      Type = MyParameterType.Bool;
    }
  }
  if (Type == MyParameterType.NA) {
    if (Value.Length == 1) {
      char c = Value[0];
      Parameter = new SqlCeParameter(AtName, SqlDbType.Char) { Value = c };
      Type = MyParameterType.Char;
    }
  }
  if (Type == MyParameterType.NA) {
    DateTime date;
    if (DateTime.TryParse(Value, out date)) {
      Parameter = new SqlCeParameter(AtName, SqlDbType.DateTime) { Value = date };
      Type = MyParameterType.Date;
    }
  }
  if (Type == MyParameterType.NA) {
    if (50 < Value.Length) {
      Value = Value.Substring(0, 49);
    }
    Parameter = new SqlCeParameter(AtName, SqlDbType.NVarChar, 50) { Value = this.Value };
    Type = MyParameterType.String;
  }
}

public string AtName { get { return "@" + Name; } }

public string Name { get; set; }

public MyParameterType Type { get; set; }

public SqlCeParameter Parameter { get; set; }

public string Value { get; set; }

}

My biggest concern is that I don’t want to mistakenly interpret one of the inputs (like a boolean value to be a char).

I am also looking for a way to compare new instances of MyParameter (i.e. if it is less than one type, try another type).

Bonus points for seeing some cool new expressions to generate this!

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

    Given some abstract CsvReader:

    using (var reader = new CsvReader(file))
    {
        TableGuess table = new TableGuess { Name = file };
    
        // given: IEnumerable<string> CsvReader.Header { get; }
        table.AddColumns(reader.Header);
    
        string[] parts;
        while (null != (parts = reader.ReadLine()))
        {
            table.AddRow(parts);
        }
    }
    

    Your ColumnGuess:

    class ColumnGuess
    {
        public string Name { get; set; }
        public Type Type { get; set; }
        public int Samples { get; private set; }
    
        public void ImproveType(string value)
        {
            if (this.Samples > 10) return;
            this.Samples++;
    
            float f; bool b; DateTime d; int i;
            if (Single.TryParse(value, out f))
            {
                this.Type = typeof(float);
            }
            else if (Boolean.TryParse(value, out b))
            {
                this.Type = typeof(bool);
            }
            else if (DateTime.TryParse(value, out d))
            {
                this.Type = typeof(DateTime);
            }
            else if (value.Length == 1 && this.Type == null && !Char.IsDigit(value[0]))
            {
                this.Type = typeof(char);
            }
            else if (this.Type != typeof(float) && Int32.TryParse(value, out i))
            {
                this.Type = typeof(int);
            }
        }
    }
    

    TableGuess would contain the guessed columns and the rows:

    class TableGuess
    {
        private List<string[]> rows = new List<string[]>();
        private List<ColumnGuess> columns;
    
        public string Name { get; set; }
    
        public void AddColumns(IEnumerable<string> columns)
        {
            this.columns = columns.Select(cc => new ColumnGuess { Name = cc })
                                  .ToList();
        }
    
        public void AddRow(string[] parts)
        {
            for (int ii = 0; ii < parts.Length; ++ii)
            {
                if (String.IsNullOrEmpty(parts[ii])) continue;
                columns[ii].ImproveType(parts[ii]);
            }
    
            this.rows.Add(parts);
        }
    }
    

    You could add to TableGuess an AsDataTable() method:

    public DataTable AsDataTable()
    {
        var dataTable = new dataTable(this.Name);
        foreach (var column in this.columns)
        {
            dataTable.Columns.Add(new DataColumn(
                column.Name,
                column.Type ?? typeof(string)));
        }
    
        foreach (var row in this.rows)
        {
            object[] values = new object[dataTable.Columns.Count];
            for (int cc = 0; cc < row.Length; ++cc)
            {
                values[cc] = Convert.ChangeType(row[cc],
                    dataTable.Columns[cc].DataType);
            }
    
            dataTable.LoadRow(values, false);
        }
    
        return dataTable;
    }
    

    You could use an SqlCeDataAdapter to move the data in the DataTable (after adding the table itself to the database).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a bunch of posts stored in text files formatted in yaml/textile (from
i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
I have a JSP page retrieving data and when single or double quotes are
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.