Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello

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

Heino
Posted
Updated 30-Jun-11 2:54am
v2

Try something like this:

C#
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.
 
Share this answer
 
v3
Comments
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.

Heino
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.
 
Share this answer
 
Comments
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 thought....save the object-read it back- and restart.

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

With Kind Regards

Heino

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900