none
%ALLUSERSPROFILE% in ConnectionString RRS feed

  • Question

  • I have a database that is located in Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) +"\\myapp" folder.

    After I add a data connection and create a DataSet from the database the visual studio creates "myapp.Properties.Settings.Default.mydbConnectionString" and initializes it to "data source=C:\ProgramData\myapp\mydb.s3db" (app.config). The trouble is that in windows xp the folder should be something like "data source=C:\Documents and Settings \All Users \ Application Data\ myapp\mydb.s3db ". I tried to solve the conflict by using "data source=%ALLUSERSPROFILE% \ myapp\mydb.s3db " but it did not work.

    What is the correct solution to my problem?

    Help much appreciated.

    Robert
    • Edited by iinlane Wednesday, March 17, 2010 7:08 AM removed extra spaces from paths
    Tuesday, March 16, 2010 12:52 PM

Answers

  • Hello Robert,

     

    I don’t think we can use the system directory variables in the connection string.   Could you modify the source codes of the typed dataset?   If so, you can consider calling Environment.GetFolderPath() method to retrieve the system folder you want in the typed dataset InitConnection method and combine the connection string retrieved by the application settings: myapp.Properties.Settings.Default.mydbConnectionString.   In the InitConnection method is inside each TableAdapter class.  The connection string in the .config file can be: “data source=mydb.s3db…”.   We can replace “data source=” by “data source=” + Environment.GetFolderPath().  

     

    Another approach is like BinaryCoder suggested that we may also use the SqlConnectionStringBuilder to build the connection string during runtime time.  

     

    If you have any questions, please feel free to let me know. 

     

    Have a nice weekend!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by iinlane Monday, March 22, 2010 2:36 PM
    Friday, March 19, 2010 4:15 PM
    Moderator

All replies

  • You need to use some kind of quoting due to the spaces in the file name.  Check the documentation regarding connection strings for your database system.

    If your database driver supplies a "connection string builder" object, consider whether it will figure out the quoting for you.

    For example, with Microsoft SQL Server, an SqlConnectionStringBuilder is provided.
                System.Data.SqlClient.SqlConnectionStringBuilder builder =
                    new System.Data.SqlClient.SqlConnectionStringBuilder();
                builder.DataSource = "your data source here";
                System.Diagnostics.Debug.WriteLine(builder.ConnectionString);
    Wednesday, March 17, 2010 2:28 AM
  • Thanks for answer but there's no spaces in the paths. <del>I must have added them by accident while typing the message.</del> The spaces were added by the text editor, there's no spaces in actual paths.


    Robert
    Wednesday, March 17, 2010 7:07 AM
  • Hello Robert,

     

    I don’t think we can use the system directory variables in the connection string.   Could you modify the source codes of the typed dataset?   If so, you can consider calling Environment.GetFolderPath() method to retrieve the system folder you want in the typed dataset InitConnection method and combine the connection string retrieved by the application settings: myapp.Properties.Settings.Default.mydbConnectionString.   In the InitConnection method is inside each TableAdapter class.  The connection string in the .config file can be: “data source=mydb.s3db…”.   We can replace “data source=” by “data source=” + Environment.GetFolderPath().  

     

    Another approach is like BinaryCoder suggested that we may also use the SqlConnectionStringBuilder to build the connection string during runtime time.  

     

    If you have any questions, please feel free to let me know. 

     

    Have a nice weekend!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by iinlane Monday, March 22, 2010 2:36 PM
    Friday, March 19, 2010 4:15 PM
    Moderator