本文介绍了如何将存储在 FTP 服务器上的 ZIP 文件中的数据导入 C# 中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此代码从 FTP 服务器导入 void.dat 文件.现在 void.dat 在 archive.zip 文件下.那么如何从数据库中的 archive.zip 中提取和导入数据 void.dat?
This code imports the void.dat file from the FTP server. Now void.dat is under archive.zip file. So how can I extract and import data void.dat from the archive.zip in my database?
WebClient request = new WebClient();
string url = "ftp://agk.com/Files/" + "Void.dat";
request.Credentials = new NetworkCredential("user", "password");
byte[] newFileData = request.DownloadData(url);
string[] lines = System.Text.Encoding.UTF8.GetString(newFileData).Split('
');
for (int i = 2; i < lines.Length-1; i++)
{
int count = lines[i].Split(' ').Count();
DateTime? Date = Convert.ToDateTime((lines[i].Split(' ')[0]) == null || (lines[i].Split(' ')[0]) == "" || (lines[i].Split(' ')[0]) == "N" ? (lines[i].Split(' ')[2]) = null : (lines[i].Split(' ')[0])).Date;
string Time = ((lines[i].Split(' ')[1]) == null || (lines[i].Split(' ')[1]) == "" || (lines[i].Split(' ')[1]) == "N" ? (lines[i].Split(' ')[1]) = null : (lines[i].Split(' ')[1]));
int Shift = Convert.ToInt32((lines[i].Split(' ')[2]) == null || (lines[i].Split(' ')[2]) == "" || (lines[i].Split(' ')[2]) == "N" ? (lines[i].Split(' ')[2]) = null : (lines[i].Split(' ')[2]));
int EmployeeID = Convert.ToInt32((lines[i].Split(' ')[3]) == null || (lines[i].Split(' ')[3]) == "" || (lines[i].Split(' ')[3]) == "N" ? (lines[i].Split(' ')[3]) = null : (lines[i].Split(' ')[3]));
double Amount = Convert.ToDouble((lines[i].Split(' ')[4]) == null || (lines[i].Split(' ')[4]) == ""|| (lines[i].Split(' ')[4]) == "N" ? (lines[i].Split(' ')[4]) = null : (lines[i].Split(' ')[4]));
double Items = Convert.ToDouble((lines[i].Split(' ')[5]) == null || (lines[i].Split(' ')[5]) == "" || (lines[i].Split(' ')[5]) == "N" ? (lines[i].Split(' ')[5]) = null : (lines[i].Split(' ')[5]));
int DrawerOpen = Convert.ToInt32((lines[i].Split(' ')[6]) == null || (lines[i].Split(' ')[6]) == "" || (lines[i].Split(' ')[6]) == "N" ? (lines[i].Split(' ')[6]) = null : (lines[i].Split(' ')[6]));
int Postpone = Convert.ToInt32((lines[i].Split(' ')[7]) == null || (lines[i].Split(' ')[7]) == "" || (lines[i].Split(' ')[7]) == "N" ? (lines[i].Split(' ')[7]) = null : (lines[i].Split(' ')[7]));
int LocationID = Convert.ToInt32((lines[i].Split(' ')[8]) == null || (lines[i].Split(' ')[8]) == "" || (lines[i].Split(' ')[8]) == "N" ? (lines[i].Split(' ')[8]) = null : (lines[i].Split(' ')[8]));
_context.Database.ExecuteSqlCommand(@"Insert into VOIDS (Date,Time,Shift,EmployeeID,Amount,Items,DrawerOpen,Postpone,LocationID)
Values({0},{1},{2},{3},{4},{5},{6},{7},{8})", Date, Time, Shift, EmployeeID, Amount, Items, DrawerOpen, Postpone, LocationID);
_context.SaveChanges();
}
return "Successful Import";
推荐答案
要从 FTP 服务器上 ZIP 存档中的文件中读取字符串内容,请使用:
To read a string contents from a file located in a ZIP archive on FTP server, use:
var request = WebRequest.Create("ftp://ftp.example.com/remote/path/archive.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;
string data;
using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (var archive = new ZipArchive(ftpStream, ZipArchiveMode.Read))
{
using (Stream entryStream = archive.GetEntry("void.dat").Open())
using (var reader = new StreamReader(entryStream, Encoding.UTF8))
{
data = reader.ReadToEnd();
}
}
string[] lines = data.Split('
');
// rest of your code
无论如何,当您将字符串拆分为行时,更有效的方法是直接按行读取流:
As you split the string into lines anyway, more efficient would be to read the stream directly by lines:
using (var reader = new StreamReader(entryStream, Encoding.UTF8))
{
string line;
int i = 0;
while ((line = reader.ReadLine()) != null)
{
// skip the first two lines, as your original code does
if (i >= 2)
{
int count = line.Split(' ').Count();
// rest of your code for parsing individual lines
// (with 'lines[i]' replaced with 'line')
}
i++;
}
}
请注意,代码会下载整个存档,即使它最终只从存档中读取一个特定文件.
您关于相反操作的相关问题:
如何在C#中上传zip和上传字符串数据到FTP服务器
这篇关于如何将存储在 FTP 服务器上的 ZIP 文件中的数据导入 C# 中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!