public void SaveShardAsCsv(Shard shard, string csvFolder, string csvSeparator, string encoding) { string stepid = Guid.NewGuid().ToString(); Logger.LogStepStart(stepid, shard.Name, "DOWNLOADING " + shard.Name); Console.WriteLine($"Downloading {shard.Name} on thread {Thread.CurrentThread.ManagedThreadId}"); SqlConnection sqlCon = new SqlConnection(this.sqlServerSourceConnStr); sqlCon.Open(); SqlCommand sqlCmd = new SqlCommand(shard.Sql, sqlCon); SqlDataReader reader = sqlCmd.ExecuteReader(); string path = csvFolder + shard.TableName + "\\"; string fileName = path + "\\" + shard.Name + ".csv"; Directory.CreateDirectory(path); StreamWriter sw = null; if (encoding == "UTF8") { sw = new StreamWriter(fileName, false, Encoding.UTF8); } else { sw = new StreamWriter(fileName, false, Encoding.Unicode); } object[] output = new object[reader.FieldCount]; for (int i = 0; i < reader.FieldCount; i++) { output[i] = reader.GetName(i); } sw.WriteLine(string.Join(",", output)); while (reader.Read()) { reader.GetValues(output); string row = ""; int counter = 0; foreach (object o in output) { string val = ""; if (reader.GetDataTypeName(counter) == "varchar" || reader.GetDataTypeName(counter) == "nvarchar") { val = "\"" + o.ToString().Replace("\"", "\"\"") + "\""; } else if (o.GetType() == typeof(bool)) { val = "\"" + (bool.Parse(o.ToString()) == false ? 0 : 1).ToString() + "\""; } else if (reader.GetDataTypeName(counter) == "decimal") { val = "\"" + o.ToString().Replace(",", ".") + "\""; } else if (o.GetType() == typeof(byte[])) { val = "\"\""; } else { val = "\"" + o.ToString() + "\""; } if (counter++ != output.Length - 1) { val += csvSeparator; } row += val; } sw.WriteLine(row); } sw.Flush(); Log.CsvBytesWritten += sw.BaseStream.Length; sw.Close(); reader.Close(); sqlCon.Close(); Logger.LogStepEnd(stepid); }
public void SaveShardAsCsv(Shard shard, string csvFolder, string csvSeparator) { SaveShardAsCsv(shard, csvFolder, csvSeparator, "Unicode"); }