本文介绍了如何将日志文件内容添加到数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试使用以下代码将日志文件的内容添加到数据表中





Hi,

I am trying to add the content of log files to data table using the following code


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.
                 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.Columns.Add(lineWords[2].TrimEnd('x'));
                         //workTable.Columns.Add("CustFName", typeof(String));
                         workTable.Columns.Add(lineWords[0]);

                        i++;
                     }

                 }







我得到例外以下代码行



workTable.Columns.Add(lineWords [2] .TrimEnd('x'));



异常:

System.Data.DuplicateNameException



谢谢

John




I am getting exception at the following line of code

workTable.Columns.Add(lineWords[2].TrimEnd('x'));

Exception:
System.Data.DuplicateNameException

Thanks
John

推荐答案

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++;
                     }

                 }


这篇关于如何将日志文件内容添加到数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 04:07