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!
Given some abstract
CsvReader:Your
ColumnGuess:TableGuesswould contain the guessed columns and the rows:You could add to
TableGuessanAsDataTable()method:You could use an
SqlCeDataAdapterto move the data in theDataTable(after adding the table itself to the database).