将 varbinary 转换回 .txt 文件

发布于 2024-12-10 11:58:04 字数 351 浏览 0 评论 0原文

我有一个数据库(SQL 2008),用于存储文件。 它们保存为 varbinary(max) 类型。

现在我需要再次获取 .txt 文件,以便可以像以前使用 StreamReader 一样循环遍历该文件的内容。

while ((line = file.ReadLine()) != null)
{
string code = line.Substring(line.Length - 12);
}

但是如何将 varbinary byte[] 转换回正常的 .txt 文件,以便我能够逐行浏览内容。

我发现了一些关于内存流或文件流的想法,但无法让它们发挥作用。

提前致谢!

I have a database (SQL 2008) where I store file's in.
These are saved as a varbinary(max) type.

Now I need to get the .txt file again so I can loop through the contents of the file like i used to do with StreamReader.

while ((line = file.ReadLine()) != null)
{
string code = line.Substring(line.Length - 12);
}

But how can I convert the varbinary byte[] back to the normal .txt file so I'm able to go through the contents line by line.

I found some ideas with memorystream or filestream but can't get them to work.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过度放纵 2024-12-17 11:58:04
MemoryStream m = new MemoryStream(byteArrayFromDB);
StreamReader file = new StreamReader(m);
while ((line = file.ReadLine()) != null)
{
string code = line.Substring(line.Length - 12);
}
MemoryStream m = new MemoryStream(byteArrayFromDB);
StreamReader file = new StreamReader(m);
while ((line = file.ReadLine()) != null)
{
string code = line.Substring(line.Length - 12);
}
难得心□动 2024-12-17 11:58:04

cv 是一个 varbinary(max) 字段

SqlCommand sqlCmd = new SqlCommand("SELECT cv FROM [job].[UserInfo] Where ID = 39 ", conn); 
SqlDataReader reader = sqlCmd.ExecuteReader(); 

if (reader.Read() != null)
{
    byte[] buffer = (byte[])reader["cv"];
    File.WriteAllBytes("c:\\cv1.txt", buffer);
}

cv is a varbinary(max) field

SqlCommand sqlCmd = new SqlCommand("SELECT cv FROM [job].[UserInfo] Where ID = 39 ", conn); 
SqlDataReader reader = sqlCmd.ExecuteReader(); 

if (reader.Read() != null)
{
    byte[] buffer = (byte[])reader["cv"];
    File.WriteAllBytes("c:\\cv1.txt", buffer);
}
榕城若虚 2024-12-17 11:58:04

试试这个:

System.IO.File.WriteAllBytes("path to save your file", bytes);

try this:

System.IO.File.WriteAllBytes("path to save your file", bytes);
你是年少的欢喜 2024-12-17 11:58:04
    static void Main(string[] args)
    {
        GetData();
    }
    public static void GetData()
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["main"].ConnectionString);
        conn.Open();
        string query = "SELECT FileStream FROM [EventOne] Where  ID=2";
        SqlCommand cmd = new SqlCommand(query, conn);

        DataTable dt = new DataTable();
        dt.Load(cmd.ExecuteReader());
        byte[] buffer = dt.AsEnumerable().Select(c => c.Field<byte[]>("FileStream")).SingleOrDefault();
        File.WriteAllBytes("c:\\FileStream.txt", buffer);
    }
    static void Main(string[] args)
    {
        GetData();
    }
    public static void GetData()
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["main"].ConnectionString);
        conn.Open();
        string query = "SELECT FileStream FROM [EventOne] Where  ID=2";
        SqlCommand cmd = new SqlCommand(query, conn);

        DataTable dt = new DataTable();
        dt.Load(cmd.ExecuteReader());
        byte[] buffer = dt.AsEnumerable().Select(c => c.Field<byte[]>("FileStream")).SingleOrDefault();
        File.WriteAllBytes("c:\\FileStream.txt", buffer);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文