C#逐行读取文本文件

1.前言

  有两个日志文件Arrive和Done,里面分别保存了程序处理一个报文的日志信息,Arrive里面保存的是报文的到达时间,Done里面保存的是报文处理完成的时间,现在想知道每个报文的处理时间是多长。如果靠人工在两个日志文件中逐个报文对比的话需要花很长的时间,于是想到了将两个报文的信息存储到数据库中,再利用数据库的强大处理功能来获得每个报文的处理时间。

2.实现

  有了想法,立刻着手实现。

2.1定义数据库

  在数据库中增加一个LOG_INFO的表,语法如下:

create table LOG_INFO
(
  FILE_NAME   CHAR(15) not null,
  ARRIVE_TIME DATE,
  DEAL_TIME   DATE
);

2.2报文解析入库

  读文件并写入数据库:

 1         private void btnStart_Click(object sender, EventArgs e)
 2         {
 3             String line, time, filename, sql;
 4             int i = 0, result = 0;
 5             OracleConnection conn = new OracleConnection(connectionString);
 6             conn.Open();
 7             for (i = 0; i < 8; i++)
 8             {
 9                 switch (i)
10                 { 
11                     case 0:
12                         lblCurrFile.Text = "Arrive3.log";
13                         break;
14                     case 1:
15                         lblCurrFile.Text = "Arrive2.log";
16                         break;
17                     case 2:
18                         lblCurrFile.Text = "Arrive1.log";
19                         break;
20                     case 3:
21                         lblCurrFile.Text = "Arrive.log";
22                         break;
23                     case 4:
24                         lblCurrFile.Text = "Done3.log";
25                         break;
26                     case 5:
27                         lblCurrFile.Text = "Done2.log";
28                         break;
29                     case 6:
30                         lblCurrFile.Text = "Done1.log";
31                         break;
32                     case 7:
33                         lblCurrFile.Text = "Done.log";
34                         break;
35                     default:                     
36                         break;
37                 }
38                 if (File.Exists(tbxPath.Text + lblCurrFile.Text))
39                 {
40                     try
41                     {
42                         using (StreamReader sr = new StreamReader(tbxPath.Text + lblCurrFile.Text)) 
43                         {
44                             while ((line = sr.ReadLine()) != null) 
45                             {
46                                 if (lbxReportInfo.Items.Count > 100)
47                                 {
48                                     lbxReportInfo.Items.Clear();
49                                 }
50                                 else
51                                 {
52                                     lbxReportInfo.Items.Add(line);
53                                 }
54                                 time = line.Substring(1, 19);
55                                 filename = line.Substring(22, 15);
56 
57                                 if (lblCurrFile.Text.Substring(0, 4).Equals("Arri"))
58                                     sql = String.Format("insert into LOG_INFO(FILE_NAME,ARRIVE_TIME) values('{0}',TO_DATE('{1}','YYYY-MM-DD HH24:MI:SS'))", filename, time);
59                                 else
60                                     sql = String.Format("update LOG_INFO set DEAL_TIME=TO_DATE('{0}','YYYY-MM-DD HH24:MI:SS') where FILE_NAME='{1}'", time, filename);
61                                 // 入库
62                                 try
63                                 {
64                                     OracleCommand cmd = new OracleCommand(sql, conn);
65                                     result = cmd.ExecuteNonQuery();
66                                     cmd.Dispose();
67                                 }
68                                 catch(Exception ex)
69                                 {
70                                     lblMsg.Text = ex.Message;
71                                 }
72                             }
73                         }
74                     }
75                     catch (Exception ex)
76                     {
77                         lblMsg.Text = ex.Message;
78                     }
79                 }
80                 else
81                 {
82                     continue;
83                 }
84             }
85             conn.Close();
86         }

  数据库是Oracle 11g,连接字符串格式如下:

  private string connectionString = "server=xxx;uid=xxx;pwd=xxx";

2.3计算报文处理时间

  在PL/SQL命令窗口中输入如下命令:

  select (deal_time-arrive_time)*24*60*60 from log_info;

  可以得到每个报文的处理时间。由于deal_time-arrive_time的结果单位是天,通过*24*60*60将其转换为秒。当然也可以通过*24*60转换为分钟,具体看需要。

  当然也可以加上一些查询条件,比如想得到处理时间大于100秒的记录就可以这样写:

  select (deal_time-arrive_time)*24*60*60 from log_info where (deal_time-arrive_time)*24*60*60>100;

  查询结果如下:

SQL> select (deal_time-arrive_time)*24*60*60 from log_info where (deal_time-arrive_time)*24*60*60>100;
 
(DEAL_TIME-ARRIVE_TIME)*24*60*
------------------------------
                           285
                           322
                           124
                           136
                           124
                           129
                           261
 
7 rows selected
 
SQL> 

3.总结

  程序在WinXP SP3+VS2010+Oracle 11g下测试通过。

posted on 2012-11-17 00:08  onedime  阅读(9100)  评论(0编辑  收藏  举报