Odds are, you’ve got more than one application or script accessing your database or SQL Server instance at any given time. You’re probably stacking them on a small number of servers in an attempt to cut down on licensing costs and resource usage. All those PowerShell scripts running on the central job server are running under a single service account, and you’ve got a lazy vendor who set up both the website and back-end application server to run under the same account, maybe even on a single app/web server.
Then the call comes in. “The database is slow.” Before firing up your monitoring suite, you take a quick pass with
sp_whoisactive and you’re greeted with a dozen sessions, all sporting a
.Net SqlClient Data Provider. Terrific! Which session is coming from which application and causing all the trouble? Not so easy to figure out.
Fortunately, the .NET
SqlClient (and other ODBC drivers as well) has a built-in solution. Your application’s
connection string has quite a few parameters available to provide configuration and information, and one that seems to get overlooked is
Application Name. This one does exactly what it says on the tin - it lets you specify a name that will be displayed to anyone looking for it in SQL Server, including
sp_whoisactive. Anyplace you have the ability to write a connection string, you can use this. It costs you nothing!
- If you’re using
System.Data.SqlClient.ConnectionStringBuilder, it’s just another item in the properties collection.
- If you’re constructing your connection string as a regular string, just add
Application Name=Andy's Awesome App;to the end of your current connection string.
Here’s the difference between using
Invoke-SQLCmd in PowerShell when seen from the SQL Server side:
While the .NET Framework lets you specify this fairly easily, as we move up in abstraction layers these options become buried or entirely unavailable. This is the case with the PowerShell
Invoke-SqlCmd cmdlet. One of the many benefits of using
Invoke-SqlCmd2 is that it does construct its own connection string (if you don’t pass in a
SqlConnection object) but it doesn’t expose
Application Name as an available parameter for it.
In an upcoming post, I’ll show how I added
Application Name support to