2010-05-22 5 views
0

내 dillema는 ... 아래에 표시된 것처럼 정렬 된 .txt 쉼표로 구분 된 파일로 가득 찬 디렉토리가 있습니다. 내가하고 싶은 일은 SQL 또는 SQLite 데이터베이스에 이들 각각을 가져 와서 마지막 하나 하나를 추가하는 것입니다. (1 테이블) ... 나는 C# 또는 VB 스크립팅에 개방되어 있으며 이것을 수행하는 방법을 모르겠습니다. 나는 'Feat.'를 시작으로 데이터를 추출하고 가져 오기만을 원합니다. 유형, Feat. 이름, 등 '줄.디렉토리의 파일을 파싱/데이터베이스에 삽입

이들은 네트워크 드라이브의 \ mynetwork \ directory \ stats 폴더에 저장됩니다. 이상적으로는 이미 소프트웨어/스크립트가 데이터베이스에 파일을 다시 추가하지 않도록 알리는 기능을 추가 할 수있을 것입니다.

모든 안내 또는 정보를 제공해 주시면 감사하겠습니다.

$$ SAMPLE= 
$$ FIXTURE=- 
$$ OPERATOR=- 
$$ INSPECTION PROCESS=CMM #4 
$$ PROCESS OPERATION=- 
$$ PROCESS SEQUENCE=- 
$$ TRIAL=- 

Feat. Type,Feat. Name,Value,Actual,Nominal,Dev.,Tol-,Tol+,Out of Tol.,Comment 
Point,_FF_PLN_A_1,X,-17.445,-17.445,0.000,-999.000,999.000,, 
Point,_FF_PLN_A_1,Y,-195.502,-195.502,0.000,-999.000,999.000,, 
Point,_FF_PLN_A_1,Z,32.867,33.500,-0.633,-0.800,0.800,, 
Point,_FF_PLN_A_2,X,-73.908,-73.908,0.000,-999.000,999.000,, 
Point,_FF_PLN_A_2,Y,-157.957,-157.957,0.000,-999.000,999.000,, 
Point,_FF_PLN_A_2,Z,32.792,33.500,-0.708,-0.800,0.800,, 
Point,_FF_PLN_A_3,X,-100.180,-100.180,0.000,-999.000,999.000,, 
Point,_FF_PLN_A_3,Y,-142.797,-142.797,0.000,-999.000,999.000,, 
Point,_FF_PLN_A_3,Z,32.768,33.500,-0.732,-0.800,0.800,, 
Point,_FF_PLN_A_4,X,-160.945,-160.945,0.000,-999.000,999.000,, 
Point,_FF_PLN_A_4,Y,-112.705,-112.705,0.000,-999.000,999.000,, 
Point,_FF_PLN_A_4,Z,32.719,33.500,-0.781,-0.800,0.800,, 
Point,_FF_PLN_A_5,X,-158.096,-158.096,0.000,-999.000,999.000,, 
Point,_FF_PLN_A_5,Y,-73.821,-73.821,0.000,-999.000,999.000,, 
Point,_FF_PLN_A_5,Z,32.756,33.500,-0.744,-0.800,0.800,, 
Point,_FF_PLN_A_6,X,-195.670,-195.670,0.000,-999.000,999.000,, 
Point,_FF_PLN_A_6,Y,-17.375,-17.375,0.000,-999.000,999.000,, 
Point,_FF_PLN_A_6,Z,32.767,33.500,-0.733,-0.800,0.800,, 
Point,_FF_PLN_A_7,X,-173.759,-173.759,0.000,-999.000,999.000,, 
Point,_FF_PLN_A_7,Y,14.876,14.876,0.000,-999.000,999.000,, 

답변

1
using System; 
using System.Data; 
using System.Data.SQLite; 
using System.IO; 

namespace CSVImport 
{ 
    internal class Program 
    { 
     private static void Main(string[] args) 
     { 
      using (SQLiteConnection con = new SQLiteConnection("data source=data.db3")) 
      { 
       if (!File.Exists("data.db3")) 
       { 
        con.Open(); 
        using (SQLiteCommand cmd = con.CreateCommand()) 
        { 
         cmd.CommandText = 
          @" 
         CREATE TABLE [Import] (
          [RowId] integer PRIMARY KEY AUTOINCREMENT NOT NULL, 
          [FeatType] varchar, 
          [FeatName] varchar, 
          [Value] varchar, 
          [Actual] decimal, 
          [Nominal] decimal, 
          [Dev] decimal, 
          [TolMin] decimal, 
          [TolPlus] decimal, 
          [OutOfTol] decimal, 
          [Comment] nvarchar);"; 
         cmd.ExecuteNonQuery(); 
        } 
        con.Close(); 
       } 

       con.Open(); 

       using (SQLiteCommand insertCommand = con.CreateCommand()) 
       { 
        insertCommand.CommandText = 
         @" 
        INSERT INTO Import (FeatType, FeatName, Value, Actual, Nominal, Dev, TolMin, TolPlus, OutOfTol, Comment) 
        VALUES  (@FeatType, @FeatName, @Value, @Actual, @Nominal, @Dev, @TolMin, @TolPlus, @OutOfTol, @Comment);"; 

        insertCommand.Parameters.Add(new SQLiteParameter("@FeatType", DbType.String)); 
        insertCommand.Parameters.Add(new SQLiteParameter("@FeatName", DbType.String)); 
        insertCommand.Parameters.Add(new SQLiteParameter("@Value", DbType.String)); 
        insertCommand.Parameters.Add(new SQLiteParameter("@Actual", DbType.Decimal)); 
        insertCommand.Parameters.Add(new SQLiteParameter("@Nominal", DbType.Decimal)); 
        insertCommand.Parameters.Add(new SQLiteParameter("@Dev", DbType.Decimal)); 
        insertCommand.Parameters.Add(new SQLiteParameter("@TolMin", DbType.Decimal)); 
        insertCommand.Parameters.Add(new SQLiteParameter("@TolPlus", DbType.Decimal)); 
        insertCommand.Parameters.Add(new SQLiteParameter("@OutOfTol", DbType.Decimal)); 
        insertCommand.Parameters.Add(new SQLiteParameter("@Comment", DbType.String)); 

        string[] files = Directory.GetFiles(Environment.CurrentDirectory, "TextFile*.*"); 

        foreach (string file in files) 
        { 
         string[] lines = File.ReadAllLines(file); 
         bool parse = false; 

         foreach (string tmpLine in lines) 
         { 
          string line = tmpLine.Trim(); 
          if (!parse && line.StartsWith("Feat. Type,")) 
          { 
           parse = true; 
           continue; 
          } 
          if (!parse || string.IsNullOrEmpty(line)) 
          { 
           continue; 
          } 


          foreach (SQLiteParameter parameter in insertCommand.Parameters) 
          { 
           parameter.Value = null; 
          } 

          string[] values = line.Split(new[] {','}); 

          for (int i = 0; i < values.Length - 1; i++) 
          { 
           SQLiteParameter param = insertCommand.Parameters[i]; 
           if (param.DbType == DbType.Decimal) 
           { 
            decimal value; 
            param.Value = decimal.TryParse(values[i], out value) ? value : 0; 
           } 
           else 
           { 
            param.Value = values[i]; 
           } 
          } 

          insertCommand.ExecuteNonQuery(); 
         } 
        } 
       } 
       con.Close(); 
      } 
     } 
    } 
} 

결과 :

 
1 Point _FF_PLN_A_1 X -17.445 -17.445 0 -999 999 0 NULL 
2 Point _FF_PLN_A_1 Y -195.502 -195.502 0 -999 999 0 NULL 
3 Point _FF_PLN_A_1 Z 32.867 33.5 -0.633 -0.8 0.8 0 NULL 
4 Point _FF_PLN_A_2 X -73.908 -73.908 0 -999 999 0 NULL 
5 Point _FF_PLN_A_2 Y -157.957 -157.957 0 -999 999 0 NULL 
6 Point _FF_PLN_A_2 Z 32.792 33.5 -0.708 -0.8 0.8 0 NULL 
7 Point _FF_PLN_A_3 X -100.18 -100.18 0 -999 999 0 NULL 
8 Point _FF_PLN_A_3 Y -142.797 -142.797 0 -999 999 0 NULL 
9 Point _FF_PLN_A_3 Z 32.768 33.5 -0.732 -0.8 0.8 0 NULL 
10 Point _FF_PLN_A_4 X -160.945 -160.945 0 -999 999 0 NULL 
11 Point _FF_PLN_A_4 Y -112.705 -112.705 0 -999 999 0 NULL 
12 Point _FF_PLN_A_4 Z 32.719 33.5 -0.781 -0.8 0.8 0 NULL 
13 Point _FF_PLN_A_5 X -158.096 -158.096 0 -999 999 0 NULL 
14 Point _FF_PLN_A_5 Y -73.821 -73.821 0 -999 999 0 NULL 
15 Point _FF_PLN_A_5 Z 32.756 33.5 -0.744 -0.8 0.8 0 NULL 
16 Point _FF_PLN_A_6 X -195.67 -195.67 0 -999 999 0 NULL 
17 Point _FF_PLN_A_6 Y -17.375 -17.375 0 -999 999 0 NULL 
18 Point _FF_PLN_A_6 Z 32.767 33.5 -0.733 -0.8 0.8 0 NULL 
19 Point _FF_PLN_A_7 X -173.759 -173.759 0 -999 999 0 NULL 
20 Point _FF_PLN_A_1 X -17.445 -17.445 0 -999 999 0 NULL 
21 Point _FF_PLN_A_1 Y -195.502 -195.502 0 -999 999 0 NULL 
22 Point _FF_PLN_A_1 Z 32.867 33.5 -0.633 -0.8 0.8 0 NULL 
23 Point _FF_PLN_A_2 X -73.908 -73.908 0 -999 999 0 NULL 
24 Point _FF_PLN_A_2 Y -157.957 -157.957 0 -999 999 0 NULL 
25 Point _FF_PLN_A_2 Z 32.792 33.5 -0.708 -0.8 0.8 0 NULL 
26 Point _FF_PLN_A_3 X -100.18 -100.18 0 -999 999 0 NULL 
27 Point _FF_PLN_A_3 Y -142.797 -142.797 0 -999 999 0 NULL 
28 Point _FF_PLN_A_3 Z 32.768 33.5 -0.732 -0.8 0.8 0 NULL 
29 Point _FF_PLN_A_4 X -160.945 -160.945 0 -999 999 0 NULL 
30 Point _FF_PLN_A_4 Y -112.705 -112.705 0 -999 999 0 NULL 
31 Point _FF_PLN_A_4 Z 32.719 33.5 -0.781 -0.8 0.8 0 NULL 
32 Point _FF_PLN_A_5 X -158.096 -158.096 0 -999 999 0 NULL 
33 Point _FF_PLN_A_5 Y -73.821 -73.821 0 -999 999 0 NULL 
34 Point _FF_PLN_A_5 Z 32.756 33.5 -0.744 -0.8 0.8 0 NULL 
35 Point _FF_PLN_A_6 X -195.67 -195.67 0 -999 999 0 NULL 
36 Point _FF_PLN_A_6 Y -17.375 -17.375 0 -999 999 0 NULL 
37 Point _FF_PLN_A_6 Z 32.767 33.5 -0.733 -0.8 0.8 0 NULL 
38 Point _FF_PLN_A_7 X -173.759 -173.759 0 -999 999 0 NULL 
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
+0

너희들은 바위! 나는 이것을 지금 시험해보고 그것이 효과를 낼 수 있는지를 알 것이다! –

+0

위대한 작품! 대단히 감사합니다 !! 이 코드에서 나를 도와 줄 수 있는지 궁금 그냥 몇 작은 것들 .... 텍스트 파일 이름 형식은 어떻게 그렇게 테이블의 첫 번째 열이 인식 얻을 수 있습니다 R303717COMP_140A4075_20100520 있습니다 R303717 그 파일의 각 레코드에 대해 열 1에 삽입합니까? (그래서 내가 R515200 파일을 가지고 있다면 그 파일의 각 레코드 옆에 R515200 번호를 넣을 것입니다.) 이것은 시리얼 쿼리로 허용합니다. 또한 파일을 처리 한 후에 그 이름을 테이블에 저장할 수 있습니까? 그것은 두 번 파일을 추가하지 않는다 그래서 검사 해 얻는가? –

1

Directory.GetFiles(path, searchPattern)이 폴더의 모든 파일을 가져옵니다. searchPattern*.txt으로 설정하십시오.

이렇게하면 현재 디렉토리에있는 파일 목록을 반환하므로 각 파일을 한 번만 처리 할 수 ​​있습니다. 나중에 더 많은 파일을 추가 할 수 있다면 가져 오기가 완료되면 파일을 "처리 된"폴더로 옮기십시오.

"Feat. Type ..."을 읽을 때까지 한 번에 한 줄씩 각 파일을 읽은 다음 각 후속 줄을 읽고 내용을 나누어 데이터베이스에 추가해야합니다.

StreamReader.ReadLine()을 사용하여 파일에서 한 줄을 읽을 수 있습니다.

// Create an instance of StreamReader to read from a file. 
// The using statement also closes the StreamReader. 
using (StreamReader sr = new StreamReader("TestFile.txt")) 
{ 
    string line; 
    bool processLine = false; 
    // Read lines from the file until the end of the file is reached. 
    while ((line = sr.ReadLine()) != null) 
    { 
     if (processLine) 
     { 
      // Your processing here 
     } 

     // Check to see if we need to process the next lines 
     if (line.StartsWith("Feat. Type,")) 
     { 
      processLine = true; 
     } 
    } 
관련 문제