1

Im trying to retrieve a query as a byte array. Im stuck @ the code though.

byte[] temp = QueryFile("SELECT modelFile FROM items WHERE modelName='F_Pants1'");
    public byte[] QueryFile(string queue)
            {
                MySqlCommand command = MySqlCon.CreateCommand();
                MySqlDataReader Reader;
                command.CommandText = queue;
                MySqlCon.Open();
                Reader = command.ExecuteReader();
                byte[] thisrow = new byte[1000];
                while (Reader.Read())
                {
                    for (int i = 0; i < Reader.FieldCount; i++)
                        thisrow[0] = Convert.ToByte(Reader.GetValue(i).ToString());


                }
                thisrow = thisrow.Remove(thisrow.Length - 1, 1);
                MySqlCon.Close();

                return thisrow;

            }

If anyone has an answer, i would greatly appreciate it.

2
  • functions that accept completed sql queries as strings will lead to sql injection security issues. Commented Nov 29, 2011 at 4:22
  • SQL Injection if the function will get an un-checked strings. Commented Nov 29, 2011 at 7:42

1 Answer 1

4

You're making it a bit too complicated. Try this instead:

       try {
         Reader = command.ExecuteReader();
         if (Reader.Read())
          {
              return Reader.GetValue(0) as Byte[];
          }
        } finally {
           MySqlCon.Close();
        }
Sign up to request clarification or add additional context in comments.

2 Comments

This is also too complicated - he can just use .ExecuteScalar()
ill change the function later but it returns null when i query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.