2010-12-05 2 views
0

Я работаю на C# .my входного файл вид любит:Как заполнить DataSet с помощью многократных разделителей текстовых файлов

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 

Я хочу сохранить эти текстовые файлы на * набор данных. * Как это сделать. Я напишу следующий код, чтобы сделать это.

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 unwanted 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; 
} 

Если я использовать мульти разделителей чем shows.How ошибок использовать мульти delimited.If я запустить код, чтобы загрузить текстовый файл, заданный в выше показывает сильфон ошибки

Входной массив является дольше, чем количество столбцов в этой таблице.

ответ

0

Ваша проблема заключается в том, что ваш ограничитель включен в полях вы ограничивающие ....

Других слов, ваш код будет Ве хорошо, если вместо пробелов, имя клиента и такого, имели подчеркивания вместо пространств, таким образом, функция split будет возвращать только данные столбцов. Например:

ALFKI Alfreds Futterkiste Maria Anders Sales Representative Obere Str. 57 Berlin NULL 12209 Germany 030-0074321 030-0076545 

расколоть бы как:

ALKFI, Alfreds, Futterkiste, Maria, Anders, Sales, Representative..... 

Как вы используете пространство для разделения полей.

Надеется, что ответ на ваш вопрос :)

EDIT: Как указано в одном из комментариев ниже я пришел с идеей использования регулярных выражений, так вот она:

Это даст вам данные для каждой линии:

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 является линия, считываемый из файла

Я учусь RegEx прямо сейчас, так что дайте мне время для дополнительных выражений RegEx ...

+0

PLZ объяснить подробно .show какой-либо синтаксис или изменить мой код .thanks ответить – shamim 2010-12-05 15:54:46

Смежные вопросы