Convert SQL binary(64) to byte[]

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

    Expert Comment

    Can you be more specific about what you want? A little more detail will help.
    LVL 5

    Expert Comment

    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

    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()?

    LVL 5

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    Via a live example, show how to shrink a transaction log file down to a reasonable size.
    Viewers will learn how the fundamental information of how to create a table.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now