Beispiel #1
0
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object val, int length)
        {
            byte[] buffToWrite = null;

            if (val is System.Byte[])
            {
                buffToWrite = (byte[])val;
            }
            else if (val is Char[])
            {
                buffToWrite = stream.Encoding.GetBytes(val as char[]);
            }
            else
            {
                string s = val.ToString();
                if (length == 0)
                {
                    length = s.Length;
                }
                else
                {
                    s = s.Substring(0, length);
                }
                buffToWrite = stream.Encoding.GetBytes(s);
            }

            // we assume zero length means write all of the value
            if (length == 0)
            {
                length = buffToWrite.Length;
            }

            if (buffToWrite == null)
            {
                throw new MySqlException("Only byte arrays and strings can be serialized by MySqlBinary");
            }

            if (binary)
            {
                stream.WriteLength(length);
                stream.Write(buffToWrite, 0, length);
            }
            else
            {
                if (stream.Version.isAtLeast(4, 1, 0))
                {
                    stream.WriteStringNoNull("_binary ");
                }

                stream.WriteByte((byte)'\'');
                EscapeByteArray(buffToWrite, length, stream);
                stream.WriteByte((byte)'\'');
            }
        }
Beispiel #2
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);
     }
 }