C# Connection String Write-Read .ini - c#

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

Related

ConfigurationManager throwing null exception when trying to read connection string in .NET 4.0

I am working on .NET Framework 4.0 Class Library project along with ADO.NET. I have connectionString inside the App.config which I am trying to read using ConfigurationManager but getting null reference exception.
Error
app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name ="dbConnection" connectionString="Data Source=xyz;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Reading ConnectionString
using System.Configuration;
var x22 = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
in app config
<connectionStrings>
<add name="[name]" connectionString="[YOUR CONNECTION]" />
</connectionStrings>
call your connectionString
ConfigurationManager.ConnectionStrings["name"].ConnectionString
In your solution explorer and under reference node, check if assembly System.Configuration has been added related to you needed Configuration Class and there is not any error regarding to your references.
But, your code is correct and I suggest to you try to avoid this error by checking null situation in return value of that command. Maybe config file is not exist in your default path. check config file to be exist then try to read it's data.

How to move connectionString from App.config to code in C#?

I want to remove connectionString from App.config and add it to code. I'm using WinForms(C#) and .Net Framework 4.8. I have textBoxes and a datagridview on my form. I'm using Microsoft Access Database (.mdb). I want to do this because database's password is visible in App.config. Is there any way to do that
Here is my app.config XML code :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="Locker.Properties.Settings.pw_dbConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\files\pw_db.mdb;Persist Security Info=True;Jet OLEDB:Database Password=XYZ"
providerName="System.Data.OleDb" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
</configuration>
I would suggest you not to define the connection string in code, better you can encrypt it in the app.config file.
Please follow the following steps:
Rename app.config to web.config
Open command prompt and type:
%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef
"connectionStrings" path of the web.config>
Rename web.config back to app.config
If you open the file in notepad, you will see the encrypted value and you can continue using it as you were using before.

Dynamic sql connection string

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

Get connection string from app.config

I'm trying to make this simple call:
DataContext dc = new DataContext(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString)
And here's my app.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="MyDB" connectionString="Server=STEVEN-PC;Database=MyDB;Trusted_Connection=yes;" />
</connectionStrings>
</configuration>
But I'm getting an error: Object reference not set to an instance of an object.
It can't find the connection string. What am I doing wrong?
A common mistake is trying to read a connection string from an app.config from a referenced project instead of from the executable project (the web site or .exe project). You may need to copy the config settings containing the connection string to your main config file.
Double check the existence and contents of your configuration file in the build directory. That code should work fine if the config file is in place.
You could also pull put the connection string into a local variable so you can be sure the null reference exception is happening where you think it is.
You might need to specify the providerName:
<add name="MyDB" connectionString="Server=STEVEN-PC;Database=MyDB;Trusted_Connection=yes;" providerName="System.Data.SqlClient" />

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.

Categories

Resources