473,395 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Coustom Counter

I'm a VB6 convert with 6 months into VB2005... I'm a Database developer and
like to roll my own "counters" for my front end programs. Instead of using
IDENTITY I use my counters.
Is this code even close to being efficient or am I off base. It does work
though... I just want to get off the ground correctly with the new VB
Thanks
Paul

'-----------------------------------------------------------------------------------------------------------
Function Increment_Counter() As Integer
Dim sFile As String = "L:\Warranty\Secure\123.dat"

Dim CurrentValue As Integer = 0
Dim NewValue As Integer = 0
Dim fs As New System.IO.FileStream(sFile, IO.FileMode.Open,
IO.FileAccess.ReadWrite, IO.FileShare.None)
Dim r As New IO.BinaryReader(fs)

fs.Lock(0, 5)

CurrentValue = (r.ReadInt32())

fs.Unlock(0, 5)
fs.Close()

fs = New System.IO.FileStream(sFile, IO.FileMode.Open,
IO.FileAccess.ReadWrite)

Dim w As New IO.BinaryWriter(fs)

NewValue = CurrentValue + 1
fs.Lock(0, 5)
w.Write(NewValue)
r = New IO.BinaryReader(fs)
CurrentValue = (r.ReadInt32())
fs.Unlock(0, 5)
r.Close()
w.Close()
fs.Close()
Return NewValue

End Function
'-----------------------------------------------------------------------------------------------------------
Jun 27 '08 #1
4 813
Paul,

You are sure that you don't want to use the isolated storage?

http://msdn2.microsoft.com/en-us/lib...k0(VS.71).aspx

I got that idea that you was on that route?

Cor

"Paul Ilacqua" <pi******@twcny.rr.comschreef in bericht
news:es**************@TK2MSFTNGP04.phx.gbl...
I'm a VB6 convert with 6 months into VB2005... I'm a Database developer
and like to roll my own "counters" for my front end programs. Instead of
using IDENTITY I use my counters.
Is this code even close to being efficient or am I off base. It does work
though... I just want to get off the ground correctly with the new VB
Thanks
Paul

'-----------------------------------------------------------------------------------------------------------
Function Increment_Counter() As Integer
Dim sFile As String = "L:\Warranty\Secure\123.dat"

Dim CurrentValue As Integer = 0
Dim NewValue As Integer = 0
Dim fs As New System.IO.FileStream(sFile, IO.FileMode.Open,
IO.FileAccess.ReadWrite, IO.FileShare.None)
Dim r As New IO.BinaryReader(fs)

fs.Lock(0, 5)

CurrentValue = (r.ReadInt32())

fs.Unlock(0, 5)
fs.Close()

fs = New System.IO.FileStream(sFile, IO.FileMode.Open,
IO.FileAccess.ReadWrite)

Dim w As New IO.BinaryWriter(fs)

NewValue = CurrentValue + 1
fs.Lock(0, 5)
w.Write(NewValue)
r = New IO.BinaryReader(fs)
CurrentValue = (r.ReadInt32())
fs.Unlock(0, 5)
r.Close()
w.Close()
fs.Close()
Return NewValue

End Function
'-----------------------------------------------------------------------------------------------------------

Jun 27 '08 #2
Cor....
I will look at isolated storage... I wasn't aware of it... In your opine,
is a file as I describe better offf in isolated storage?
Thanks again
Paul

"Cor Ligthert[MVP]" <no************@planet.nlwrote in message
news:5E**********************************@microsof t.com...
Paul,

You are sure that you don't want to use the isolated storage?

http://msdn2.microsoft.com/en-us/lib...k0(VS.71).aspx

I got that idea that you was on that route?

Cor

"Paul Ilacqua" <pi******@twcny.rr.comschreef in bericht
news:es**************@TK2MSFTNGP04.phx.gbl...
>I'm a VB6 convert with 6 months into VB2005... I'm a Database developer
and like to roll my own "counters" for my front end programs. Instead of
using IDENTITY I use my counters.
Is this code even close to being efficient or am I off base. It does work
though... I just want to get off the ground correctly with the new VB
Thanks
Paul

'-----------------------------------------------------------------------------------------------------------
Function Increment_Counter() As Integer
Dim sFile As String = "L:\Warranty\Secure\123.dat"

Dim CurrentValue As Integer = 0
Dim NewValue As Integer = 0
Dim fs As New System.IO.FileStream(sFile, IO.FileMode.Open,
IO.FileAccess.ReadWrite, IO.FileShare.None)
Dim r As New IO.BinaryReader(fs)

fs.Lock(0, 5)

CurrentValue = (r.ReadInt32())

fs.Unlock(0, 5)
fs.Close()

fs = New System.IO.FileStream(sFile, IO.FileMode.Open,
IO.FileAccess.ReadWrite)

Dim w As New IO.BinaryWriter(fs)

NewValue = CurrentValue + 1
fs.Lock(0, 5)
w.Write(NewValue)
r = New IO.BinaryReader(fs)
CurrentValue = (r.ReadInt32())
fs.Unlock(0, 5)
r.Close()
w.Close()
fs.Close()
Return NewValue

End Function
'-----------------------------------------------------------------------------------------------------------


Jun 27 '08 #3
Here some more info

http://msdn2.microsoft.com/en-us/lib...sy(VS.80).aspx

Note that isolated storage works with user profiles so user A has a
different storage location as user B
HTH

Michel
"Paul Ilacqua" <pi******@twcny.rr.comschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
Cor....
I will look at isolated storage... I wasn't aware of it... In your opine,
is a file as I describe better offf in isolated storage?
Thanks again
Paul

"Cor Ligthert[MVP]" <no************@planet.nlwrote in message
news:5E**********************************@microsof t.com...
>Paul,

You are sure that you don't want to use the isolated storage?

http://msdn2.microsoft.com/en-us/lib...k0(VS.71).aspx

I got that idea that you was on that route?

Cor

"Paul Ilacqua" <pi******@twcny.rr.comschreef in bericht
news:es**************@TK2MSFTNGP04.phx.gbl...
>>I'm a VB6 convert with 6 months into VB2005... I'm a Database developer
and like to roll my own "counters" for my front end programs. Instead of
using IDENTITY I use my counters.
Is this code even close to being efficient or am I off base. It does
work though... I just want to get off the ground correctly with the new
VB
Thanks
Paul

'-----------------------------------------------------------------------------------------------------------
Function Increment_Counter() As Integer
Dim sFile As String = "L:\Warranty\Secure\123.dat"

Dim CurrentValue As Integer = 0
Dim NewValue As Integer = 0
Dim fs As New System.IO.FileStream(sFile, IO.FileMode.Open,
IO.FileAccess.ReadWrite, IO.FileShare.None)
Dim r As New IO.BinaryReader(fs)

fs.Lock(0, 5)

CurrentValue = (r.ReadInt32())

fs.Unlock(0, 5)
fs.Close()

fs = New System.IO.FileStream(sFile, IO.FileMode.Open,
IO.FileAccess.ReadWrite)

Dim w As New IO.BinaryWriter(fs)

NewValue = CurrentValue + 1
fs.Lock(0, 5)
w.Write(NewValue)
r = New IO.BinaryReader(fs)
CurrentValue = (r.ReadInt32())
fs.Unlock(0, 5)
r.Close()
w.Close()
fs.Close()
Return NewValue

End Function
'-----------------------------------------------------------------------------------------------------------



Jun 27 '08 #4
Paul Ilacqua wrote:
I'm a Database developer and like to roll my own "counters"
for my front end programs. Instead of using IDENTITY I use my counters.
Even so, shouldn't the counters themselves be /in/ the database, along
with the data they're "indexing" into?

If you already have a database then putting anything "out" in the file
system strikes me as a [big] step backwards.

I'd use a "last number" table, something like

Create Table Last_X
( X as Integer )

(with a single row in it)

then to get the "next" number to put into a "key" column, I'd use
something like ...

update Last_X set X = X + 1;
select X [into ?] from Last_X;

.... and (possibly) wrap that up into a stored procedure of some sort, to
streamline things even further.
It does work though...
And it's probably completely reliable, too - on a single-user system.
As soon as you have to think concurrent, multiple users, managing the
contention around those "index" files is going to be a massive headache
compared to the [relative] simplicity of doing it with a table (and
having the /database/ take care of it /for/ you).

HTH,
Phill W.
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Shane | last post by:
:) I am following a tutorial for php (hudzilla.org) absolutely loving it however I am having trouble with the counter example <?php // your content here... $filename = 'counter.txt'; // our...
16
by: Paul Rubin | last post by:
I'd like to have a function (or other callable object) that returns 0, 1, 2, etc. on repeated calls. That is: print f() # prints 0 print f() # prints 1 print f() # prints 2 # etc. ...
7
by: JellyON | last post by:
Hi. Is there a way to delay a call to a page counter (ie. call to a server script from an IMG tag) for the purpose to not lock the page loading awaiting counter be displayed. Maybe a...
0
by: Earl Anderson | last post by:
KB Article Q140908 provided the following function to create an Auto Incrementing Counter: Function Next_Custom_Counter () On Error GoTo Next_Custom_Counter_Err Dim MyDB As Database Dim...
7
by: mistral | last post by:
I use htaccess to protect directory and granting access to download file only for the authorized users. Just want implement simple PHP file download counter for single file. I need track the number...
0
by: Trevor L. | last post by:
I decide to put a custom Hit Counter on my page (below). Then I won't be reliant on the standard FrontPage one which uses webbots. It is called by <b>Hit Counter: </b><!--#include...
12
by: devospice | last post by:
Hi, I'm trying to create a download counter for individual files on a web site and I'm not sure how to do this. Right now I'm using Webalizer to just read the log files and see how many times...
5
by: jasonchan | last post by:
How would you set up a counter in javascript that goes from 5 to 0 This is what i have so far and it is not displaying in the div container "counter" for some reason... <!DOCTYPE html PUBLIC...
3
blackstormdragon
by: blackstormdragon | last post by:
Here were our instructions: "My mother always took a little red counter to the grocery store. The counter was used to keep tally of the amount of money she would have spent so far on that visit to...
2
by: gumbercules | last post by:
I am designing a site that is regulary updated with new podcasts. I would like to be able to have a counter next to each podcast showing how many hits/listens/plays/views each particular podcast has...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.