locked
Data Base Connection String RRS feed

  • Question

  • Hi All !

    Right now, I've created a ConnectionString to my Excel Database and it looks something like this "DataSource=C:\Data.xlsx".

    However, something came to my mind. Wouldn't I expect to see a problem during Deployment as the server where I store the file may not be in C: drive.

    Therefore, I would like to ask how can I change the connection string such that it reacts relative to where it application is stored?

    ICY.

    Friday, September 23, 2011 4:01 AM

Answers

  • Use an app.config file that looks like this:

    <?xml version="1.0"?>
    <configuration>
      <appSettings>
        <!-- any app setting key/value pairs you need -->
      </appSettings>
      <connectionStrings>
        <add name="ExcelConnection" connectionString="YourExcelConnection"/>
      </connectionStrings>
    </configuration>
    
    

    Then, in your code, you'd retrieve that connection string like this:

    string MyConnectionString = ConfigurationManager.ConnectionStrings["ExcelConnection"].ConnectionString;
    

    You may need to add a using System.Configuration to your class and you may also need to add a reference to System.Configuration to your project references.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by Helen Zhou Thursday, September 29, 2011 2:22 AM
    Saturday, September 24, 2011 4:07 PM
  • Hi Icychemist,

    Addition to Bonnie's, you can get more information about getting connection string from app.config from Grabbing connectionString from App.config, or use an app.config looks like this

    Code:

    <configuration>
    	<appSettings>
    		<add key="ConnectionString" value="connection string here"/>
    	</appSettings>
    </configuration>

    And then you can get connection string by nabeelisnabeel's way.

    Code:
    string str = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];

    And this article Using Application Configuration Files in .NET   covers using application configuration files in Microsoft .NET. It briefly outlines the concept of application configuration files and touches on the native support the Microsoft .NET Framework provides. In particular, it discusses using the System.Configuration namespace among others within the Framework.


    Helen Zhou [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.

    • Marked as answer by Helen Zhou Thursday, September 29, 2011 2:30 AM
    Thursday, September 29, 2011 2:30 AM

All replies

  • //Assumes a function exists to retrieve the location of the file from
    //wherever your program or installer put it.
    string myDataLocation = GetLocationFromRegistryOrConfigFile();
    string myCS = String.Format("<however the connection string looks like before DataSource>DataSource={0}<the rest of the conn. string>", myDataLocation);
    


    MCP
    Friday, September 23, 2011 5:38 AM
  • Use an app.config file that looks like this:

    <?xml version="1.0"?>
    <configuration>
      <appSettings>
        <!-- any app setting key/value pairs you need -->
      </appSettings>
      <connectionStrings>
        <add name="ExcelConnection" connectionString="YourExcelConnection"/>
      </connectionStrings>
    </configuration>
    
    

    Then, in your code, you'd retrieve that connection string like this:

    string MyConnectionString = ConfigurationManager.ConnectionStrings["ExcelConnection"].ConnectionString;
    

    You may need to add a using System.Configuration to your class and you may also need to add a reference to System.Configuration to your project references.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by Helen Zhou Thursday, September 29, 2011 2:22 AM
    Saturday, September 24, 2011 4:07 PM
  • Hi Icychemist,

    Addition to Bonnie's, you can get more information about getting connection string from app.config from Grabbing connectionString from App.config, or use an app.config looks like this

    Code:

    <configuration>
    	<appSettings>
    		<add key="ConnectionString" value="connection string here"/>
    	</appSettings>
    </configuration>

    And then you can get connection string by nabeelisnabeel's way.

    Code:
    string str = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];

    And this article Using Application Configuration Files in .NET   covers using application configuration files in Microsoft .NET. It briefly outlines the concept of application configuration files and touches on the native support the Microsoft .NET Framework provides. In particular, it discusses using the System.Configuration namespace among others within the Framework.


    Helen Zhou [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.

    • Marked as answer by Helen Zhou Thursday, September 29, 2011 2:30 AM
    Thursday, September 29, 2011 2:30 AM