[英]Reading content from a text file saved to a byte field in SQL Server
我之间缺少一些东西。 我将CSV文件内容存储在数据库中,而不是文件中。 下面的第二行是错误的,我想读取字节数据并将其分配给行数组。 因此,我可以像直接从磁盘上的文件中读取一样循环结果。 非常感谢...
FileContentResult x23File = File(x23.FileData,
"application/text", x23.Filename);
string[] Lines = System.Text.Encoding.UTF8.GetString(x23File);
如果我们假设x23.FileData
是byte[]
,则您可能需要:
List<string> lines = new List<string>();
using(var ms = new MemoryStream(x23.FileData))
using(var reader = new StreamReader(ms, Encoding.UTF8))
{
string line;
while((line = reader.ReadLine()) != null)
lines.Add(line);
}
现在, lines
具有所有单独的行。 请注意,您还可以通过IEnumerable<string>
以非缓冲方式使用此数据。 例如:
static IEnumerable<string> ReadLines(byte[] source, Encoding enc = null)
{
using(var ms = new MemoryStream(source))
using(var reader = new StreamReader(ms, enc ?? Encoding.UTF8))
{
string line;
while((line = reader.ReadLine()) != null)
yield return line;
}
}
因为x23File
不是byte[]
,所以x23File
了此错误。 您的第二行实际上应该是
string[] Lines = System.Text.Encoding.UTF8.GetString(x23File.FileContents).Split(new String[]{"\\r\\n"}, StringSplitOptions.None);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.