Click here to Skip to main content
14,265,882 members
Rate this:
Please Sign up or sign in to vote.
See more:

I need to store the System.Diagnostics.Stopwatch in database.

I managed to store it (as blob(byte)) and i can read it back as byte, but then i have problem to convert it back to the System.Diagnostics.Stopwatch object.

Any one that can help ?

I think the real issue is how to get the bytes back to the stopwatch object.

With Kind Regards

Updated 30-Jun-11 2:54am
Rate this:
Please Sign up or sign in to vote.

Solution 1

Try something like this:

Stopwatch stopWatch = (StopWatch)DataTable.Rows[0]["StopWatch"];

EDIT (after comment) ===========

Let me ask you this - why are you trying to save an entire object when you only need one or two properties? Can't you just change the column from a blob to a bigint in your db and make it easier on yourself? In fact, all you really need is the ElapsedTicks property, and you should be gold.
henur 30-Jun-11 8:39am
Well...i tried following
Stopwatch stw = (Stopwatch)(reader["sw"]); (I use MySql)
but then i get error: Unable to cast object of type 'System.Byte[]' to type 'System.Diagnostics.Stopwatch'.

So...i dont know how to get further.

Rate this:
Please Sign up or sign in to vote.

Solution 2

Your issue is two fold. First, John is right, storing an object like this is stupidity. What if your UI layer changes how it shows this data ? The only data you need, is the ticks. However, if you have used serialisation to get a byte presentation of the object, then you need to reverse that process to build the object out of the stream of bytes. If you didn't do that, I doubt the data you have in your DB represents a stopwatch object in the first place.
henur 3-Jul-11 5:26am
Ok :-)

Maybe it's stupid....

I am open for suggestions. Let me describe the needs instead.

I have stopwatches (maybe severals) starting in a application.
As long as the applications runs i have no problem to pause and restart.
However.....when the application closes for the workingday and then starts next time i need to show the stopwatches and restart with saved values(milliseconds or ticks....) but as i understand it, i cant set values to a stopwatch.

So...i the object-read it back- and restart.

But as i said...i am open for suggestions.

With Kind Regards


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100