在 c# 中将多个 csv 文件上传到 SQL 服务器
uploading multiple csv files to SQL server in c#
我的问题解决了。出于隐私原因,我删除了我的代码
看看this related question。
您需要进行一些更改,例如将文件扩展名更改为“.csv”并更改目标文件路径。
foreach (var file in d.GetFiles("*.csv"))
{
//Load CSV into SQL using existing code
Directory.Move(file.FullName, filepath + "\TextFiles\" + file.Name);
}
我不太确定你说的是什么意思我希望能够一次读取三个不同文件夹中的一个文件 但这里有一个解决方案,它将遍历一个文件夹的 csv 文件,让您可以选择阅读每个文件并执行您需要做的事情,然后将文件移动到新文件夹。
private void ProcessFilesCSVFiles(string copyPath, string destinationPath)
{
// first check if path exists
if (!Directory.Exists(copyPath))
// doesn't exist then exit, can't copy from something that doesn't exist
return;
var copyPathDirectory = new DirectoryInfo(copyPath);
// using the SearchOption.AllDirectories will search sub directories
var copyPathCSVFiles = copyPathDirectory.GetFiles("*.csv", SearchOption.AllDirectories);
for(var i = 0; i < copyPathCSVFiles.Length; i++)
{
// get the file
var csvFile = copyPathCSVFiles[i];
// read the csv file line by line
using (StreamReader sr = csvFile.OpenText())
{
string line = "";
while ((line = sr.ReadLine()) != null)
{
// use split to read the individual columns
// will fail if the text field has a comma in it
var split = line.Split(',');
Console.WriteLine(line);
}
}
// do other sql mojo here if needed
// move the files over to another place
var destinationFilePath = Path.Combine(destinationPath, csvFile.Name);
if (File.Exists(destinationFilePath))
{
File.Delete(destinationFilePath);
}
csvFile.MoveTo(destinationFilePath);
}
}
可以这样调用:
ProcessFilesCSVFiles(@"C:\data\copypath", @"C:\data\destinationpath");
我的问题解决了。出于隐私原因,我删除了我的代码
看看this related question。
您需要进行一些更改,例如将文件扩展名更改为“.csv”并更改目标文件路径。
foreach (var file in d.GetFiles("*.csv"))
{
//Load CSV into SQL using existing code
Directory.Move(file.FullName, filepath + "\TextFiles\" + file.Name);
}
我不太确定你说的是什么意思我希望能够一次读取三个不同文件夹中的一个文件 但这里有一个解决方案,它将遍历一个文件夹的 csv 文件,让您可以选择阅读每个文件并执行您需要做的事情,然后将文件移动到新文件夹。
private void ProcessFilesCSVFiles(string copyPath, string destinationPath)
{
// first check if path exists
if (!Directory.Exists(copyPath))
// doesn't exist then exit, can't copy from something that doesn't exist
return;
var copyPathDirectory = new DirectoryInfo(copyPath);
// using the SearchOption.AllDirectories will search sub directories
var copyPathCSVFiles = copyPathDirectory.GetFiles("*.csv", SearchOption.AllDirectories);
for(var i = 0; i < copyPathCSVFiles.Length; i++)
{
// get the file
var csvFile = copyPathCSVFiles[i];
// read the csv file line by line
using (StreamReader sr = csvFile.OpenText())
{
string line = "";
while ((line = sr.ReadLine()) != null)
{
// use split to read the individual columns
// will fail if the text field has a comma in it
var split = line.Split(',');
Console.WriteLine(line);
}
}
// do other sql mojo here if needed
// move the files over to another place
var destinationFilePath = Path.Combine(destinationPath, csvFile.Name);
if (File.Exists(destinationFilePath))
{
File.Delete(destinationFilePath);
}
csvFile.MoveTo(destinationFilePath);
}
}
可以这样调用:
ProcessFilesCSVFiles(@"C:\data\copypath", @"C:\data\destinationpath");