从文本文件中检索数据

时间:2013-03-21 05:56:22

标签: c# algorithm

我的问题是从文本文件中获取连续数据。这是对问题的描述:

我的要求是将每一行保存在文本文件中。此文本文件由外部源以未知间隔提供。此外,最重要的是 - 当文件达到100行(例如)时,它会将这些行存档在无法访问的源中并清除其内容。

考虑一下监听器查看文件并看到它包含10行的情况,监听器将采用这些行并保存它们。然后在增量时间(例如1秒)之后,它再次窥视并看到93行。它还保存了83个未被记录的内容。 现在,当听众第三次偷看时,它会看到3行。它显然可以保存它们,但请注意,听众没有记录7行(索引94-100)。

看起来在恒定三角洲偷看不会确保不会发生错过。

您如何建议达到此要求的最佳方式?

3 个答案:

答案 0 :(得分:0)

这是从文件中逐行获取数据并将其保存到数据库>>

的代码
 string line;
            string[] subLine;
            DialogResult result = openFileDialog1.ShowDialog();
            try
            {
                if (result == DialogResult.OK)
                {
                    string file = openFileDialog1.FileName;
                    StreamReader inputData = new StreamReader(file);
                    const int maxRecords = 500;
                    int actualRecords = 0;
                    while ((line = inputData.ReadLine()) != null)
                    {
                        subLine = line.Split(',');

                        try
                        {


                            string unknown = "unknown Fileld";
                            con.Open();
                            // int j=subLine.Length;
                            //for(int i=0;i<subLine.Length;i++)
                            if (actualRecords < maxRecords)
                            {
                                int i = 0;
                                //int j=subLine.Length;
                                //int index=subLine.Length-j;

                               // SQL= "insert into tradeFile values('" + ID + "','" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')";

                                cmd = new SqlCommand("insert into tradeFile values('" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')", con);
                                // cmd = new OleDbCommand("insert into tradeFile values('" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')", con);
                                cmd.ExecuteNonQuery();
                                actualRecords++;
                                //  j--;

                            }
                            else
                            {
                                int i = 0;
                                //int j=subLine.Length;
                                //int index=subLine.Length-j;
                                cmd = new SqlCommand("insert into tradeFile values('" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')", con);
                                //cmd = new OleDbCommand("insert into tradeFile values('" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')", con);
                                cmd.ExecuteNonQuery();
                                actualRecords = 0;
                            }





                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            con.Close();
                        }
                        //counter++;
                    }
                    inputData.Close();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

希望您能应用更多逻辑,这将对您有所帮助。

答案 1 :(得分:0)

有关更多不同的文件操作详情,请单击此处

File Operations in C#

答案 2 :(得分:0)

您可以使用某种形式的集合来保存这些行,例如List<string>并使用while或if语句{{1来验证List1.Add(line)对集合的容量(例如100) }}

如果您对该文件具有读/写权限,则可以在将其添加到集合后删除这些行。