In keeping with the recommendation here we separated our connection string into an xml file. Our web.config:
//old - works
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=.\SQLExpress;Initial Catalog=xxx;Integrated Security=True;"
providerName="System.Data.SqlClient" />
<connectionStrings>
//new - fails
<connectionStrings configSource="connections.config" />
content of connections.config:
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLExpress;Initial Catalog=xxx;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Both files are located in the project root folder. EntityFramework works fine with both solutions. However when I try to obtain the connection string for some tests it fails to retrieve.This is the code that we use to extract the connection string:
string ConnectionString =
ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
Reading the connection string direct from web.config works, when we introduce the ConfigSource it fails. What do we need to make it work
You need to set the Copy to Output Directory property of your configuration file to either Copy always or Copy if newer.
If you don't do this, your ConnectionStrings.config file will not be copied to the output debug/release folder and so will not be found.
Also note that the external file should have the same name as of the section name. Like it should be connectionStrings.config, not connections.config
Related
What path should be given to the base file in the project.
I tried it but it does not work:
` <connectionStrings>
<add name="connection" connectionString="Data Source=(LocalDb)\v11.0; AttachDbFilename=..\Database.mdf;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>`
You can use |DataDirectory| to get the base of your project.
AttachDbFilename=|DataDirectory|\Database.mdf
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.
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.
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;
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
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();
i have developed a project. i am uploading this project in a server. i could not connect this with the database.
what will be thw path in web.config file. help me please.this is my connection string in web.config file.
<connectionStrings>
<add name="bcharyaConnectionString" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"/>
</connectionStrings>
should i use database path there?
D:\microsoft sql server\data\bcharya.mdf
this path or
D:\microsoft sql server\data\bcharya_log.LDF
MDF Location
Put your .MDF file in App_data folder.
Connection string
<connectionStrings>
<add name="bcharyaConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bcharya.mdf;
Integrated Security=True;
User Id=myUsername;
Password=myPassword;
User Instance=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Add a backslash "\" just after |DataDirectory| in your connection string:
AttachDBFilename=|DataDirectory|\bcharya.mdf;
<connectionStrings>
<add name="bcharyaConnectionString" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\bcharya.mdf;User Instance=true"/>
</connectionStrings>
|DataDirectory| ( Enclosed in pipe symbols ) is a substitution string that indicates the path to the database.
To set the DataDirectory property, call the AppDomain.SetData method. If you do not set the DataDirectory property, the following default rules will be applied to access the database folder:
1: For applications that are put in a folder on the user's computer, the database folder uses the application folder.
2: For web application the database folder uses App_Data folder.
You can set the path in Application_Start method in your Global.ascx.cs
AppDomain.CurrentDomain.SetData("DataDirectory", #"D:\microsoft sql server\data");
DO NOT Forget to change
AttachDBFilename=|DataDirectory|\aspnetdb.mdf;
to
AttachDBFilename=|DataDirectory|\bcharya.mdf;
If the database is already attached to sql server change the connection to:
<connectionStrings>
<add name="bcharyaConnectionString" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog = bcharya;User Instance=true"/>
</connectionStrings>
This way you don't need to set the |DataDirectory| path.
In debug mode I have been using:
ConnectionString = Properties.Settings.Default.SomeConnectionString
to retrieve my connection string. However, this same code does not give me the connection string when running in release mode.
Note: SomeConnectionString is a connection string in a local Settings.settings file.
How can I use the same code above regardless of debug/release mode?
Thanks!
Use the settings designer to set a setting of type Connection String, which will be placed in the app.config file for your csproj. Make sure you specify the scope (Application or User) appropriately.
Instead of using a Properties file to store the connection string, you should store the connection string in app.config if you can't get access to the connection string in the web.config.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="SomeConnectionString"
connectionString="Data Source=(local);Initial Catalog=Database;
Persist Security Info=True;Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
<connectionStrings>
</configuration>
The C# project I'm working on uses nHibernate and the connection string is in the web.config as a property of a Hibernate element. I need to read the connection string in the installer to get a connection manually without using Hibernate. I know I can use configManager.connectionStrings, but as the connection string is already defined in the Hibernate portion of web.config I don't want to copy it again into the connectionStrings element. So how can I access this?
You could put the connection string in the <connectionStrings /> section of the web.config and then have NHibernate get it from there. In the NHibernate settings, remove the <connection.connection_string> property and replace it with <connection.connection_string_name> supplying the name from the <connectionStrings> section. See here for details.
<hibernate>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="hibernate.connection.connection_string" value="${local}"/>
</hibernate>
<connectionStrings>
<add name="local" connectionString="server=(local);database=db;Uid=username;Pwd=password;"/>
</connectionStrings>
This makes it available in your ConfigurationManager, but only referenced once.