mardi 25 août 2015

SQL select only returns part of a text field

I currently have a table with two TEXT columns. One of these columns is a serialized object using Protobuf-net and seems to be able to be inserted into the table fine (Checked the sqlite file in notepad). My problem is that when I retrieve this TEXT item, it only returns the first half of the TEXT!

How can this happen?

My code:

public void save(SQLiteConnection m_dbConnection, Day day)
{
  using (MemoryStream stream = new MemoryStream())
  {
    Serializer.Serialize<Day>(stream, day);


    stream.Position = 0;
    StreamReader sr = new StreamReader(stream);
    string data = sr.ReadToEnd();
    string sql = "INSERT INTO mytable (date, data) VALUES (@date, @data)";
    SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
    command.CommandType = System.Data.CommandType.Text;
    command.Parameters.Add(new SQLiteParameter("@date", day.getSqlDate()));
    command.Parameters.Add(new SQLiteParameter("@data", data));
    command.ExecuteNonQuery();
  }
}

public Day retrieve(SQLiteConnection m_dbConnection)
{
  string sql = "SELECT * FROM mytable ORDER BY date desc";
  SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
  SQLiteDataReader reader = command.ExecuteReader();

  string str = "";
  while (reader.Read())
  {
    //HERE: Only first half of the serialized object
    str = reader["data"].ToString();
    using (Stream stream = GenerateStreamFromString(str))
    {
      return Serializer.Deserialize<Day>(stream);
    }
  }
  return null;
}

public Stream GenerateStreamFromString(string s)
{
  MemoryStream stream = new MemoryStream();
  StreamWriter writer = new StreamWriter(stream);
  writer.Write(s);
  writer.Flush();
  stream.Position = 0;
  return stream;
}

Aucun commentaire:

Enregistrer un commentaire