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>
Related
When i change connection string after publish project in visual studio it is giving me an error
Here is my app.config file
<connectionStrings>
<add name="DataContext" connectionString="metadata=res://*
/Model.DataContext.csdl|res://*/Model.DataContext.ssdl|res:
//*/Model.DataContext.msl;provider=System.Data.SqlClient;provider connection string="data source=CODE-SERVER\SQLSILENT;initial catalog=pos-standard;persist security info=True;user id=sa;password=abc123**;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
and in new app.config file i want to replace this connection string it my local database connection string.
<add name="DataContext" connectionString="metadata=res://*/Model.DataContext.csdl|res://*/Model.DataContext.ssdl|res://*/Model.DataContext.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=pos-standard;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
I also attach here a error window
Are you sure that your database authorises Integrated security ?
Can you connect using SQL server management studio with integrated security ?
If yes, try to not replace all the connection string but only remove username and password ant put integrated security = true, and change server name.
Finally, check the database server name may be .\Something.
You need to re-sign the application and deployment manifests after you have edited the config file. Please refer to the following links for more information about this.
http://msdn.microsoft.com/en-us/library/dd465299.aspx
Resign Clickonce manifest using mage.exe
I want to write and read my connection string in the Baglanti.cs class, but I don't know anything about this. I trying this first time. I search on Google, but I can't find any clear information.
The .NET way of doing this is using app.config file instead of .ini file
Create a new Application Configuration File with the following content
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyConnectionString" connectionString="YourConnectionString" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Retrieve the connectionString inside your application using
var connection = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
Check out MSDN: Connection Strings and Configuration Files
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
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();
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.