Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Connection string loaded from config file

Connection string loaded from config file

Scheduled Pinned Locked Moved Database
databasesysadmindebuggingxmlquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nigel Mackay
    wrote on last edited by
    #1

    I have:

    configMap =
    new ExeConfigurationFileMap { ExeConfigFilename = "ToolHistoryServer.exe.config" };
    myCfg =
    ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
    dataSource = myCfg.AppSettings.Settings["DataSource"].Value;
    cnStr = "Data Source='" + dataSource + "';" +
    "Trusted_Connection=true;" +
    "Database=ToolHistory";

    Config file:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <appSettings>
    <add key="DataSource" value="NIGEL-PC\\SQLEXPRESS" />
    </appSettings>
    <system.runtime.remoting>
    <application>
    <channels>
    <channel ref="tcp" port="50050" />
    </channels>
    </application>
    </system.runtime.remoting>
    </configuration>

    I have tried cnStr with and without the single quotes, but neither work. Something is wrong with my syntax. It compiles and runs, but data access fails. Can't debug because it is on the server side.

    L 1 Reply Last reply
    0
    • N Nigel Mackay

      I have:

      configMap =
      new ExeConfigurationFileMap { ExeConfigFilename = "ToolHistoryServer.exe.config" };
      myCfg =
      ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
      dataSource = myCfg.AppSettings.Settings["DataSource"].Value;
      cnStr = "Data Source='" + dataSource + "';" +
      "Trusted_Connection=true;" +
      "Database=ToolHistory";

      Config file:

      <?xml version="1.0" encoding="utf-8"?>
      <configuration>
      <appSettings>
      <add key="DataSource" value="NIGEL-PC\\SQLEXPRESS" />
      </appSettings>
      <system.runtime.remoting>
      <application>
      <channels>
      <channel ref="tcp" port="50050" />
      </channels>
      </application>
      </system.runtime.remoting>
      </configuration>

      I have tried cnStr with and without the single quotes, but neither work. Something is wrong with my syntax. It compiles and runs, but data access fails. Can't debug because it is on the server side.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Doesn't that give a connectionstring like below?

      Data Source='NIGEL-PC\SQLEXPRESS';
      Trusted_Connection = true;
      Database = ToolHistory;

      What happens when you remove the quotes around the name of the datasource?

      I are Troll :suss:

      N 1 Reply Last reply
      0
      • L Lost User

        Doesn't that give a connectionstring like below?

        Data Source='NIGEL-PC\SQLEXPRESS';
        Trusted_Connection = true;
        Database = ToolHistory;

        What happens when you remove the quotes around the name of the datasource?

        I are Troll :suss:

        N Offline
        N Offline
        Nigel Mackay
        wrote on last edited by
        #3

        I would have thought so. I have just written test code to return cnStr, and I find that dataSource is NIGELPC\\\\SQLEXPRESS! Wrote a snippet to remove the extra \\ and now it works.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups