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.

Keren Caelen
  • 1,466
  • 3
  • 17
  • 38
user1872014
  • 17
  • 2
  • 8

4 Answers4

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.

garvit gupta
  • 301
  • 3
  • 18
  • is .\SQLExpress is default instance? – Freelancer Jun 06 '13 at 11:09
  • i use the sql attached with visual studio 2012.how can i change my connection string with \SQLEXpress – user1872014 Jun 06 '13 at 11:13
  • @Freelancer:Yes for more information view this link: http://support.microsoft.com/kb/958778 – garvit gupta Jun 06 '13 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 06 '13 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;
Sean Airey
  • 6,352
  • 1
  • 20
  • 38
  • AttachDbFilename attribute is not allowed – user1872014 Jun 06 '13 at 11:33
  • missing quotes. i cant understand where to put quotes. – user1872014 Jun 06 '13 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 06 '13 at 11:35
  • its ok. can help me in this problem – user1872014 Jun 06 '13 at 11:39
  • The `connectionString` attribute is just a string like you would find in any XML file. I find http://www.connectionstrings.com to be a very useful resource when working with connection strings. This page (http://www.connectionstrings.com/articles/show/all-sql-server-connection-string-keywords) is especially helpful. – Sean Airey Jun 06 '13 at 11:42
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();
Taj
  • 1,718
  • 1
  • 12
  • 16
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

Community
  • 1
  • 1
Rohan Büchner
  • 5,333
  • 4
  • 62
  • 106