Using connection strings twice in web.config file; another for Nlog config

asp.net, nlog

Solution

First add the add providerName attribute to connection string. Then use connectionStringName instead of connectionString and refer to the connection string from the settings.

<connectionStrings>
  <add name="SQL_ConnStr" 
       providerName="System.Data.SqlClient"
       connectionString="Initial Catalog=ConfigDB;Provider=SQLOLEDB; Data Source=mysqlserver; User ID=sa; Password=sa; Persist Security Info=True;"/>
</connectionStrings>
...
<nlog>
  <targets>
    <target name="database" 
            type="Database" 
            dbProvider="sqlserver"
            connectionStringName="SQL_ConnStr" 
            commandText="INSERT INTO ...">
    </target>
  </targets>
  <rules>
    <logger name="*" 
            minlevel="Debug" 
            writeTo="database"/>
  </rules>
</nlog>

Problem

I am using nlog in my project. My web.config looks like this: ``` <connectionStrings> <add name="SQL_ConnStr" connectionString="Initial Catalog=ConfigDB;Provider=SQLOLEDB; Data Source=mysqlserver; User ID=sa; Password=sa; Persist Security Info=True;"/> </connectionStrings> ... <nlog> <targets> <target name="database" type="Database" dbProvider="sqlserver" **connectstring="Initial Catalog=ConfigDB;Provider=SQLOLEDB; Data Source=mysqlserver; User ID=sa; Password=sa"** commandText="INSERT INTO ..."> </target> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="database"/> </rules> </nlog> ``` Two identical connection strings! My question is how to keep only one connection string?

Original source

Related problems