Quick SQL hint: Use Application Name in connection strings
Tags: programming, database, mssql, picture
Published: 2022-05-22 22:23:25

---
title: Quick SQL hint: Use Application Name in connection strings
published: true
date: 2022-05-22 22:22:37 UTC
tags: programming,database,mssql,picture
canonical_url: https://siderite.dev/blog/quick-sql-hint-use-application-name-in-connection-/
---

  Original URL: https://siderite.dev/blog/quick-sql-hint-use-application-name-in-connection-/

  When we connect to SQL we usually copy/paste some connection string and change the values we need and rarely consider what we could change in it. That is mostly because of the arcane looking syntax and the rarely read [documentation for it](https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring "The connection string that includes the source database name, and other parameters needed to establish the initial connection."). You want to connect to a database, give it the server, instance and credentials and be done with it. However, there are some parameters that, when set, can save us a lot of grief later on.

  [Application Name](https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring "The name of the application, or '.NET SQLClient Data Provider' if no application name is provided. An application name can be 128 characters or less.") is something that identifies the code executing SQL commands to SQL Server, which can then be seen in profilers and DMVs or used in SQL queries. It has a maximum length of 128 characters. Let's consider the often met situation when your application is large enough to be segregated into different domains, each having their own data access layer, business rules and user interface. In this case, each domain can have its own connection string and it makes sense to specify a different Application Name for each. Later on, one can use SQL Profiler, for example, and filter on the specific area of interest.

![Sql Server Profiler screenshot showing column ApplicationName](https://siderite.dev/Posts/files/ApplicationName_637888549575756402.jpg) 

  The application name can also be seen in some queries to SQL Server's [Dynamic Management Views](https://docs.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/system-dynamic-management-views "Dynamic management views and functions return server state information that can be used to monitor the health of a server instance, diagnose problems, and tune performance.") (quite normal, considering DMVs are used by SQL Profiler) like [sys.dm\_exec\_sessions](https://docs.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-exec-sessions-transact-sql "Returns one row per authenticated session on SQL Server. sys.dm\_exec\_sessions is a server-scope view that shows information about all active user connections and internal tasks. This information includes client version, client program name, client login time, login user, current session setting, and more. Use sys.dm\_exec\_sessions to first view the current system load and to identify a session of interest, and then learn more information about that session by using other dynamic management views or dynamic management functions."). Inside your own queries you can also get the value of the application name by simply calling [APP\_NAME()](https://docs.microsoft.com/en-us/sql/t-sql/functions/app-name-transact-sql "This function returns the application name for the current session, if the application sets that name value."). For example, running _SELECT APP\_NAME();_ in SQL Management Studio returns a nice "Microsoft SQL Server Management Studio - Query" value. In [SQL Server Profiler](https://docs.microsoft.com/en-us/sql/tools/sql-server-profiler/sql-server-profiler "SQL Server Profiler is an interface to create and manage traces and analyze and replay trace results. Events are saved in a trace file that can later be analyzed or used to replay a specific series of steps when diagnosing a problem.") the column is _ApplicationName_ while in DMVs like sys.dm\_exec\_sessions the column is _program\_name_.

  Example connection string: Server=localhost;Database=MyDatabase;User Id=Siderite;Password=P4ssword; Application Name=Greatest App Ever

  Hope it helps!