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.

Sign up to request clarification or add additional context in comments.

4 Comments

is .\SQLExpress is default instance?
i use the sql attached with visual studio 2012.how can i change my connection string with \SQLEXpress
@Freelancer:Yes for more information view this link: support.microsoft.com/kb/958778
@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.
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 Comments

AttachDbFilename attribute is not allowed
missing quotes. i cant understand where to put quotes.
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.
its ok. can help me in this problem
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.
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

Comments

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 Comments

right click on your project go to add and then add new item and then add application configuration file
is this web configuration file
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.
No it is a application Configuration file. like Web Config in other application
it shows an error TypeInitilaizationException was unhandled by error
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.