[Last Call] Learn how to a build a cloud-first strategyRegister Now


Convert SQL binary(64) to byte[]

Posted on 2007-10-20
Medium Priority
Last Modified: 2008-01-09
How to convert SQL binary(64) to byte[] in C# using SqlReader?
Question by:sepknow
  • 2
LVL 21

Expert Comment

ID: 20115369
Can you be more specific about what you want? A little more detail will help.

Expert Comment

ID: 20115417
Assuming you have a table with a column 'id' (integer) and a column 'data' (binary(64)), the following example reads each row sequentially and shows it in a message box:

                  using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString))

                        SqlCommand cmd = new SqlCommand("SELECT * from bindata", con);
                        using (SqlDataReader dtr = cmd.ExecuteReader())
                              while (dtr.Read())
                                    int id = (int)dtr["id"];
                                    byte[] data = (byte[])dtr["data"];

                                    MessageBox.Show(string.Format("{0} = {1}",

Author Comment

ID: 20116458
Hi surajguptha,
Let me re-phrase:
How to read a SQL column of datatype binary(64) using SqlReader?

discon0 solution is close to what I want.
To discon, how to retrieve using GetBytes()?


Accepted Solution

discon0 earned 2000 total points
ID: 20117121
Here's how to do it using GetBytes()

      byte[] data = new byte[64];            // Allocate 64 bytes
            dtr.GetOrdinal("data"),            // Column ordinal (index)
            0,                                          // start reading at byte 0
            data,                                    // write to the 'data' array
            0,                                          // start writing at index 0 inside 'data'
            64);                                    // 64 bytes

You can also use the way I've shown before:
      byte[] data = (byte[])dtr["data"];  // Use this only if the 'data' column can't be null

      byte[] data = dtr["data"] as byte[]; // Use this either way

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question