如何读取文本文件包含一些字符并插入到数据表中 [英] how read text file contain some charater and insert into datatable

查看:53
本文介绍了如何读取文本文件包含一些字符并插入到数据表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

my code:

string filename = @"E:\DvrAlarmLog.txt";

string str = string.Empty;

if (File.Exists(filename))
{
    string[] arrays = new string[7];
    StreamReader sr = new StreamReader(filename);
    String line = string.Empty;

    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("ID"), new DataColumn("Start Date"), new DataColumn("End Date") });

    StringBuilder sb = new StringBuilder();

    List<string> list = new List<string>();

    using (StreamReader srr = new StreamReader(filename))
    {
        //string[] lines = System.IO.File.ReadAllLines(filename);
        //foreach (string linesingle in lines)
        //{
        //    if (linesingle.Equals("Alarm ended"))
        //    {
                while (srr.Peek() >= 0)
                {
                    list.Add(srr.ReadLine());
                }

            //    }
            //}
        }
       
    
    for (int i = 13; i < list.Count; i++)
    {
        string[] strlist = list[i].Split('.',':');

        foreach (string strdt in strlist)
        {
            if (!strdt.Contains("-----------------------------------------------------"))
            {
                dt.Rows.Add(strlist[0], strlist[1], strlist[2]);
            }
        }
    }
      GridView1.DataSource = dt;
      GridView1.DataBind();            
}



和我的文本文件是如何拆分数据


and my textfile is this how split data

-------------------------------------------------
| DVR alarm log. Start time: 26-11-2015 21:02:36 |
-------------------------------------------------

Alarm started. Guid: 844429225099264
	Start time: 26-11-2015: 21:03:17
	Trigger DigitalInput
		Channel: 1
		Until acknowledged: No
	Actions: Priority = Medium
		DigitalOutput: output = 1
		CameraRecording: camera = 1 time = 10 
                 resolution = <camera default=""> framerate = <camera default="">
		CameraPrerecording: channel = 1 time = 10
-----------------------------------------------------
Alarm ended. Guid: 844429225099264
	Start time: 26-11-2015 21:03:17
	End time  : 27-11-2015 17:51:03
	Trigger DigitalInput
		Channel: 1
		Until acknowledged: No
	Actions: Priority = Medium
		DigitalOutput: output = 1
		CameraRecording: camera = 1 time = 10 
                resolution = <camera default=""> framerate = <camera default="">
		CameraPrerecording: channel = 1 time = 10
-----------------------------------------------------



i希望从报警结束数据输出这样的结果


i want to output like this from Alarm ended data

Guid              starttime       	  Endtime
844429225099264  26-11-2015 21:03:17  	27-11-2015 17:51:03



但是我没有得到这种类型的输出


but i am not getting this type of output

Alarm ended	 Guid	 844429225099264
Alarm ended	 Guid	 844429225099264
Alarm ended	 Guid	 844429225099264
 Start time	 26-11-2015 21	03
 Start time	 26-11-2015 21	03
 Start time	 26-11-2015 21	03
 Start time	 26-11-2015 21	03
 End time  	 27-11-2015 17	51
 End time  	 27-11-2015 17	51

推荐答案

Most您的问题是
string[] strlist = list[i].Split('.',':');

在包含日期和时间的行上,您将时间段拆分成碎片而不重建它。



正如PIEBALDconsult试图引导你的那样,报警结束部分包含了你需要的一切。



该文件也是一个固定的格式,所以一旦你找到了正确的部分,你可以应用规则来获取包含其余信息的行。



1. [Guid ]可以从包含文字报警结束的行中获得

2.报警结束后[下一行]的[开始时间]

3. 结束时间将在报警结束之后的行之后显示

4.每列的数据将在第一个实例:之后开始

5.每列的数据将在每一行的末尾结束。



转换为

On the lines containing a date and time you are splitting the time section into pieces and not reconstructing it.

As PIEBALDconsult tried to guide you, the section for "Alarm ended" contains everything you need.

The file is also a fixed format, so once you have found the right section you can apply rules to how to get the lines containing the rest of the information.

1. The [Guid] can be obtained from the line containing text "Alarm ended"
2. The [Start Time] will be on the next line after "Alarm ended"
3. The [End Time] will be on the line after the line after "Alarm ended"
4. The data for each column will start after the first instance of ": "
5. The data for each column will end at the end of each line.

Which translates to

string filename = @"c:\temp\DvrAlarmLog.txt";

if (!File.Exists(filename)) return;

var dt = new DataTable();
dt.Columns.AddRange(new[] { new DataColumn("ID"), new DataColumn("Start Date"), new DataColumn("End Date") });

var lines = File.ReadAllLines(filename);

for (var i = 0; i < lines.Length; i++)
{
    if (!lines[i].Contains("Alarm ended")) continue;

    //This section of the file contains everything we need
    var currGuid = lines[i].Substring(lines[i].IndexOf(":", StringComparison.InvariantCultureIgnoreCase) + 2);
    var startTime = lines[i + 1].Substring(lines[i + 1].IndexOf(":", StringComparison.InvariantCultureIgnoreCase) + 2);
    var endTime = lines[i+2].Substring(lines[i+2].IndexOf(":", StringComparison.InvariantCultureIgnoreCase) + 2);

    dt.Rows.Add(currGuid, startTime, endTime);
}
GridView1.DataSource = dt;
GridView1.DataBind();


这篇关于如何读取文本文件包含一些字符并插入到数据表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆