使用C#,我想從DB生成1,000,000個文件,每個記錄都在單獨的文件中。 在最短時間內生成這些文件的最佳方式是什麼?從數據庫生成1,000,000個文件的最佳代碼
這裏是我的代碼,不必穿線:
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit); // to calculate the execution time in case of using threading
SqlCommand cmd = new SqlCommand(@"select top 1000000 p1+','+p2+','+p3+','+p4 as line from lines ", con);
con.Open();
var rdr = cmd.ExecuteReader();
int i = 0;
while (rdr.Read())
{
string line = rdr.Getring(0);
string filename = String.Format("file{0}.txt", ++i);
File.WriteAllText(filename, line);
}
rdr.Close();
con.Close();
您使用的是C#3.0嗎? – abhilash 2009-07-10 02:53:39
他不得不在他的代碼中使用var關鍵字。 – Kredns 2009-07-10 02:59:18
你真的希望他們都在一個目錄? – 2009-07-10 03:26:09