FTP只下载文件更新的部分数据;每小时自动下载;

 

 

第一部分:FTP文件下载

FTP操作类: https://www.cnblogs.com/hao-1234-1234/p/14237141.html

 

第二部分:创建window服务

https://www.cnblogs.com/hao-1234-1234/p/14237157.html

 

 第三部分window整体调用

using FarmWeather.DAL;
using FileOperate;
using FTPLib;
//using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace MyWindowsService
{
    public partial class MyService : ServiceBase
    {
        private string website;
        private string user;
        private string password;
        private string localFilePath;
        private string nameList;
        private System.Timers.Timer m_Timer;
        public MyService()
        {
            InitializeComponent();
        //事件中命名
            if (!System.Diagnostics.EventLog.SourceExists("数据获取服务"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    "数据获取服务", "A软件开发有限公司");
            }

            this.m_EventLog.Source = "数据获取服务";
            m_EventLog.Log = "A软件开发有限公司";
        }

        #region 服务开关
        protected override void OnStart(string[] args)
        {
            website = System.Configuration.ConfigurationManager.AppSettings["website"];
            user = System.Configuration.ConfigurationManager.AppSettings["user"];
            password = System.Configuration.ConfigurationManager.AppSettings["password"];
            localFilePath = System.Configuration.ConfigurationManager.AppSettings["localFilePath"];
            nameList = System.Configuration.ConfigurationManager.AppSettings["nameList"];
            System.Timers.Timer timer = new System.Timers.Timer();

            //启动Timer
            //timer.Interval = 24*60 * 60 * 1000;
            timer.Interval = 1 * 60 * 1000;//1分钟循环一次
            timer.AutoReset = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.Start();
            m_Timer = timer;
        }
        /// <summary>
        /// 暂停后继续运行
        /// </summary>
        protected override void OnContinue()
        {
            if (this.m_Timer != null)
            {
                this.m_Timer.Start();
            }
            base.OnContinue();
        }
        /// <summary>
        /// 暂停
        /// </summary>
        protected override void OnPause()
        {
            if (this.m_Timer != null)
            {
                this.m_Timer.Stop();
            }
            base.OnPause();
        }

        protected override void OnStop()
        {
            if (this.m_Timer != null)
            {
                this.m_Timer.Dispose();
                this.m_Timer = null;
            }

            this.m_EventLog.WriteEntry("服务停止成功");
        }
        #endregion

        #region  获取最新数据

        //下载数据方法在一小时内只执行一次。
        bool isDownloading = false;
        private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            var dateTime = DateTime.Now;
            if (dateTime.Minute >=45) 
            {
                if (isDownloading == false)
                {
                    isDownloading = true;
                    try
                    {
                        Import();
                        LogManage.Info(dateTime.ToString("yyyy-MM-dd:HH:mm:ss")+"导入成功");
                    }
                    catch (Exception ex)
                    {
                        LogManage.Error(ex);

                    }
                      
                }

            }
            else
            {
                isDownloading = false;
            }
  

        }

        //下载FTP服务器数据,插入数据库
        private void Import()
        {
            var ftp = new FtpWeb(website, "", user, password);
            string[] namelist = nameList.Split(',');
            //本地保存地址
            var filePath = localFilePath;
            foreach (var name in namelist)
            {
                //下载文件
                var judge = ftp.Download(filePath, name);
                if (judge == true)
                {
                    var inputPath = System.IO.Path.Combine(filePath, name);
                    //读文件
                    var lines = FileHelper.ReadList(inputPath);
                    //插入数据库
                    var importObj = new ImportDataByModel();
                    switch (name)
                    {
                        case "AWS_30min.dat":
                            importObj.ImportAWS30min(lines);
                            break;
                        case "AWS_10min.dat":
                            importObj.ImportAWS10min(lines);
                            break;
                        case "TMS_10min.dat":
                            importObj.ImportTMS10min(lines);
                            break;
                        case "TMS_30min.dat":
                            importObj.ImportTMS30min(lines);
                            break;
                        default:
                            break;
                    }
                }
            }
           
        }

        #endregion


    }

}

 

 

下载成功

数据格式举例:

 

 

 入库成功:

 

C# WebRequest出错:"服务器提交了协议冲突"

webRequest.KeepAlive = false;//设置一下看看

 

第四部分:window服务中配置文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <root>
      <appender-ref ref="RollingLogFileAppender" />
      <appender-ref ref="ConsoleAppender" />
    </root>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%date] %level [%thread][%c{1}:%line] - %m%n" />
      </layout>
    </appender>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <!--日志存放位置 ~/..-->
      <param name="File" value="logs\\" />
      <!--追加到文本末尾-->
      <param name="AppendToFile" value="true" />
      <!--最多产生的日志文件数,超过则只保留最新的n个。设定值value="-1"为不限文件数-->
      <param name="MaxSizeRollBackups" value="50" />
      <!--最大文件大小-->
      <param name="MaximumFileSize" value="5MB" />
      <param name="RollingStyle" value="Date" />
      <!--文件命名方式-->
      <param name="DatePattern" value="yyyy-MM-dd'.log'" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%date] %level [%thread][%c{1}:%line]  %n%m%n" />
      </layout>
    </appender>
  </log4net>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <appSettings>
    <add key="website" value="我是FTP地址" />
    <add key="user" value="我是用户名" />
    <add key="password" value="我是密码" />
    <add key="localFilePath" value="D:ftp\data" />
    <!--<add key="nameList" value="AWS_10min.dat" />-->
    <add key="nameList" value="AWS_30min.dat,AWS_10min.dat,TMS_10min.dat,TMS_30min.dat" />
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="FarmWeatherEntities" connectionString="metadata=res://*/FarmWeather.csdl|res://*/FarmWeather.ssdl|res://*/FarmWeather.msl;provider=System.Data.SqlClient;provider connection string="data source=IP地址;initial catalog=FarmWeather;persist security info=True;user id=数据库用户名;password=数据库密码;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />

  </connectionStrings>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

  

windows服务中,Log4Net 使用和配置详细说明:

https://www.cnblogs.com/hao-1234-1234/p/14237157.html

 

第五部分:插入SQLserver数据库

 

public class ImportDataByModel
    {
      

        public DateTime? AWS30minMaxDate()
        {
            DateTime? date = null;
            using (var dbfw = new FarmWeatherEntities())
            {
                date = dbfw.AWS30min.Select(d => (DateTime?)d.TIMESTAMP).Max();
            }
            return date;
        }

        public DateTime? AWS10minMaxDate()
        {

            DateTime? date = null;
            using (var dbfw = new FarmWeatherEntities())
            {
                date = dbfw.AWS10min.Select(d => (DateTime?)d.TIMESTAMP).Max();
            }
            return date;
        }



        public DateTime? TMS30minMaxDate()
        {
            DateTime? date = null;
            using (var dbfw = new FarmWeatherEntities())
            {
                date = dbfw.TMS30min.Select(d => (DateTime?)d.TIMESTAMP).Max();
            }
            return date;

        }

        public DateTime? TMS10minMaxDate()
        {
            DateTime? date = null;
            using (var dbfw = new FarmWeatherEntities())
            {
                date = dbfw.TMS10min.Select(d => (DateTime?)d.TIMESTAMP).Max();
            }
            return date;
        }

        public void ImportAWS30min(List<string> lines)
        {
            var list = new List<AWS30min>() { };
            //try
            //{
  
              if (lines != null && lines.Count > 4)
              {

                var maxDate = AWS30minMaxDate();
                string[] columnNameArray = lines[1].Split(',');
            
                //行数
                for (int i = 4; i < lines.Count; i++)
                {
                    //列数
                    string[] temp = lines[i].Split(',');
                    if (temp != null && temp.Length >= 30)
                    {
                        var info = new AWS30min();
                        var temp0 = temp[0].Replace("\"", "");
                        var dateTime= Convert.ToDateTime(temp0);

                        info.TIMESTAMP = dateTime;
                        info.RECORD = Convert.ToDouble(temp[1]);
                        if (temp[2] != "\"NAN\"")
                        {
                            info.batt_volt_Min = Convert.ToDouble(temp[2]);
                        }
                        if (temp[3] != "\"NAN\"")
                        {
                            info.PTemp = Convert.ToDouble(temp[3]);
                        }
                        if (temp[4] != "\"NAN\"")
                        {
                            info.TA_Avg = Convert.ToDouble(temp[4]);
                        }
                        if (temp[5] != "\"NAN\"")
                        {
                            info.RH_Avg = Convert.ToDouble(temp[5]);
                        }
                        if (temp[6] != "\"NAN\"")
                        {
                            info.DR_Avg = Convert.ToDouble(temp[6]);
                        }
                        if (temp[7] != "\"NAN\"")
                        {
                            info.UR_Avg = Convert.ToDouble(temp[7]);
                        }
                        if (temp[8] != "\"NAN\"")
                        {
                            info.DLR_Avg = Convert.ToDouble(temp[8]);
                        }
                        if (temp[9] != "\"NAN\"")
                        {
                            info.ULR_Avg = Convert.ToDouble(temp[9]);
                        }
                        //if (temp[10] != "\"NAN\"")
                        //{
                        //    info.cnr4_T_C_Avg = Convert.ToDouble(temp[10]);
                        //}
                        //if (temp[11] != "\"NAN\"")
                        //{
                        //    info.cnr4_T_K_Avg = Convert.ToDouble(temp[11]);
                        //}
                        //if (temp[12] != "\"NAN\"")
                        //{
                        //    info.Rn_Avg = Convert.ToDouble(temp[12]);
                        //}
                        //if (temp[13] != "\"NAN\"")
                        //{
                        //    info.albedo_Avg = Convert.ToDouble(temp[13]);
                        //}
                        if (temp[14] != "\"NAN\"")
                        {
                            info.Press_Avg = Convert.ToDouble(temp[14]);
                        }
                        if (temp[15] != "\"NAN\"")
                        {
                            info.GS_2cm_Avg = Convert.ToDouble(temp[15]);
                        }
                        if (temp[16] != "\"NAN\"")
                        {
                            info.GS_5cm_Avg = Convert.ToDouble(temp[16]);
                        }
                        if (temp[17] != "\"NAN\"")
                        {
                            info.PAR_Avg = Convert.ToDouble(temp[17]);
                        }
                        if (temp[18] != "\"NAN\"")
                        {
                            info.WS_Avg = Convert.ToDouble(temp[18]);
                        }
                        if (temp[19] != "\"NAN\"")
                        {
                            info.WD = Convert.ToDouble(temp[19]);
                        }
                        if (temp[20] != "\"NAN\"")
                        {
                            info.TargTempC_Avg = Convert.ToDouble(temp[20]);
                        }
                        if (temp[21] != "\"NAN\"")
                        {
                            info.SBTempC_Avg = Convert.ToDouble(temp[21]);
                        }
                        if (temp[22] != "\"NAN\"")
                        {
                            info.UV_Avg = Convert.ToDouble(temp[22]);
                        }
                        if (temp[23] != "\"NAN\"")
                        {
                            info.Rain_Tot = Convert.ToDouble(temp[23]);
                        }

                        if (temp[24] != "\"NAN\"")
                        {
                            info.Sun_Hour_Tot = Convert.ToDouble(temp[24]);
                        }
                        if (temp[25] != "\"NAN\"")
                        {
                            info.dir_Ra_Avg = Convert.ToDouble(temp[25]);
                        }
                        if (temp[26] != "\"NAN\"")
                        {
                            info.Depth_Avg = Convert.ToDouble(temp[26]);
                        }
                        if (temp[27] != "\"NAN\"")
                        {
                            info.TS_Solinst = Convert.ToDouble(temp[27]);
                        }
                        if (temp[28] != "\"NAN\"")
                        {
                            info.Depth_Solinst = Convert.ToDouble(temp[28]);
                        }
                        if (temp[29] != "\"NAN\"")
                        {
                            info.EC_Solinst = Convert.ToDouble(temp[29]);
                        }

                        if (maxDate != null)
                        {
                           if (dateTime > maxDate)
                            {
                                list.Add(info);
                          }

                        }else
                        {
                            list.Add(info);
                        }

                    }
                }

               
                if (list.Count > 0)
                {
                    list = list.DistinctByOneField(d => d.TIMESTAMP).ToList();
                    using (var dbfw = new FarmWeatherEntities())
                    {
                        dbfw.AWS30min.AddRange(list);
                        dbfw.SaveChanges();
                    }
                }
             }
           //}
           // catch (Exception ex)
           // {
           //     while (ex.InnerException != null)
           //     {
           //         ex = ex.InnerException;
           //         //若重复错误,则逐条遍历。
           //         if (ex.Message.Contains("IX_"))
           //         {
           //             foreach (var myObj in list)
           //             {
           //                 try
           //                 {
           //                     using (var dbfw = new FarmWeatherEntities())
           //                     {
           //                         list = list.DistinctByOneField(d => d.TIMESTAMP).ToList();
           //                         db.AWS30min.AddRange(list);
           //                         db.SaveChanges();
           //                     }
           //                 }
           //                 catch (Exception ex2)
           //                 {
           //                 }
           //             }
           //         }
           //     }
           // }

        }

        public void ImportAWS10min(List<string> lines)
        {
            
            if (lines != null && lines.Count > 4)
            {
                var maxDate = AWS10minMaxDate();
                string[] columnNameArray = lines[1].Split(',');
                var list = new List<AWS10min>() { };

                //行数
                for (int i = 4; i < lines.Count; i++)
                {
                    //列数
                    string[] temp = lines[i].Split(',');
                    if (temp != null && temp.Length >=30)
                    {
                        var info = new AWS10min();

                        var temp0 = temp[0].Replace("\"", "");
                        var dateTime = Convert.ToDateTime(temp0);

                        info.TIMESTAMP = dateTime;
                        info.RECORD = Convert.ToDouble(temp[1]);
                        if (temp[2] != "\"NAN\"")
                        {
                            info.batt_volt_Min = Convert.ToDouble(temp[2]);
                        }
                        if (temp[3] != "\"NAN\"")
                        {
                            info.PTemp = Convert.ToDouble(temp[3]);
                        }
                        if (temp[4] != "\"NAN\"")
                        {
                            info.TA_Avg = Convert.ToDouble(temp[4]);
                        }
                        if (temp[5] != "\"NAN\"")
                        {
                            info.RH_Avg = Convert.ToDouble(temp[5]);
                        }
                        if (temp[6] != "\"NAN\"")
                        {
                            info.DR_Avg = Convert.ToDouble(temp[6]);
                        }
                        if (temp[7] != "\"NAN\"")
                        {
                            info.UR_Avg = Convert.ToDouble(temp[7]);
                        }
                        if (temp[8] != "\"NAN\"")
                        {
                            info.DLR_Avg = Convert.ToDouble(temp[8]);
                        }
                        if (temp[9] != "\"NAN\"")
                        {
                            info.ULR_Avg = Convert.ToDouble(temp[9]);
                        }
                        //if (temp[10] != "\"NAN\"")
                        //{
                        //    info.cnr4_T_C_Avg = Convert.ToDouble(temp[10]);
                        //}
                        //if (temp[11] != "\"NAN\"")
                        //{
                        //    info.cnr4_T_K_Avg = Convert.ToDouble(temp[11]);
                        //}
                        //if (temp[12] != "\"NAN\"")
                        //{
                        //    info.Rn_Avg = Convert.ToDouble(temp[12]);
                        //}
                        //if (temp[13] != "\"NAN\"")
                        //{
                        //    info.albedo_Avg = Convert.ToDouble(temp[13]);
                        //}
                        if (temp[14] != "\"NAN\"")
                        {
                            info.Press_Avg = Convert.ToDouble(temp[14]);
                        }
                        if (temp[15] != "\"NAN\"")
                        {
                            info.GS_2cm_Avg = Convert.ToDouble(temp[15]);
                        }
                        if (temp[16] != "\"NAN\"")
                        {
                            info.GS_5cm_Avg = Convert.ToDouble(temp[16]);
                        }
                        if (temp[17] != "\"NAN\"")
                        {
                            info.PAR_Avg = Convert.ToDouble(temp[17]);
                        }
                        if (temp[18] != "\"NAN\"")
                        {
                            info.WS_Avg = Convert.ToDouble(temp[18]);
                        }
                        if (temp[19] != "\"NAN\"")
                        {
                            info.WD = Convert.ToDouble(temp[19]);
                        }
                        if (temp[20] != "\"NAN\"")
                        {
                            info.TargTempC_Avg = Convert.ToDouble(temp[20]);
                        }
                        if (temp[21] != "\"NAN\"")
                        {
                            info.SBTempC_Avg = Convert.ToDouble(temp[21]);
                        }
                        if (temp[22] != "\"NAN\"")
                        {
                            info.UV_Avg = Convert.ToDouble(temp[22]);
                        }
                        if (temp[23] != "\"NAN\"")
                        {
                            info.Rain_Tot = Convert.ToDouble(temp[23]);
                        }

                        if (temp[24] != "\"NAN\"")
                        {
                            info.Sun_Hour_Tot = Convert.ToDouble(temp[24]);
                        }
                        if (temp[25] != "\"NAN\"")
                        {
                            info.dir_Ra_Avg = Convert.ToDouble(temp[25]);
                        }
                        if (temp[26] != "\"NAN\"")
                        {
                            info.Depth_Avg = Convert.ToDouble(temp[26]);
                        }
                        if (temp[27] != "\"NAN\"")
                        {
                            info.TS_Solinst = Convert.ToDouble(temp[27]);
                        }
                        if (temp[28] != "\"NAN\"")
                        {
                            info.Depth_Solinst = Convert.ToDouble(temp[28]);
                        }
                        if (temp[29] != "\"NAN\"")
                        {
                            info.EC_Solinst = Convert.ToDouble(temp[29]);
                        }
                        if (maxDate != null)
                        {
                            if (dateTime > maxDate)
                            {


                                list.Add(info);
                            }

                        }
                        else
                        {
                            list.Add(info);
                        }

                    }
                }

                if (list.Count > 0)
                {

                    var list2 = list.DistinctByOneField(d => d.TIMESTAMP).ToList();
                    using (var dbfw=new FarmWeatherEntities())
                    {
                        dbfw.AWS10min.AddRange(list2);
                        dbfw.SaveChanges();
                    }
    
                }
            }


        }



        public void ImportTMS10min(List<string> lines)
        {

            var list = new List<TMS10min>() { };
      
                if (lines != null && lines.Count > 4)
                {
                    var maxDate = TMS10minMaxDate();
                    string[] columnNameArray = lines[1].Split(',');
              
                    //行数
                    for (int i = 4; i < lines.Count; i++)
                    {
                        //列数
                        string[] temp = lines[i].Split(',');
                        if (temp != null && temp.Length >= 24)
                        {
                            var info = new TMS10min();
                            var temp0 = temp[0].Replace("\"", "");
                            var dateTime = Convert.ToDateTime(temp0);
                            info.TIMESTAMP = dateTime;
                            info.RECORD = Convert.ToDouble(temp[1]);
                            if (temp[2] != "\"NAN\"")
                            {
                                info.TS_0cm_Avg = Convert.ToDouble(temp[2]);
                            }
                            if (temp[3] != "\"NAN\"")
                            {
                                info.MS_5cm_Avg = Convert.ToDouble(temp[3]);
                            }
                            if (temp[4] != "\"NAN\"")
                            {
                                info.EC_5cm_Avg = Convert.ToDouble(temp[4]);
                            }
                            if (temp[5] != "\"NAN\"")
                            {
                                info.TS_5cm_Avg = Convert.ToDouble(temp[5]);
                            }
                            if (temp[6] != "\"NAN\"")
                            {
                                info.MS_10cm_Avg = Convert.ToDouble(temp[6]);
                            }
                            if (temp[7] != "\"NAN\"")
                            {
                                info.EC_10cm_Avg = Convert.ToDouble(temp[7]);
                            }
                            if (temp[8] != "\"NAN\"")
                            {
                                info.TS_10cm_Avg = Convert.ToDouble(temp[8]);
                            }
                            if (temp[9] != "\"NAN\"")
                            {
                                info.MS_20cm_Avg = Convert.ToDouble(temp[9]);
                            }
                            if (temp[10] != "\"NAN\"")
                            {
                                info.EC_20cm_Avg = Convert.ToDouble(temp[10]);
                            }
                            if (temp[11] != "\"NAN\"")
                            {
                                info.TS_20cm_Avg = Convert.ToDouble(temp[11]);
                            }
                            if (temp[12] != "\"NAN\"")
                            {
                                info.MS_40cm_Avg = Convert.ToDouble(temp[12]);
                            }
                            if (temp[13] != "\"NAN\"")
                            {
                                info.EC_40cm_Avg = Convert.ToDouble(temp[13]);
                            }
                            if (temp[14] != "\"NAN\"")
                            {
                                info.TS_40cm_Avg = Convert.ToDouble(temp[14]);
                            }
                            if (temp[15] != "\"NAN\"")
                            {
                                info.MS_60cm_Avg = Convert.ToDouble(temp[15]);
                            }
                            if (temp[16] != "\"NAN\"")
                            {
                                info.EC_60cm_Avg = Convert.ToDouble(temp[16]);
                            }
                            if (temp[17] != "\"NAN\"")
                            {
                                info.TS_60cm_Avg = Convert.ToDouble(temp[17]);
                            }
                            if (temp[18] != "\"NAN\"")
                            {
                                info.MS_80cm_Avg = Convert.ToDouble(temp[18]);
                            }
                            if (temp[19] != "\"NAN\"")
                            {
                                info.EC_80cm_Avg = Convert.ToDouble(temp[19]);
                            }
                            if (temp[20] != "\"NAN\"")
                            {
                                info.TS_80cm_Avg = Convert.ToDouble(temp[20]);
                            }
                            if (temp[21] != "\"NAN\"")
                            {
                                info.MS_100cm_Avg = Convert.ToDouble(temp[21]);
                            }
                            if (temp[22] != "\"NAN\"")
                            {
                                info.EC_100cm_Avg = Convert.ToDouble(temp[22]);
                            }
                            if (temp[23] != "\"NAN\"")
                            {
                                info.TS_100cm_Avg = Convert.ToDouble(temp[23]);
                            }

                           if (maxDate != null)
                            {
                                if (dateTime > maxDate)
                                {
                                   list.Add(info);
                                }
                            }
                            else
                            {
                                list.Add(info);
                            }

                        }
                    }

                    if (list.Count > 0)
                    {
                        list = list.DistinctByOneField(d => d.TIMESTAMP).ToList();
                        using (var dbfw = new FarmWeatherEntities())
                        {
                            dbfw.TMS10min.AddRange(list);
                            dbfw.SaveChanges();
                        }

                    }
                }

        }


        public void ImportTMS30min(List<string> lines)
        {
            if (lines != null && lines.Count > 4)
            {
                var maxDate = TMS30minMaxDate();
                string[] columnNameArray = lines[1].Split(',');
                var list = new List<TMS30min>() { };

                //行数
                for (int i = 4; i < lines.Count; i++)
                {
                    //列数
                    string[] temp = lines[i].Split(',');
                    if (temp != null && temp.Length >= 24)
                    {
                        var info = new TMS30min();
                        var temp0 = temp[0].Replace("\"", "");
                        var dateTime = Convert.ToDateTime(temp0);
                        info.TIMESTAMP = dateTime;
                        info.RECORD = Convert.ToDouble(temp[1]);
                        if (temp[2] != "\"NAN\"")
                        {
                            info.TS_0cm_Avg = Convert.ToDouble(temp[2]);
                        }
                        if (temp[3] != "\"NAN\"")
                        {
                            info.MS_5cm_Avg = Convert.ToDouble(temp[3]);
                        }
                        if (temp[4] != "\"NAN\"")
                        {
                            info.EC_5cm_Avg = Convert.ToDouble(temp[4]);
                        }
                        if (temp[5] != "\"NAN\"")
                        {
                            info.TS_5cm_Avg = Convert.ToDouble(temp[5]);
                        }
                        if (temp[6] != "\"NAN\"")
                        {
                            info.MS_10cm_Avg = Convert.ToDouble(temp[6]);
                        }
                        if (temp[7] != "\"NAN\"")
                        {
                            info.EC_10cm_Avg = Convert.ToDouble(temp[7]);
                        }
                        if (temp[8] != "\"NAN\"")
                        {
                            info.TS_10cm_Avg = Convert.ToDouble(temp[8]);
                        }
                        if (temp[9] != "\"NAN\"")
                        {
                            info.MS_20cm_Avg = Convert.ToDouble(temp[9]);
                        }
                        if (temp[10] != "\"NAN\"")
                        {
                            info.EC_20cm_Avg = Convert.ToDouble(temp[10]);
                        }
                        if (temp[11] != "\"NAN\"")
                        {
                            info.TS_20cm_Avg = Convert.ToDouble(temp[11]);
                        }
                        if (temp[12] != "\"NAN\"")
                        {
                            info.MS_40cm_Avg = Convert.ToDouble(temp[12]);
                        }
                        if (temp[13] != "\"NAN\"")
                        {
                            info.EC_40cm_Avg = Convert.ToDouble(temp[13]);
                        }
                        if (temp[14] != "\"NAN\"")
                        {
                            info.TS_40cm_Avg = Convert.ToDouble(temp[14]);
                        }
                        if (temp[15] != "\"NAN\"")
                        {
                            info.MS_60cm_Avg = Convert.ToDouble(temp[15]);
                        }
                        if (temp[16] != "\"NAN\"")
                        {
                            info.EC_60cm_Avg = Convert.ToDouble(temp[16]);
                        }
                        if (temp[17] != "\"NAN\"")
                        {
                            info.TS_60cm_Avg = Convert.ToDouble(temp[17]);
                        }
                        if (temp[18] != "\"NAN\"")
                        {
                            info.MS_80cm_Avg = Convert.ToDouble(temp[18]);
                        }
                        if (temp[19] != "\"NAN\"")
                        {
                            info.EC_80cm_Avg = Convert.ToDouble(temp[19]);
                        }
                        if (temp[20] != "\"NAN\"")
                        {
                            info.TS_80cm_Avg = Convert.ToDouble(temp[20]);
                        }
                        if (temp[21] != "\"NAN\"")
                        {
                            info.MS_100cm_Avg = Convert.ToDouble(temp[21]);
                        }
                        if (temp[22] != "\"NAN\"")
                        {
                            info.EC_100cm_Avg = Convert.ToDouble(temp[22]);
                        }
                        if (temp[23] != "\"NAN\"")
                        {
                            info.TS_100cm_Avg = Convert.ToDouble(temp[23]);
                        }
                        if (maxDate != null)
                        {
                            if (dateTime > maxDate)
                            {
                                list.Add(info);
                            }

                        }
                        else
                        {
                            list.Add(info);
                        }

                    }
                }

                if (list.Count > 0)
                {
                    var list2 = list.DistinctByOneField(d => d.TIMESTAMP).ToList();
                    using (var dbfw = new FarmWeatherEntities())
                    {
                        dbfw.TMS30min.AddRange(list2);
                        dbfw.SaveChanges();
                    }
                
                }
            }


        }

    }

实体类 

 public class AWS30min
    {
        public int ID { get; set; }
        public System.DateTime TIMESTAMP { get; set; }
        public Nullable<double> RECORD { get; set; }
        public Nullable<double> batt_volt_Min { get; set; }
        public Nullable<double> PTemp { get; set; }
        public Nullable<double> TA_Avg { get; set; }
        public Nullable<double> RH_Avg { get; set; }
        public Nullable<double> DR_Avg { get; set; }
        public Nullable<double> UR_Avg { get; set; }
        public Nullable<double> DLR_Avg { get; set; }
        public Nullable<double> ULR_Avg { get; set; }
        public Nullable<double> Press_Avg { get; set; }
        public Nullable<double> GS_2cm_Avg { get; set; }
        public Nullable<double> GS_5cm_Avg { get; set; }
        public Nullable<double> PAR_Avg { get; set; }
        public Nullable<double> WS_Avg { get; set; }
        public Nullable<double> WD { get; set; }
        public Nullable<double> TargTempC_Avg { get; set; }
        public Nullable<double> SBTempC_Avg { get; set; }
        public Nullable<double> UV_Avg { get; set; }
        public Nullable<double> Rain_Tot { get; set; }
        public Nullable<double> Sun_Hour_Tot { get; set; }
        public Nullable<double> dir_Ra_Avg { get; set; }
        public Nullable<double> Depth_Avg { get; set; }
        public Nullable<double> TS_Solinst { get; set; }
        public Nullable<double> Depth_Solinst { get; set; }
        public Nullable<double> EC_Solinst { get; set; }
    }

    public  class TMS30min
    {
        public int ID { get; set; }
        public System.DateTime TIMESTAMP { get; set; }
        public Nullable<double> RECORD { get; set; }
        public Nullable<double> TS_0cm_Avg { get; set; }
        public Nullable<double> MS_5cm_Avg { get; set; }
        public Nullable<double> EC_5cm_Avg { get; set; }
        public Nullable<double> TS_5cm_Avg { get; set; }
        public Nullable<double> MS_10cm_Avg { get; set; }
        public Nullable<double> EC_10cm_Avg { get; set; }
        public Nullable<double> TS_10cm_Avg { get; set; }
        public Nullable<double> MS_20cm_Avg { get; set; }
        public Nullable<double> EC_20cm_Avg { get; set; }
        public Nullable<double> TS_20cm_Avg { get; set; }
        public Nullable<double> MS_40cm_Avg { get; set; }
        public Nullable<double> EC_40cm_Avg { get; set; }
        public Nullable<double> TS_40cm_Avg { get; set; }
        public Nullable<double> MS_60cm_Avg { get; set; }
        public Nullable<double> EC_60cm_Avg { get; set; }
        public Nullable<double> TS_60cm_Avg { get; set; }
        public Nullable<double> MS_80cm_Avg { get; set; }
        public Nullable<double> EC_80cm_Avg { get; set; }
        public Nullable<double> TS_80cm_Avg { get; set; }
        public Nullable<double> MS_100cm_Avg { get; set; }
        public Nullable<double> EC_100cm_Avg { get; set; }
        public Nullable<double> TS_100cm_Avg { get; set; }
    }

AWS30min 与AWS10min 相同,TMS30min 与TMS10min 相同。

 

文件流操作类:

    public class FileHelper
    {
        public static List<string> ReadList(string filePath)
        {
            List<string> lines = new List<string>();
            using (var reader = new StreamReader(filePath, Encoding.Default))
            {
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    lines.Add(line);
                }
            }
            return lines;
        }
    }

 

  

 

文件流操作参考文章: https://www.cnblogs.com/hao-1234-1234/p/14236832.html

 

 

 

posted @ 2020-11-11 12:00  hao_1234_1234  阅读(843)  评论(0编辑  收藏  举报