我使用以下代码创建了一个csv,当它工作时,我想命名列名称,任何人都不知道我会怎么做。
public void DumpTableToFile(SqlConnection connection, string tableName, string destinationFile)
{
using (var command = new SqlCommand("select * from " + tableName, connection))
using (var reader = command.ExecuteReader())
using (var outFile = File.CreateText(destinationFile))
{
string[] columnNames = GetColumnNames(reader).ToArray();
int numFields = columnNames.Length;
outFile.WriteLine(string.Join(",", columnNames));
if (reader.HasRows)
{
while (reader.Read())
{
string[] columnValues =
Enumerable.Range(0, numFields)
.Select(i => reader.GetValue(i).ToString())
.Select(field => string.Concat("\"", field.Replace("\"", "\"\""), "\""))
.ToArray();
outFile.WriteLine(string.Join(",", columnValues));
}
}
}
}
private IEnumerable<string> GetColumnNames(IDataReader reader)
{
foreach (DataRow row in reader.GetSchemaTable().Rows)
{
yield return (string)row["ColumnName"];
}
}
我supsect我可以在这里做一个if语句并转置它们。但是想知道somone是否有更好的主意。
答案 0 :(得分:1)
我建议首先为普通字符串提取方法为csv,第二方面为sql查询:
private static String ToCsv(String value) {
if (String.IsNullOrEmpty(value))
return "";
StringBuilder sb = new StringBuilder(value.Length);
bool hasSpecial = false;
foreach (var ch in value) {
if ((ch == ',') || (ch == '\\') || (ch == '"')) {
hasSpecial = true;
sb.Append('\\');
}
sb.Append(ch);
}
if (hasSpecial) {
sb.Insert(0, '"');
sb.Append('"');
}
return sb.ToString();
}
private static IEnumerable<String> TableToCsv(SqlConnection connection,
string tableName,
bool colummNames = true) {
//TODO: simplest, but prone to SQL injection solution; tableName should be validated
String sql = String.Format("select * from {0}", tableName);
using (var command = new SqlCommand(sql, connection))
using (var reader = command.ExecuteReader()) {
// Let's take columns' names from query
if (colummNames)
yield return String.Join(",", Enumerable
.Range(0, reader.FieldCount)
.Select(i => ToCsv(reader.GetName(i))));
while (reader.Read())
yield return String.Join(",", Enumerable
.Range(0, reader.FieldCount)
.Select(i => ToCsv(reader.GetFieldValue<String>(i))));
}
}
然后使用它:
File.WriteAllLines(@"C:\MyFile.csv",
TableToCsv(myConnection, "MyTable", true));