如何从 CSV 文件中提取特定数据点?

问题描述 投票:0回答:1

该项目涉及通过从 CSV 文件中选择第 10 层数据集并将其插入数据库来转换单个文件夹中的多个 CSV 文件。我目前面临从 CSV 文件中的特定点选择数据的问题。因此,您能否解释一下或提供一个如何从集合 10 中提取 CSV 文件的示例?

private void ReadCSVAndInsertToDatabase(string filePath)
{
    try
    {
        string connectionString = "Data Source=10.24.1.239;Initial Catalog=test;Integrated Security=true;TrustServerCertificate=true;";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (StreamReader reader = new StreamReader(filePath))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    string[] data = line.Split(',');

                    if (data.Length >= 12)
                    {
                        string query = "INSERT INTO MeasurementData (Time, Pressure, [Current], Rate, Thickness, Flow, IBV, IBC, SupV, BiasC, IBO2, IBAr) " +
                                       "VALUES (@Time, @Pressure, @Current, @Rate, @Thickness, @Flow, @IBV, @IBC, @SupV, @BiasC, @IBO2, @IBAr)";

                        using (SqlCommand command = new SqlCommand(query, connection))
                        {
                            command.Parameters.AddWithValue("@Time", data[0]);
                            command.Parameters.AddWithValue("@Pressure", data[1]);
                            command.Parameters.AddWithValue("@Current", data[2]);
                            command.Parameters.AddWithValue("@Rate", data[3]);
                            command.Parameters.AddWithValue("@Thickness", data[4]);
                            command.Parameters.AddWithValue("@Flow", data[5]);
                            command.Parameters.AddWithValue("@IBV", data[6]);
                            command.Parameters.AddWithValue("@IBC", data[7]);
                            command.Parameters.AddWithValue("@SupV", data[8]);
                            command.Parameters.AddWithValue("@BiasC", data[9]);
                            command.Parameters.AddWithValue("@IBO2", data[10]);
                            command.Parameters.AddWithValue("@IBAr", data[11]);

                            command.ExecuteNonQuery();
                        }
                    }
                    else
                    {
                        Console.WriteLine($"{line}");
                    }
                }
            }
            connection.Close();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: " + ex.Message);
    }
}
3
时间 压力 当前
1 0.016 175
2 0.015 193
3 0.015 207
4 0.016 208

enter image description here

c#
1个回答
0
投票

没有使用实际文件测试解决方案,但它可能会让您了解如何解决问题。请看下面的代码

while ((line = reader.ReadLine()) != null) // read rows as usual
{
   if (line == "Layer") // first row should be "Layer"
   {
      line = reader.ReadLine(); // read next row

      if (line == "10")   // next row should be specific value
      {
          while ((line = reader.ReadLine()) != null) // read rows after "Layer" and "10"
          {
              if (line == "Layer") // if line is equal to "Layer", it means you've hit the next time point
              { 
                  return; 
              }

              // Extract/Insert rows as in your question
          }
      }
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.