我有一个制表符分隔的文本文件,其中一些十进制值的格式如下:0,12(逗号作为小数分隔符)。我通过 Provider=Microsoft.Jet.OLEDB.4.0 将其内容解析为 OleDBDataReader,但是当我查看结果时,应该是十进制的值被转换为 DateTime!
如何“强制”我的应用程序将逗号理解为数字的小数分隔符?无法将cultureinfo 更改为en-US 或任何其他。
public static OleDbDataReader CriarOleDbDataReader(string sCaminhoArquivo)
{
FileInfo fi = new FileInfo(sCaminhoArquivo);
string format;
string linha;
if (fi.Extension.ToLower().Equals(".csv"))
{
string texto = System.IO.File.ReadAllText(sCaminhoArquivo).Replace(',', ';');
System.IO.File.WriteAllText(sCaminhoArquivo, texto);
linha = System.IO.File.ReadAllLines(sCaminhoArquivo)[0];
format = "Delimited(;)";
}
else
{
linha = System.IO.File.ReadAllLines(sCaminhoArquivo)[0];
format = (linha.Split('\t').Count() > 1) ? "TabDelimited" : "Delimited(;)";
}
DefinirSchema(fi, format);
string cn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fi.DirectoryName + ";Extended Properties='text;HDR=Yes';";
OleDbConnection con = new OleDbConnection(cn);
OleDbCommand cmd = new OleDbCommand("select * from [" + fi.Name + "]", con);
OleDbDataReader dr;
con.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
字段“IOF”是一个小数,它在 .txt 文件中的值为 0,02,但它返回日期时间。
dr["iof"]
{30/12/1899 00:02:00}
Date: {30/12/1899 00:00:00}
Day: 30
DayOfWeek: Saturday
DayOfYear: 364
Hour: 0
Kind: Unspecified
Millisecond: 0
Minute: 2
Month: 12
Second: 0
Ticks: 599264353200000000
TimeOfDay: {00:02:00}
Year: 1899