I have some LinqToSQL objects. One of those has a 'Binary' column, I think it's VarBinary(max) on SQL.
Every now and then I need to add/concatenate a byte array into that column for a certain row. This is how I do it:
void StoreBytesInDb(IEnumerable<byte> bytesToStore)
{
using (var dataBaseContext = new MyDataClasses(DatabaseInfo.ConnectionString))
{
if (this.currentObjectId == -1)
{
this.NewObjectToDatabase();
this.currentSavedBytes = new List<byte>();
}
this.CurrentObject = dataBaseContext.Objects.Single(e => e.Id_Object == this.currentObjectId);
this.currentSavedBytes.AddRange(bytesToStore);
this.CurrentObject.Bytes_Data = this.currentSavedBytes.ToArray();
dataBaseContext.SubmitChanges();
}
}
So, as you can see:
At low speeds it works.
The problem appears when I speed it up to several times per second. Sometimes it throws a 'System.ArgumentException' saying: "An item with the same key has already been added." The exact line that throws the exception is:
this.CurrentObject.Bytes_Data = currentSavedBytes.ToArray();
Any tips on how to improve this?
If it's larger byte data you're dealing with - maybe move it out of Db , keep a ref onto File System - and append there - which is better.
On the other thing - just an idea...
currentSavedBytes
- why you keep that as a member? may be a concurency issue (if sharing) - try moving that in the scope of the method.
None of this is optimal.
I would suggest you to:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.