博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ftp读取txt数据并插入数据库
阅读量:4662 次
发布时间:2019-06-09

本文共 2813 字,大约阅读时间需要 9 分钟。

去官网下载http://enterprisedt.com/ .netftp组件

目前最新版本为2.2.3,下载后在bin目录中找到edtFTPnet.dll,在项目中添加引用。 

using EnterpriseDT.Net.Ftp;

public partial class test_ftp : System.Web.UI.Page {

    protected void Page_Load(object sender, EventArgs e)    

{         if (!IsPostBack)      

    {           

     Connect_Ftp();        

       insertdata();    

     }

    }

    #region 连接FTP     private static void Connect_Ftp()     {       

        string[] strArray = null;       

  FTPClient ftp = new FTPClient();              

  var itccConfig = ConfigFactory.GetConfig<IitccConfig>();        

try      

   {             //以下开始获取FTP数据         

     ftp.RemoteHost = itccConfig.FTPIp;           

  ftp.ControlPort = 21;          

   ftp.Timeout = 0x2710;

            ftp.Connect();         

    ftp.Login(itccConfig.FTPName, itccConfig.FTPPassWord);

            ftp.ConnectMode = FTPConnectMode.PASV;        

     ftp.TransferType = FTPTransferType.ASCII;

            ftp.ChDir("/home/ftpuser/itcc/"); //文件夹路径 

            strArray = ftp.Dir(".", true);

            string strDay = DateTime.Now.AddDays(-1).ToString("yyyyMMdd");         

    //foreach (string strFile in strArray)         

    //{             //if (strFile.Length >= 0)         

    //{             //string strfilename = strFile.Substring(0, 24).TrimStart();          

   string strfilename = "poc_ftp_T04_2015-06-11T0329309.txt";         

    //if (strfilename == "per" + strDay + ".txt")         

    //{             // if (CheckIsOver(ConnectionString, strfilename, strDay))       

      //{             //if (CheckIsInserting(ConnectionString, strfilename, strDay))          

   //{             //string strLogFile = @"E:\log\upload\copy." + strfilename;         

    string strLogFile = @"E:\downftp\copy." + strfilename;

            if (File.Exists(strLogFile))              

   File.Delete(strLogFile);            

ftp.Get(strLogFile, strfilename);               

      //ftp.Get(strfilename);             //以上为获取FTP数据,并将文件存放在本地硬盘中      

        //Insert_Date(ConnectionString, null, strDay, strDataInfo, strFilePath + @"/" + strLogFile);//数据插入数据库              // }             //}             //}             //}             //}

            if (ftp.Connected)       

      {                               ftp.Quit();

            }           

        }         catch (Exception ex)        

{             if (ftp.Connected)          

   {                 ftp.Quit();             }          

   throw ex;         }        

    }    

#endregion

    private static void insertdata()     {        

FileStream fs = new FileStream("E:/downftp/copy.poc_ftp_T04_2015-06-11T0329309.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

        System.IO.StreamReader my = new System.IO.StreamReader(fs, System.Text.Encoding.UTF8);

        string line;      

   while ((line = my.ReadLine()) != null)       

  {             //Response.Write(line + "<br />");     

        string[] mm = line.Split(';');          

   Insert(mm);         }

        my.Close();

    }     private static void Insert(string[] mm)   

  {         for (int i = 0; i < mm.Length/8; i++)        

{             string employeecode = mm[i*8];         

    string employeename = mm[i*8+1];           

  string phone1 = mm[i * 8 + 2];           

  string phone2 = mm[i * 8 + 3];         

    string phone3 = mm[i * 8 + 4];            

string email = mm[i * 8 + 5];        

     string department = mm[i * 8 + 6];        

     string workplace = mm[i * 8 + 7];

            EmployeeCommand cmd = new EmployeeCommand();        

     if (cmd.EmployyeeCode(employeecode) == true)         

    {                 cmd.UpdateEmployee(employeecode, employeename, phone1, phone2, phone3, department, workplace, email);             }        

     else             {                 cmd.CreateEmployee(employeecode, employeename, phone1, phone2, phone3, department, workplace, email);

            }         }     }

}

 

转载于:https://www.cnblogs.com/zhaojingwei/p/4584888.html

你可能感兴趣的文章
微信开发创业交流QQ群列表
查看>>
微信小程序开发(5) 2048游戏
查看>>
LOJ-10106(有向图欧拉回路的判断)
查看>>
day06 statcic 文档的生成 静态代码块 单例设计模式
查看>>
独生子女政策对老年丧子的家庭来说,确实太残酷了
查看>>
我们绝不赞同也不鼓励赖宁这样的孩子去救火
查看>>
Python之ftp下载文件测试代码
查看>>
Flume简介与使用(三)——Kafka Sink消费数据之Kafka安装
查看>>
网页结构和表现形式
查看>>
saltstack系列(一)——介绍与安装
查看>>
php imagemagick 处理 图片剪切、压缩、合并、插入文本、背景色透明
查看>>
哈佛管理学———气质的培养
查看>>
es 修改拼音分词器源码实现汉字/拼音/简拼混合搜索时同音字不匹配
查看>>
Go学习笔记(二):编写 HelloWorld 程序
查看>>
视觉slam十四讲课后习题ch3-7
查看>>
adb_亮屏
查看>>
Windows默认字符集_查询
查看>>
深度学习基础1--神经网络
查看>>
深度学习基础2--神经网络参数的反向传播算法
查看>>
洛谷 P1560 [USACO5.2]蜗牛的旅行Snail Trails
查看>>