Alexandru
Alexandru

Reputation: 12892

InvalidOperationException when trying to insert a string larger than 4000 chars into SQL Server CE database

The following is my logic (which adds a row of data to the database). This logic works, but when I have a string length larger than 4000 characters for log.Value.Message, it fails on rs.Insert. Why is that? How can I get around this? I tried searching the web but I couldn't find anything on this. The connection string will look something like this:

Data Source=C:\\File.sdf;Max Database Size=4000;Password=password;

Code:

SqlCeConnection conn = new SqlCeConnection(ConnectionString);
SqlCeCommand cmd = new SqlCeCommand();
SqlCeResultSet rs;
SqlCeUpdatableRecord rec;

conn.Open();
cmd.Connection = conn;
cmd.CommandText = "LogRecords";  // Table name.
cmd.CommandType = CommandType.TableDirect;
rs = cmd.ExecuteResultSet(ResultSetOptions.Updatable);
rec = rs.CreateRecord();

foreach (KeyValuePair<UInt64,LogRecord> log in v)
{
    rec.SetGuid(0, log.Value.ID);
    rec.SetSqlString(4, log.Value.Message);
    rs.Insert(rec);
}

This is the exception I see:

String truncation: max=4000, len=6850, value='[MY HUGE STRING FULL OF THE ENTIRE LOREM IPSUM TEXT, A LENGTH OF 6850]'.

This is how I create my database:

SqlCeEngine engine = new SqlCeEngine(ConnectionString);
engine.CreateDatabase();

Upvotes: 0

Views: 702

Answers (1)

Alexandru
Alexandru

Reputation: 12892

I figured it out, needed to set the typename, ie: [Column(TypeName = "ntext")] public string Body { get; set; }

Upvotes: 1

Related Questions