Frage

I have a following problem. I have a table which has 26 columns. Some of the columns are tinyblob or blob columns. Data stored in those blob columns are in format:

0000 0010 0005 0400 0365 0000 0020 0041 0251 0023

I use the following code to get row of my selection:

            try

             {

                 string[] CurrentCons;
                 mysql.command = new OdbcCommand("SELECT * FROM current.cons_list WHERE name = ?", mysql.connection);
                 mysql.command.Parameters.Add("name", OdbcType.VarChar).Value = nick;
                 mysql.myReader = mysql.command.ExecuteReader();
                 CurrentCons = new string[mysql.myReader.FieldCount];
                 mysql.myReader.Read();

                 for (int i = 0; i < CurrentCons.Length; i++)
                 {
                     CurrentCons[i] = mysql.myReader[i].ToString();
                 }
                 return CurrentCons;
             }
             catch(Exception ex)
             {
                 string[] CurrentCons;
                 LOG.WriteLog("(" + DateTime.Now + ")\t" + ex.Message + "\t" + ex.Source + "\t" + ex.InnerException + "\n");
                 CurrentCons = new string[0];
                 return CurrentCons;
             }

What I want to do is to get tinyblob as byte array and call it like one, in example

byte[] MyBLOB = functions.GetData("John213");
MessageBox.Show(string.format("{0}", MyBLOB[0])); //0
MessageBox.Show(string.format("{0}", MyBLOB[2])); //5
MessageBox.Show(string.format("{0}", MyBLOB[4])); //869

Is that possible? Thank you in advance.

War es hilfreich?

Lösung

The way to read a blob into a byte array is through the GetBytes() method

int length; //the width of the blob
byte[] buffer = new byte[length];
reader.GetBytes(reader.GetOrdinal("John123"), 0, buffer, 0, length);

MSDN Documentation for the method

FYI, GetBytes() is available on most DataReader classes in C#.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top