简体   繁体   中英

How to concatenate a byte array into an existing one inside a Database

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:

  1. I use Linq to "AddRange" into the saved bytes (in the heap).
  2. Then I put it into the right column in the object.
  3. Submit Changes.

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.

  1. You should not store big files in the database
  2. Linq 2 Sql is not optimized to do it
  3. Also, it~s not a good idea to handle files all at once in memory - they might be big and you get and OOM exception

I would suggest you to:

  1. Save the files somewhere else like @NSGaga said in his comment
  2. If you must save in the database at least use a FileStream column (which is pretty much saving into the database disk).
  3. Do it like explained in this article so you are able to append to the file

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM