none
DataDirectory in App.Config RRS feed

  • Question

  • Hello, I have the following app.config that seems to work just fine when connecting to my database:

     

    <?xml version="1.0" encoding="utf-8" ?><configuration>  <configSections>    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >      <section name="MyProj.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />    </sectionGroup>  </configSections>  <connectionStrings>    <add name="Xpo.Core.Properties.Settings.ConnectionString" connectionString="Data Source=.\SqlExpress;Initial Catalog= C:\MyProj\DB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;" />  </connectionStrings>  <applicationSettings>    <XCater.Properties.Settings>      <setting name="SurveySvc_MainService" serializeAs="String">        <value>http://localhost:50047/MainService.asmx</value>      </setting>    </MyProj.Properties.Settings>  </applicationSettings></configuration>

    Now if I change the path of the database to

    connectionString="Data Source=.\SqlExpress;Initial Catalog= |DataDirectory|\DB.mdf

    I get the following execption:

    "CREATE FILE encountered operating system error 123(The filename, directory name, or volume label syntax is incorrect.) while attempting to open or create the physical file 'C:\Users\Admin\AppData\Local\Microsoft\Microsoft SQL Server Data\SQLEXPRESS\|DataDirectory|_DB.mdf.mdf'.
    CREATE DATABASE failed. Some file names listed could not be created. Check related errors."

     I also tried adding the following as the first line in my form_load

    "AppDomain.CurrentDomain.SetData("DataDirectory", @"C:\MyProj\");"

    I am not quite sure what else I can do, it almost seems like it's not recognizing the DataDirectory as an expandable variable.  I have seen posts that talk about VS creating this for you, but I'm not sure how to hand edit this and get it to work.  Does anyone have any suggestions?

    Thanks for any help!

    Wednesday, February 1, 2012 2:51 AM

Answers

  • Try with

    "Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;"

    Why is the Database parameter needed? If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection.


    Lingaraj Mishra
    Wednesday, February 1, 2012 11:12 AM

All replies

  • Try with

    "Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;"

    Why is the Database parameter needed? If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection.


    Lingaraj Mishra
    Wednesday, February 1, 2012 11:12 AM
  • Thanks for the response Lingaraj.

    I may have some misunderstandings about how this all fits together, however you are correct.  I do have the database already attached on my dev machine.  But when I go to install the applicaiton on a non-development box, I will need a way to reference the .mdf file and that machine will not have an sql server connection to the db.

    I will try your modifications and see if I have any luck.

    Wednesday, February 1, 2012 10:45 PM
  • Hi bushman_IL16,

    Any updates for your question now?

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 6, 2012 2:57 AM
    Moderator