Here's a version of our classic hit counter (database) script implemented in ASP.NET.
I've implemented it almost exactly the same way I implemented the ASP.NET text file version.
In fact, if you look at them, counter.aspx and counter_db.aspx are almost exactly the same.
The syntax for using the control doesn't change between the text file and database versions.
All the changes are internal to the control.
Speaking of those changes... the first and foremost is that since this counter stores
its counts in a database table, you'll need a copy of the table. You can download an
MS Access file that illustates the layout from here: counter_db.mdb, but
once you look at the code, you'll quickly realize that it's set up to use our SQL Server
and not Access. Getting it to use Access should not be difficult, but it's left as an exercise for the
reader. (If you want to do this, you may find our plain old
ASP.NET Database Sample useful.)
For those of you using SQL or any other DB, here's a quick run down of the fields and their setup
via a sample table creation script.
Even if you don't know how to use it, it should explain enough about the layout for you to recreate it.
After all, it's really pretty simple.
CREATE TABLE dbo.hit_count
(
page_name varchar(255) NOT NULL,
hit_count int NULL
) ON [PRIMARY]
GO
ALTER TABLE dbo.hit_count ADD CONSTRAINT
PK_hit_count PRIMARY KEY NONCLUSTERED
(
page_name
) ON [PRIMARY]
GO
Oh and feel free to change the table or field names. I'm using the same table I originally set up
for the classic asp version of the script. If I were doing it today I might name
things differently, but I really didn't see any reason to change things at this point.
The zip file below includes the ascx control, a sample aspx file that
uses the control, and a copy of the SQL script listed above.
You can get the images in a zip file from here(5.3 KB).