【发布时间】:2022-07-20 23:22:37
【问题描述】:
我做了我的小项目(c# app)your text,它将数据从 sql 数据库导出到 csv 文件。
我想从 | 更改日期格式10.01.2022 至 -> 2022.01.10.
感谢您的帮助。
问候,
维生素
using System.Data;
using System.Data.SqlClient;
namespace ExtractApp
{
class Program
{
static void Main(string[] args)
{
GetCSV();
}
private static string GetCSV()
{
using (SqlConnection cn = new SqlConnection(GetConnectionString()))
{
cn.Open();
return CreateCSV(new SqlCommand("Select * from [reports].[xxx]", cn).ExecuteReader());
cn.Close();
}
}
private static string CreateCSV(IDataReader reader)
{
string file = "C:\\SqlToCsv\\Data.csv";
List<string> lines = new List<string>();
string headerLine = "";
if (reader.Read())
{
string[] columns = new string[reader.FieldCount];
for (int i = 0; i < reader.FieldCount; i++)
{
columns[i] = reader.GetName(i);
}
headerLine = string.Join(",", columns);
lines.Add(headerLine);
}
//data
while (reader.Read())
{
object[] values = new object[reader.FieldCount];
reader.GetValues(values);
lines.Add(string.Join(",", values));
}
//file
System.IO.File.WriteAllLines(file, lines);
return file;
}
}
}
【问题讨论】:
-
您是否使用日期时间类型将这些日期存储在您的数据库中?
-
只是约会
-
您使用的是哪个 dbms?
-
微软 sql 服务器管理 2018
标签: c# sql date format export-to-csv