Ejemplo n.º 1
0
 void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object val, int length)
 {
     byte[] bytes = null;
     if (val is byte[])
     {
         bytes = (byte[]) val;
     }
     else if (val is char[])
     {
         bytes = stream.Encoding.GetBytes(val as char[]);
     }
     else
     {
         string s = val.ToString();
         if (length == 0)
         {
             length = s.Length;
         }
         else
         {
             s = s.Substring(0, length);
         }
         bytes = stream.Encoding.GetBytes(s);
     }
     if (length == 0)
     {
         length = bytes.Length;
     }
     if (bytes == null)
     {
         throw new MySqlException("Only byte arrays and strings can be serialized by MySqlBinary");
     }
     if (binary)
     {
         stream.WriteLength((long) length);
         stream.Write(bytes, 0, length);
     }
     else
     {
         if (stream.Version.isAtLeast(4, 1, 0))
         {
             stream.WriteStringNoNull("_binary ");
         }
         stream.WriteByte(0x27);
         this.EscapeByteArray(bytes, length, stream);
         stream.WriteByte(0x27);
     }
 }