-1
我的問題已解決。我爲了隱私原因取下了我的代碼在c中將多個csv文件上傳到SQL服務器#
我的問題已解決。我爲了隱私原因取下了我的代碼在c中將多個csv文件上傳到SQL服務器#
我真的不知道你的意味着什麼,我希望能夠在三個不同的文件夾,但這裏的時間在一個讀取這些文件是一個解決方案,它通過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");
您需要進行一些更改,例如將文件擴展名更改爲「.csv」並更改目標文件路徑。
foreach (var file in d.GetFiles("*.csv"))
{
//Load CSV into SQL using existing code
Directory.Move(file.FullName, filepath + "\\TextFiles\\" + file.Name);
}
通過「發送到SQL Server」你的意思是你想要的物理文件複製到SQL服務器的磁盤,請閱讀來自文件的數據並將數據插入到表中,或者...您嘗試了哪些代碼以及哪些具體操作不起作用?我想如果你把這個問題分解成具體的,可回答的問題,你很可能很容易找到答案(如何將數據插入sql數據庫,如何移動文件等) – DVK