使用微软企业库获取sql varbinary记录

发布于 2024-09-12 03:35:18 字数 277 浏览 3 评论 0原文

我有这行代码

byte[] field1 = (reader.GetSqlBinary(reader.GetOrdinal("Field1")).Value;

,它是一个 SqlDataReader

我正在尝试将代码转换为使用企业库数据访问块,但无法弄清楚如何获取 byte[]使用IDataReader

我仔细浏览了 MS 文档,但没有找到任何有帮助的内容。

I have this line of code

byte[] field1 = (reader.GetSqlBinary(reader.GetOrdinal("Field1")).Value;

which is a SqlDataReader

I am trying to convert the code to use the enterprise library data access block but cant figure out how to get a byte[] using the IDataReader.

I've had a good look through the MS docs but failed to find anything that helped.

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

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

发布评论

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

评论(1

深府石板幽径 2024-09-19 03:35:19

我要尝试的第一件事是(其中 ireader.GetOrdinal("Field1")):

    byte[] firstTry = (byte[])reader.GetValue(i);

如果失败,也许:

byte[] secondTry;
using (var ms = new MemoryStream())
{
    byte[] buffer = new byte[8040]; // sql page size
    int read;
    long offset = 0;
    while ((read = (int)reader.GetBytes(i, offset, buffer, 0, buffer.Length)) > 0)
    {
        ms.Write(buffer, 0, read);
        offset += read; // oops! added this later... kinda important
    }
    secondTry = ms.ToArray();
}

另请注意,该行为可能 根据是否指定 CommandBehavior.SequentialAccess 略有变化。

The first thing I would try is (where i is reader.GetOrdinal("Field1")):

    byte[] firstTry = (byte[])reader.GetValue(i);

If that fails, perhaps:

byte[] secondTry;
using (var ms = new MemoryStream())
{
    byte[] buffer = new byte[8040]; // sql page size
    int read;
    long offset = 0;
    while ((read = (int)reader.GetBytes(i, offset, buffer, 0, buffer.Length)) > 0)
    {
        ms.Write(buffer, 0, read);
        offset += read; // oops! added this later... kinda important
    }
    secondTry = ms.ToArray();
}

Note also that the behaviour may change slightly depending on whether CommandBehavior.SequentialAccess is specified.

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