This code serializes an array of integers then inserts it into a sql table. It isn't as fast as I need it to be. Could I be doing anything more efficiently?
Thanks!
public void SetItem(long Id, int[] items)
{
using (MemoryStream stream = new MemoryStream())
{
foreach (int d in items)
{
var bin = BitConverter.GetBytes(d); //Serialize
stream.Write(bin, 0, bin.Length);
}
var array = stream.ToArray();
using (SqlCommand cmd = new SqlCommand("INSERT INTO Items(Id, Item, DateCreated) VALUES (@Id, @binaryValue, @dateCreated)", sqlConnection))
{
cmd.Parameters.Add("@binaryValue", SqlDbType.VarBinary, array.Length).Value = array;
cmd.Parameters.Add("@Id", SqlDbType.BigInt).Value = Id;
cmd.Parameters.Add("@dateCreated", SqlDbType.DateTime2).Value = DateTime.Now;
cmd.ExecuteNonQuery();
}
}
}
I advise you to divide this function in two. One regarding the byte array the other for insertion into the DB.
Then run profiling and see if you byte array code is slow or if it is a db problem.
Maybe you are trying to accelerate something that isn't slow :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With