How to Fill a DataSet by using multiple delimited text files
I work on C# .My input file look likes :
d00 d04 WinMain
d00 d04 lpCmdLine: '/UNREGSERVER'
d00 d04 Run
d00 d04
lpCmdLine: '/UNREGSERVER'
d00 d04 nCmdShow: 10
d00 d04 leaving WinMain
CustomerID CompanyName ContactName ContactTitle Address City Region PostalCode Country Phone Fax
ALFKI Alfreds Futterkiste Maria Anders Sales Representative Obere Str. 57 Berlin NULL 12209 Germany 030-0074321 030-0076545
ANATR Ana Trujillo Emparedados y helados Ana Trujillo Owner Avda. de la Constitución 2222 México D.F. NULL 05021 Mexico (5) 555-4729 (5) 555-3745
ANTON Antonio Moreno Taquería Antonio Moreno Owner Mataderos 2312 México D.F. NULL 05023 Mexico (5) 555-3932 NULL
AROUT Around the Horn Thomas Hardy Sales Representative 120 Hanover Sq. London NULL WA1 1DP UK (171) 555-7788 (171) 555-6750
BERGS Berglunds snabbköp Christina Berglund Order Administrator Berguvsvägen 8 Luleå NULL S-958 22 Sweden 0921-12 34 65 0921-12 34 67
BLAUS Blauer See Delikatessen Hanna Moos Sales Representative Forsterstr. 57 Mannheim NULL 68306 Germany 0621-08460 0621-08924
BLONP Blondesddsl père et fils Frédérique Citeaux Marketing Manager 24, place Kléber Strasbourg NULL 67000 France 88.60.15.31 88.60.15.32
BOLID Bólido Comidas preparadas Martín Sommer Owner C/ Araquil, 67 Madrid NULL 28023 Spain (91) 555 22 82 (91) 555 91 99
BONAP Bon app' Laurence Lebihan Owner 12, rue des Bouche
d00 d04 WinMain
d00 d04 lpCmdLine: '/UNREGSERVER'
d00 d04 Run
d00 d04
lpCmdLine: '/UNREGSERVER'
d00 d04 nCmdShow: 10
d00 d04 leaving WinMain
CustomerID CompanyName ContactName ContactTitle Address City Region PostalCode Country Phone Fax
ALFKI Alfreds Futterkiste Maria Anders Sales Representative Obere Str. 57 Berlin NULL 12209 Germany 030-0074321 030-0076545
ANATR Ana Trujillo Emparedados y helados Ana Trujillo Owner Avda. de la Constitución 2222 México D.F. NULL 05021 Mexico (5) 555-4729 (5) 555-3745
ANTON Antonio Moreno Taquería Antonio Moreno Owner Mataderos 2312 México D.F. NULL 05023 Mexico (5) 555-3932 NULL
AROUT Around the Horn Thomas Hardy Sales Representative 120 Hanover Sq. London NULL WA1 1DP UK (171) 555-7788 (171) 555-6750
BERGS Berglunds snabbköp Christina Berglund Order Administrator Berguvsvägen 8 Luleå NULL S-958 22 Sweden 0921-12 34 65 0921-12 34 67
BLAUS Blauer See Delikatessen Hanna Moos Sales Representative Forsterstr. 57 Mannheim NULL 68306 Germany 0621-08460 0621-08924
BLONP Blondesddsl père et fils Frédérique Citeaux Marketing Manager 24, place Kléber Strasbourg NULL 67000 France 88.60.15.31 88.60.15.32
BOLID Bólido Comidas preparadas Martín Sommer Owner C/ Araquil, 67 Madrid NULL 28023 Spain (91) 555 22 82 (91) 555 91 99
BONAP Bon app' Laurence Lebihan Owner 12, rue des Bouche
I want to save this text files to *dataset.*How to do that.I write the bellow code to doing this.
DataSet ds = TextToDataSet.Convert(@"C:\readme.txt", "MyNewTable", "\t");
public static DataSet Convert(string File,
string TableName, string delimiter)
{
//The DataSet to Return
DataSet result = new DataSet();
//Open the file in a stream reader.
StreamReader s = new StreamReader(File);
//Split the first line into the columns
string[] columns = s.ReadLine().Split(delimiter.ToCharArray());
//Add the new DataTable to the RecordSet
result.Tables.Add(TableName);
//Cycle the colums, adding those that don't exist yet
//and sequencing the one that do.
foreach (string col in columns)
{
bool added = false;
string next = "";
int i = 0;
while (!added)
{
//Build the column name and remove any unwan开发者_高级运维ted characters.
string columnname = col + next;
columnname = columnname.Replace("#", "");
columnname = columnname.Replace("'", "");
columnname = columnname.Replace("&", "");
//See if the column already exists
if (!result.Tables[TableName].Columns.Contains(columnname))
{
//if it doesn't then we add it here and mark it as added
result.Tables[TableName].Columns.Add(columnname);
added = true;
}
else
{
//if it did exist then we increment the sequencer and try again.
i++;
next = "_" + i.ToString();
}
}
}
//Read the rest of the data in the file.
string AllData = s.ReadToEnd();
//Split off each row at the Carriage Return/Line Feed
//Default line ending in most windows exports.
//You may have to edit this to match your particular file.
//This will work for Excel, Access, etc. default exports.
string[] rows = AllData.Split("\r\n".ToCharArray());
//Now add each row to the DataSet
foreach (string r in rows)
{
//Split the row at the delimiter.
string[] items = r.Split(delimiter.ToCharArray());
//Add the item
result.Tables[TableName].Rows.Add(items);
}
//Return the imported data.
return result;
}
If i use the multi delimited than error shows.How to use the multi delimited.If i run the code to to load the text file given in above show the bellow error
Input array is longer than the number of columns in this table.
Your problem is that your delimiter is included in the fields you are delimiting....
In other words, your code would of be fine if instead of spaces, customer name and such, had underscores instead of spaces, that way the split function would only return the columns data. For example:
ALFKI Alfreds Futterkiste Maria Anders Sales Representative Obere Str. 57 Berlin NULL 12209 Germany 030-0074321 030-0076545
Would split as:
ALKFI, Alfreds, Futterkiste, Maria, Anders, Sales, Representative.....
As you are using space to delimit the fields.
Hope that answers your question :)
EDIT: As stated in one of the comments below i came up with the idea of using RegEx, so here it is:
This will provide you with data for each Line:
Match Telephone = Regex.Match(Target, @"\d*-\d*");
Match Fax = Telephone.NextMatch();
Match Country = Regex.Match(Target, @"\w*(?=\s\d{0,3}-)");
Match PCode = Regex.Match(target, @"(?=\s*)\d*(?=\s*" + Country.Value + ")");
Target is the line you read from the file
I am learning RegEx right now so give me some time for more RegEx expressions...
精彩评论