I have an access database connected to my code via a connection string as shown:
connection = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Me\Documents\Visual Studio 2013\Projects\Computing Project\Database.accdb;
Persist Security Info=False;");
However I would like this app to be able to be installed on other devices, so I'm guessing the connection string has to be dynamic and change on each system. How would I use this in my code?
Add an App.config to the solution with following content
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
<connectionStrings>
<add name="MyCon" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Me\Documents\Visual Studio 2013\Projects\Computing Project\Database.accdb; Persist Security Info=False;"/ >
</connectionStrings>
</configuration>
Then in code,
connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["MyCon"].ToString());
PS: ConfigurationManager class is in assembly System.Configuration.
So please add a reference of this assembly before using it.
Related
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.
I have created a application with c#. I define a connection string in app config file. Here is my app config file code:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="True">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<appSettings>
<add key="con" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\myFolder\Mydb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=bd1234" />
</appSettings>
</configuration>
I want to create that code to this following format and encrypted password value.
<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<appSettings>
<add key="ServerName" value="user"/>
<add key="Provider" value="Microsoft.Jet.OLEDB.4.0"/>
<add key="Data Source" value="C:\myFolder\Mydb.mdb"/>
<add key="Persist Security Info" value="True"/>
<add key="Jet OLEDB:Database Password" value="*******"/>
</appSettings>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
How can i do that?
Good question.
Ideally, if you can use windows authentication, this will solve your issue without having to protect your config file.
Try adding the following:
<add key="con" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\myFolder\Mydb.mdb;Persist Security Info=False;Integrated Security=SSPI;Jet OLEDB:Database" />
Here is an MSDN doc with more details:
https://learn.microsoft.com/en-us/visualstudio/ide/managing-application-settings-dotnet?view=vs-2017
If you do need to protect your app config, you can use the .NET famework "ProtectSection" function which will encrypt your config file (using machine.config key), and your application will be able to access the config details, but it is not easily visible.
Given the security implications it is worth having a detailed look of how it works:
https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/connection-strings-and-configuration-files
Have you tried below?
string value = System.Configuration.ConfigurationManager.AppSettings[key];
Requires add reference System.Configuration
if you know that how you will do the encryption then you can save the encrypted password following this way
How to update app settings key value pair dynamically on app.config file in c# winforms
To do the encryption and save the app settings follow the link of #ksdev
Encrypt password in App.config
I am inserting data in my form but it is not updated in database. When I refresh the table data I am getting this error:
This database cannot be imported. It is either an unsupported SQL Server version or an unsupported database compatibility.
There are no errors in my code. According to other articles I set the "Copy to Output Directory" property of database to "Copy if Newer" too. Still it is giving me this error. How can I fix this error? What changes should I make? I am using visual studio 2015 and local database.
This is my connection string:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="dbx"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\db\Database.mdf;Integrated Security=True;MultipleActiveResultSets=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
Hello im having trouble with my connection when i transfer my code to another pc. Here in its original computer it is working fine. But in another computer it says connection error. Im sure about the new connection string on that pc is correct so i think it has something to do with configuration manager.
The other pc has same version of visual studio and sql express
App config code:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="MPnew.Properties.Settings.MARISCHELLdatabaseConnectionString1" connectionString="Data Source=BENJOPC\SQLEXPRESS;Initial Catalog=MARISCHELLdatabase;Integrated Security=True" providerName="System.Data.SqlClient"/>
<add name ="cnn" connectionString="Data Source=BENJOPC\SQLEXPRESS;Initial Catalog=MARISCHELLdatabase;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
Connection code:
using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnn"].ConnectionString))
{
if (cnn.State == ConnectionState.Closed)
cnn.Open();
// continue code
}
I think BENJOPC\SQLEXPRESS is giving wrong connection message. I suggest to use correct server name or you could just use .\SQLEXPRESS instead of BENJOPC\SQLEXPRESS.
So on my development machine I have SQL Server Express and Visual Studio installed. I finished creating the application and everythings working. Here is the connection string in the App.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="ColegioDBEntities"
connectionString="metadata=res://*/Repositories.ColegioModel.csdl|res://*/Repositories.ColegioModel.ssdl|res://*/Repositories.ColegioModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;Initial Catalog=ColegioDB;Integrated Security=True;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
</connectionStrings>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" />
</startup>
</configuration>
I created an installation project and want to install the application on a Virtual Machine (to simulate deployment conditions) which is on the same network as where the SQL Server Express is installed. What would the connection string be? Like, 192.168.2.102/SQLExpress?
Any ideas?
surely the IP address or host name will be needed then the instance name of SQL Express. Consider that by default SQL Express is installed with tcp and named pipes disabled so you have to enable those facilities in the configuration manager before you could access it remotely from another machine.
The sql connection string is part of the "connectionString" attribute and is encoded as a substring with masked quotes:
provider connection string="Data Source=.\SQLEXPRESS;Initial Catalog=ColegioDB;Integrated Security=True;MultipleActiveResultSets=True"
so the config for your ip adress must look like:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="ColegioDBEntities"
connectionString="metadata=res://*/Repositories.ColegioModel.csdl|res://*/Repositories.ColegioModel.ssdl|res://*/Repositories.ColegioModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=192.168.2.102\SQLEXPRESS;Initial Catalog=ColegioDB;Integrated Security=True;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
</connectionStrings>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" />
</startup>
</configuration>