ftp读取txt数据并插入数据库

去官网下载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);

}         }     }

}

上一篇:SQL的一些基础查询语法


下一篇:Activity之间的数据传递(Arraylist)