Dynamic sql connection string - c#

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();

Related

C# Connection String Write-Read .ini

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

Connection String Problem

I'm new to Visual Studio and MySQL. I'm creating a Login Page and connect it with MySQL. But whenever I add a connection string. I always got an error.
Can anybody tell me what's the problem in here?
Please.
This is my config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ODBCDriver" value="Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=timekeeping;uid=root;pwd=admin;Option=3;"/>
</appSettings>
<connectionStrings>
<add name="ConnectionString" connectionString="Database=timekeeping;uid=root;pwd=admin;Option=3;" />
</connectionStrings>
</configuration>
I'm not entirely familiar with MySQL connections, but your config file should look more like this
<configuration>
<appSettings>
<add key="ODBCDriver" value="Driver={MySQL ODBC 5.1 Driver};Server=localhost;"/>
</appSettings>
<connectionStrings>
<add name="ConnectionString" connectionString="Database=timekeeping;uid=root;pwd=admin;Option=3;" />
</connectionStrings>
</configuration>
You probably have a syntax error in your App.config file.
Check the InnerException for details.
This usually means there is a problem with your web.config file. Probably a tag out of place, not closed, or otherwise jacked.
http://msmvps.com/blogs/kevinmcneish/archive/2010/01/06/fixing-quot-configuration-system-failed-to-initalize-quot-exception.aspx
<configuration>
<appSettings>
<add key="ODBCDriver" value="Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=timekeeping;uid=root;pwd=admin;Option=3;"/>
</appSettings>
</configuration>
But whenever I add a connection
string. I always got an error.
Your web.config or app.config configuration is not legal in its current form - this could be because of a typo, i.e an unclosed tag, otherwise malformed XML or a section that is unknown or nested in a section where it doesn't belong.

Properties.Settings.Default fails on Release Mode

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>

A Visual Studio 2008 automated tests project can read a configuration file like app.config? (C# .NET)

I have an app.config file inside my TestProject, but when I try to read it using ConfigurationManager it reads from somewhere else and it's none of my app.config's. How to correct this?
Current config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Production" connectionString="Server=127.0.0.1,2345;Uid=user;Pwd=password;Initial Catalog=DATABASE_DATA"/>
</connectionStrings>
</configuration>
Current code:
ConfigurationManager.ConnectionStrings[0].ConnectionString
Expected output:
"Server=127.0.0.1,2345;Uid=user;Pwd=password;Initial Catalog=DATABASE_DATA"
Actual output:
"data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
Try Referencing it by name.
ConfigurationManager.ConnectionStrings["Production"].ConnectionString
The config files automatically integrate machine.config which has that SQLEXPRESS connection string by default.

Reading Hibernate Properties from Web.config

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.

Categories

Resources