Archive for the ‘Programming’ Category

Find most expensive queries in Microsoft SQL server

A colleague of mine found this little gem of a query (sorry, don’t know the original author). It finds the 20 most expensive queries executed on a SQL server instance, sorted by execution time. CPU load is also returned by the query. Here it is: SELECT TOP 20 qs.sql_handle, qs.execution_count, qs.total_worker_time AS Total_CPU, total_CPU_inSeconds = –Converted from microseconds qs.total_worker_time/1000000, average_CPU_inSeconds = –Converted from microseconds (qs.total_worker_time/1000000)/ qs.execution_count, [...]

More »

Some nice programming quotes I came across…

Walking on water and developing software from a specification are easy if both are frozen. – Edward V. Berard Hofstadter’s Law: It always takes longer than you expect, even when you take into account Hofstadter’s Law. Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code [...]

More »

10 Free Tools to Load/Stress Test Your Web Applications

Follow the link… http://www.devcurry.com/2010/07/10-free-tools-to-loadstress-test-your.html

More »

Script failed for Trigger ‘Trigger-name’. (Microsoft.SqlServer.Smo))

It’s a documented bug in SQL server that causes above error message. The error is triggered by nested comment blocks, like this example: /* some comments /* nested comments*/ some more comments */ All very well, it’s a documented bug so it shouldn’t be a problem. Microsoft’s solution is plain and effective: don’t use nested comment [...]

More »

How to display table sizes in SQL server

There are a few ways to retrieve the table sizes (both row count and size in bytes) in SQL server. The simplest is the sp_spaceused stored procedure which takes a table name as argument: EXEC sp_spaceused ‘table_name’ This is all fine, but what if you want to display info about a dozen or more tables? [...]

More »

Make a .NET web service accept GET requests

The usual way to call web service is by using POST requests. But sometimes GET requests are required. To make .NET web services accept their parameters from GET requests, add a ScriptMethod attribute to your web service routine. First add some libraries: C# Using System.Web.Services; Using System.Web.Services.Protocols; Using System.Web.Script.Services; Visual Basic Imports System.Web.Services Imports System.Web.Services.Protocols [...]

More »

How to rename a column in a MS-SQL database?

Easy peasy, use the sp_rename stored procedure: EXEC sp_rename ‘TABLENAME.OLD_COLUMNNAME’, ‘NEW_COLUMNAME’, ‘COLUMN’;

More »

CakePHP: catching errors in Model->query()

In my recent CakePHP application I use some custom SQL queries that I fire at the database using the $model->query($sql) syntax. Works great, until you have an error in your $sql. I tried the obvious error handling with try … catch: try {   $this->Model->query(’INSERT INTO model WHERE id=invalid’); } catch (exception $ex) {   [...]

More »

How to: Check if a Microsoft SQL-server database exists?

Here’s an easy, single line statement to check if a database exists in the current SQL-server instance: IF db_id(’db_name’) IS NOT NULL

More »

Compute hash of a file

Computing the hash value of a file is quite strait forward. I use the routine below. It opens the file, specifies the hash-algorithm to use and returns the hash of the file. If you rather use MD5 hashes, just change hashAlgorithm into MD5CryptoServiceProvider. Public Function ComputeFileHash(ByVal fileName As String) As String   Dim hashAlgorithm As [...]

More »

How to: Get the name of the running SQL-server instance

Here’s a script to retrieve the name of the currently running SQL Server instance: SELECT @@SERVERNAME AS ‘ServerName’ Resultset: ServerName ————————— DEV-SQL (1 row(s) affected)

More »

SQL-server: Get current database name

It can sometimes be handy to know in which database you are working. To get the name of the current database you can use a simple script: SELECT DB_NAME() AS DataBaseName Resultset: DatabaseName ———————— NorthWind (1 row(s) affected)

More »

Am I running as a 32 or 64-bit application?

Here’s an easy way to determine if a .NET application is running as either 32 or 64-bit  application. VB Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)   If IntPtr.Size = 8 Then     Response.Write("64-bit")   ElseIf IntPtr.Size = 4 Then     Response.Write("32-bit")   End If End Sub C# protected void [...]

More »

Purge OpenX statistics tables

OpenX is a great tool for displaying ads on your website. But there is a small issue I’ve been wanting to address: statistics data. OpenX gathers a lot of data about clicks and impressions of your banners. While this can be very helpful, it also puts a strain on the database. And as I use [...]

More »

Error message 401.2.: Unauthorized: Logon failed due to server configuration. (IIS7)

After installing Windows 7 my ASP.NET application displayed an Access is deniederror. The complete error message was: Error message 401.2.: Unauthorized: Logon failed due to server configuration.  Verify that you have permission to view this directory or page based on the credentials you supplied and the authentication methods enabled on the Web server.  Contact the Web [...]

More »