1

I am using my SQL connection in my mvc4 application as follows:

 public static string ConnectionString=@"Data Source=LocalDB)\v11.0;AttachDbFilename=C:\Users\..\Documents\Visual Studio 2012\Projects\..\..\App_Data\RoDB.mdf;Integrated Security=True";

I want to rewrite it as dynamically.

When I change the system, I don't want to change the connection string.

2

4 Answers 4

2

If you use ".\SQLExpress" as server name it will connect to the local instance. In that case you don't need to change your connection string on different machines.

4
  • is .\SQLExpress is default instance?
    – Freelancer
    Jun 6, 2013 at 11:09
  • i use the sql attached with visual studio 2012.how can i change my connection string with \SQLEXpress Jun 6, 2013 at 11:13
  • @Freelancer:Yes for more information view this link: support.microsoft.com/kb/958778 Jun 6, 2013 at 11:16
  • 1
    @Freelancer Yes, it's the default instance name when installing SQL Server Express. For other editions (the paid ones) this is usually .\MSSQL. You can also change this during installation so I wouldn't rely on it being correct all the time. This is why it is better to move configuration properties such as connection strings out to the web.config file. After all, that is its purpose.
    – Sean Airey
    Jun 6, 2013 at 11:39
1

You can put connection strings in your web.config file, this means it is out of application code and doesn't require a re-build to change.

<configuration>
    <!-- Other config settings -->
    <connectionStrings>
        <add name="localDBConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\..\Documents\Visual Studio 2012\Projects\..\..\App_Data\RoDB.mdf;Integrated Security=True" />
    </connectionStrings>
</configuration>

Then to use this in your application you can put the following in compiled code:

string myConnectionString = ConfigurationManager.ConnectionStrings["localDBConnection"].ConnectionString;
5
  • AttachDbFilename attribute is not allowed Jun 6, 2013 at 11:33
  • missing quotes. i cant understand where to put quotes. Jun 6, 2013 at 11:33
  • Sorry I noticed the error in my code, that'll teach me to copy and paste ^_^ I've edited it now, that should be correct.
    – Sean Airey
    Jun 6, 2013 at 11:35
  • its ok. can help me in this problem Jun 6, 2013 at 11:39
  • The connectionString attribute is just a string like you would find in any XML file. I find connectionstrings.com to be a very useful resource when working with connection strings. This page (connectionstrings.com/articles/show/…) is especially helpful.
    – Sean Airey
    Jun 6, 2013 at 11:42
0

Whats wrong with using a web config? its pretty much standard practice I'd assume? Also read up on using the relative path. EG. Relative to application location

0

add a app configuration file in you application and add setting inside it

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="ConnectionString" value="Data Source=LocalDB)\v11.0;AttachDbFilename=C:\Users\..\Documents\Visual Studio 2012\Projects\..\..\App_Data\RoDB.mdf;Integrated Security=True"/>
  </appSettings>
</configuration>

in your code you can write

string ConnectionString= System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();
7
  • right click on your project go to add and then add new item and then add application configuration file
    – Taj
    Jun 6, 2013 at 11:32
  • is this web configuration file Jun 6, 2013 at 11:36
  • This will work in the web.config file as well, which is really just a specifically named application configuration file anyway but that's where your web server will look for configuration elements.
    – Sean Airey
    Jun 6, 2013 at 11:36
  • No it is a application Configuration file. like Web Config in other application
    – Taj
    Jun 6, 2013 at 11:42
  • it shows an error TypeInitilaizationException was unhandled by error Jun 6, 2013 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.