2016-05-23 7 views
-1

問題は解決しました。私はプライバシーの理由からコードを削除しました複数のcsvファイルをSQLサーバにアップロードする#

+0

をあなたはSQLサーバのディスクへの物理的なファイルをコピーしたい意味ですか、読んファイルからデータを取り出し、テーブルにデータを挿入するか、またはどのコードを試してみましたか、具体的には動作していないものはありますか?私はあなたがこれを細かく答えることができれば、回答をかなり簡単に見つけることができると思います(SQLデータベースにデータを挿入する方法、ファイルを移動する方法など) – DVK

答えて

1

私はあなたがによって何を意味するのか本当にわからないんだけど、私は1でファイルを読み取ることができるようにしたいけどは、csvファイルをループがするソリューションです1つのフォルダのファイルは、あなたが各ファイルを読んで、あなたが行う必要があることを行うためのオプションを与え、その後、新しいフォルダにファイルを移動します。

このような何か使用して呼び出されます
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); 
    } 
} 

:「SQL Serverに送る」と

ProcessFilesCSVFiles(@"C:\data\copypath", @"C:\data\destinationpath"); 
0

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); 
} 
関連する問題