且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将日志文件内容添加到数据表

更新时间:2023-12-01 09:30:58

尝试将行日志数据存储在行中而不是列中

try to store log data in rows, not in columns
string[] fileContents = null;                 
                 List<string> words = new List<string>();
                 List<string> words1 = new List<string>();
                 DataTable workTable = new DataTable();
                
                 try
                 {
                     // Open and read the entire Data Log File contents.
                     fileContents = File.ReadAllLines(file);
                 }
                 catch (Exception ex)
                 {
                     throw new Exception("The Input log file was not found or is not in correct format\nDetails: " + ex.Message);
                 }
 
                 if (fileContents == null)
                     throw new Exception("The Input log file was not found or is not in correct format");
 
                 // Process each line of the Data Log File and filter out the CAN Msg Id's corresponding to each CAN Bus.
workTable.Columns.Add("login");
workTable.Columns.Add("code");
                 for (int Index = 0; Index < fileContents.Length; Index++)
                 {
                     string CANMsgId = string.Empty;
                     string[] spaceSeperator = new string[] { " " };
                     string[] lineWords = (fileContents[Index].Trim()).Split(spaceSeperator, StringSplitOptions.RemoveEmptyEntries);
                     if (lineWords.Length < (2 + 1))
 
                         continue;
 
                     // If a CAN Msg Id is valid, it should end with 'x'. If it doesnot end with 'x',
                     // then skip the entry and go to the next line of log file
                     if (lineWords[2].EndsWith("x"))
                     {
                         int i = 1;
                         //CANMsgId = lineWords[2].TrimEnd('x');
                         //words.Add(lineWords[0]);
                         //words1.Add(lineWords[1]);
workTable.Rows.Add(lineWords[2].TrimEnd('x'),lineWords[0]);
                         //workTable.Columns.Add(lineWords[2].TrimEnd('x'));
                         //workTable.Columns.Add("CustFName", typeof(String));
                         //workTable.Columns.Add(lineWords[0]);
 
                        i++;
                     }
                     
                 }