内存中的TSQL VarBinary 到文本文件?

发布于 2024-10-11 17:10:08 字数 762 浏览 4 评论 0 原文

在 SQL Server 2008 中,我有一个接受 VarBinary 参数的存储过程。当参数在内存中时,我想将其写入 SQL Server 的 %temp% 目录。

如何从内存 blob(它来自 C# 应用程序的 byte[])到文本文件?该 blob 不存在于任何磁盘驻留表中。

我倾向于让 TSQL 存储过程调用 CLR 存储过程,到目前为止(大部分)看起来如下所示。

问题#1 是我认为 inputBytes[] 停留在 8k 输入大小。

问题#2 是我似乎无法想出一些像样的 SQL 来传递 varbinary 值作为参数,因此我可以在 Management Studio 或 VS DB 单元测试中测试它。

public static void FileWrite( string inputName , byte[] inputBytes )
using (FileStream fileStream = new    
   FileStream(Environment.GetEnvironmentVariable("TEMP") + "\\" + inputName,   
   FileMode.OpenOrCreate, FileAccess.Write))
   {
        using ( BinaryWriter binaryWriter = new BinaryWriter(fileStream))
        {
           binaryWriter.Write( inputBytes );
    }
}

谢谢。

With SQL Server 2008, I have a stored procedure that accepts a VarBinary parameter. While the parameter is in memory, I would like to write it to the %temp% directory of the SQL Server.

How can I go from memory blob (it came as a byte[] from a C# app) to text file? The blob does not exist in any disk-resident table.

I am leaning towards having the TSQL sproc call a CLR stored procedure, which so far looks (mostly) like the below.

Problem #1 is that I think the inputBytes[] is stuck at 8k input size.

Problem #2 is that I can't seem to come up with some decent SQL that passes a varbinary value as a parameter so I can test this within Management Studio or VS DB unit test.

public static void FileWrite( string inputName , byte[] inputBytes )
using (FileStream fileStream = new    
   FileStream(Environment.GetEnvironmentVariable("TEMP") + "\\" + inputName,   
   FileMode.OpenOrCreate, FileAccess.Write))
   {
        using ( BinaryWriter binaryWriter = new BinaryWriter(fileStream))
        {
           binaryWriter.Write( inputBytes );
    }
}

Thanks.

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

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

发布评论

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

评论(1

救星 2024-10-18 17:10:08

如果 C# 代码中有 byte[],则可以轻松写入文件:

http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspx

这对您有用吗?

编辑:如果必须从 SQL Server 启动文件保存,另一个想法可能是创建一个 SQL Server CLR 函数。这将允许您从 SQL Server 执行 C# 代码。

编辑2:我进行了Google搜索,发现了一篇关于CodeProject的文章。在本文中,他们正在压缩和解压缩 blob 数据。我认为这里的技巧是使用 SqlBytes 参数类型与普通的旧字节[]。以下是我在阅读本文和您更新的问题后想到的一些(未经测试的)代码:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

using System.IO;
using System.IO.Compression;

public partial class UserDefinedFunctions
{
    // Setting function characteristics
    [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic=true, DataAccess=DataAccessKind.None)]
    public static void fn_WriteToFile(string fileName, SqlBytes blob)
    {
        if( blob.IsNull || String.IsNullOrEmpty(fileName) )
        {
            return;
        }

        String fullFileName = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), fileName);
        using( FileStream fileStream = new  FileStream(fullFileName, FileMode.OpenOrCreate, FileAccess.Write))
        using( BinaryWriter binaryWriter = new BinaryWriter(fileStream) )
        {
            binaryWriter.Write(blob.Buffer);
        }
    }
};

让我知道您的想法。

If you have the byte[] in C# code, you could easily write to a file:

http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspx

Would this work for you?

EDIT: Another idea if you must initiate the file save from SQL Server might be to create a SQL Server CLR Function. This would allow you to execute C# code from SQL Server.

EDIT 2: I did a Google search and found an article on CodeProject. In the article, they are compressing and decompressing blob data. I think the trick here is to use the SqlBytes parameter type vs a plain old byte[]. Below is some (untested) code I came up with after reading the article and your updated question:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

using System.IO;
using System.IO.Compression;

public partial class UserDefinedFunctions
{
    // Setting function characteristics
    [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic=true, DataAccess=DataAccessKind.None)]
    public static void fn_WriteToFile(string fileName, SqlBytes blob)
    {
        if( blob.IsNull || String.IsNullOrEmpty(fileName) )
        {
            return;
        }

        String fullFileName = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), fileName);
        using( FileStream fileStream = new  FileStream(fullFileName, FileMode.OpenOrCreate, FileAccess.Write))
        using( BinaryWriter binaryWriter = new BinaryWriter(fileStream) )
        {
            binaryWriter.Write(blob.Buffer);
        }
    }
};

Let me know what you think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文