如何从数据库中读取TXT文件(逐行) [英] how to read the txt file from the database(line by line)

查看:144
本文介绍了如何从数据库中读取TXT文件(逐行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已存储的txt文件到SQL Server数据库。
我需要逐行读取TXT文件一线得到其中的内容。
 我的code:

i have stored the txt file to sql server database . i need to read the txt file line by line to get the content in it. my code :

DataTable dtDeleteFolderFile = new DataTable();
dtDeleteFolderFile = objutility.GetData("GetTxtFileonFileName", new object[] { ddlSelectFile.SelectedItem.Text }).Tables[0];

foreach (DataRow dr in dtDeleteFolderFile.Rows)
{
  name = dr["FileName"].ToString();
  records = Convert.ToInt32(dr["NoOfRecords"].ToString());
  bytes = (Byte[])dr["Data"];
}

FileStream readfile = new FileStream(Server.MapPath("txtfiles/" + name), FileMode.Open);

StreamReader streamreader = new StreamReader(readfile);
string line = "";
line = streamreader.ReadLine();

但在这里我使用了的FileStream 从特定路径阅读。但我已经保存在字节格式txt文件到我的数据库。如何使用的byte []值txt文件内容,而不是使用路径值读取TXT文件。

but here i have used the FileStream to read from the Particular path. but i have saved the txt file in byte format into my Database. how to read the txt file using the byte[] value to get the txt file content, instead of using the Path value.

推荐答案

由于个事实,你有一个字节数组的文件,你可以利用的的MemoryStream类

Given th fact that you have the file in a byte array, you can make use of MemoryStream Class

using (MemoryStream m = new MemoryStream(buffer))
using (StreamReader sr = new StreamReader(m))
{
    while (!sr.EndOfStream)
    {
        string s = sr.ReadLine();
    }    
}

另外,还要确保使用 using语句(C#参考)

定义范围,其中外
  一个或多个对象将被全部销毁。

Defines a scope, outside of which an object or objects will be disposed.

using语句允许
  程序员指定的对象时,
  即利用资源应该释放
  他们。提供给使用对象
  声明必须实现
  IDisposable接口。此接口
  提供了Dispose方法,这
  应该释放该对象的资源。

The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.

这篇关于如何从数据库中读取TXT文件(逐行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆