从 SQL Server varbinary 列临时检索数据

发布于 2024-08-30 13:02:37 字数 211 浏览 3 评论 0 原文

我想从 SQL Server 数据库中的 varbinary(max) 列检索一些二进制数据以进行调试。

将此数据放入本地二进制文件的最简单方法是什么,最好无需编写一次性控制台应用程序?

我尝试使用 SQL Server Management Studio(使用“结果到文件”选项),但这会向文件输出十六进制编码的二进制字符串,而不是原始二进制数据。

I would like to retreive some binary data from a varbinary(max) column in a SQL Server database for debugging purposes.

What is the easiest way to get this data into a local binary file, preferably without having to write a throw-away console application?

I have tried using SQL Server Management Studio (with the "results to file" option) but this outputs a hex encoded binary string to the file, rather than the raw binary data.

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

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

发布评论

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

评论(3

老旧海报 2024-09-06 13:02:37

我想不出比扔掉一些 C# 更简单的方法了...

    static void Main(string[] args)
    {
        GetBinaryDataToFile("Server=localhost;Initial Catalog=ReportServer;Integrated Security=true", "D:\\temp.dat");
    }

    public static void GetBinaryDataToFile(string connectionString, string path)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();

            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText = "SELECT Sid FROM Users WHERE UserID = '62066184-8403-494E-A571-438ABF938A4F'";
                command.CommandType = CommandType.Text;

                using (SqlDataReader dataReader = command.ExecuteReader())
                {
                    if (dataReader.Read())
                    {
                        SqlBinary sqlBinary = dataReader.GetSqlBinary(0);
                        File.WriteAllBytes(path, sqlBinary.Value);
                    }

                    dataReader.Close();
                }
            }

            connection.Close();
        }
    }

此代码已在带有 Reporting Services 的 SQL Server 默认安装中使用 Users.Sid​​ 列(属于 varbinary 类型)进行了测试。

I can't think of any easier way to do this than a throw away bit of C#...

    static void Main(string[] args)
    {
        GetBinaryDataToFile("Server=localhost;Initial Catalog=ReportServer;Integrated Security=true", "D:\\temp.dat");
    }

    public static void GetBinaryDataToFile(string connectionString, string path)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();

            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText = "SELECT Sid FROM Users WHERE UserID = '62066184-8403-494E-A571-438ABF938A4F'";
                command.CommandType = CommandType.Text;

                using (SqlDataReader dataReader = command.ExecuteReader())
                {
                    if (dataReader.Read())
                    {
                        SqlBinary sqlBinary = dataReader.GetSqlBinary(0);
                        File.WriteAllBytes(path, sqlBinary.Value);
                    }

                    dataReader.Close();
                }
            }

            connection.Close();
        }
    }

This code has been tested using the Users.Sid column (which is of varbinary type) in a default installation of SQL Server wih Reporting Services.

假扮的天使 2024-09-06 13:02:37

我喜欢 LinqPad 的建议。我尝试了一下,并有一个查询在 10 分钟内将二进制文件吐出到一个文件中。没有 VS 项目,没有构建 - 现在脚本已保存,我可以随时提取它。太酷了!

LinqPad 脚本:

var g = from pd in Archives 
    where pd.ArchiveId == 123
    select pd;

var q = from p in printDocs
where p.DocumentId == g.SingleOrDefault().DocumentId
select p;

File.WriteAllBytes("C:\\temp.pdf", q.SingleOrDefault().Pdf.ToArray());

I loved the LinqPad suggestion. I tried it and had a query that spit out the binary to a file within 10 minutes. No VS Project, no build - and now the script is saved and I can pull it up anytime. So cool!

LinqPad script:

var g = from pd in Archives 
    where pd.ArchiveId == 123
    select pd;

var q = from p in printDocs
where p.DocumentId == g.SingleOrDefault().DocumentId
select p;

File.WriteAllBytes("C:\\temp.pdf", q.SingleOrDefault().Pdf.ToArray());
墨小沫ゞ 2024-09-06 13:02:37

我找到了这个解决方案使用 bcp 命令(从命令提示符运行):

c:\temp>bcp "select MYVARBINARYCOL from MYTABLE where id = 1234" queryout "c:\filename.pdf" -S MYSQLSERVER\MYINSTANCE -T

Enter the file storage type of field filedata [varbinary(max)]:
Enter prefix-length of field filedata [8]: 0
Enter length of field filedata [0]:
Enter field terminator [none]:

Do you want to save this format information in a file? [Y/n] n

Starting copy...

1 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total : 15 Average : (66.67 rows per sec.)

我使用 -T 选项来使用 Windows 身份验证来连接到数据库。如果您使用密码身份验证,则需要使用 -U 和 -P 开关来指定用户名和密码。

但我也喜欢 LinqPad 建议/281048">罗布·萨德勒的回答并且不知何故更喜欢它。

I've found this solution with bcp command (run from command prompt):

c:\temp>bcp "select MYVARBINARYCOL from MYTABLE where id = 1234" queryout "c:\filename.pdf" -S MYSQLSERVER\MYINSTANCE -T

Enter the file storage type of field filedata [varbinary(max)]:
Enter prefix-length of field filedata [8]: 0
Enter length of field filedata [0]:
Enter field terminator [none]:

Do you want to save this format information in a file? [Y/n] n

Starting copy...

1 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total : 15 Average : (66.67 rows per sec.)

I used the -T option to use windows authentication to connect to the DB. If you use password auth, you'll need to use the -U and -P switches to specify a username and password.

But I also like LinqPad suggestion in Robb Sadler's answer and somehow prefer it.

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