2010-05-22 62 views
0

这里是我的dillema ...我有一个完整的.txt逗号分隔文件目录,其排列方式如下所示。我想要做的是将其中的每一个导入到SQL或SQLite数据库中,并在最后一个数据库中添加每个数据库。 (1表)...我对C#或VB脚本开放,只是不知道如何做到这一点。我只想从下面开始提取和导入数据。类型,壮举。名字等等。解析目录中的文件/在数据库中插入

这些存储在我的网络驱动器上的\ 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

你们摇滚!我现在就试试看看我能否实现它! – 2010-05-24 03:28:39

+0

这工作得很好!非常感谢你!! 只是一对夫妇的小事情,我不知道你能不能帮我出符合本规范.... 文本文件名称的格式是R303717COMP_140A4075_20100520 我怎么可能会这么在表中的第一列识别R303717并在该文件中的每个记录的第一列插入? (所以如果我有一个文件是R515200它会然后把R515200号码旁边的每个记录),这将允许我通过串行查询 此外,文件处理后,我可以将该名称保存到表被检查,所以它不会添加一个文件两次? – 2010-05-24 05:44:32

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; 
     } 
    } 
相关问题