I am very new to app.config files:
In an app.config file which look like this
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="connectionstring" connectionString="Server=server;User ID=bar;Password=foobar" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
I want to split up the connection string into various sections like Server, UserId, etc in a different file and then concat it together in my app.config:
Pseucode
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="connectionstring" connectionString=Server+UserId+Password providerName="System.Data.SqlClient />
</connectionStrings>
</configuration>
Is this possible?
Related
I'm need to read connectionStrings from different files .config
file1.ddl.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="" connectionString=""/>
</connectionStrings>
</configuration>
file2.ddl.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="" connectionString=""/>
</connectionStrings>
</configuration>
I try ConfigurationManager.ConnectionStrings["NAME"].ConnectionString but i recive "Object not set" and i can't use ConfigurationSettings.AppSettings because it´s deprecated
enter image description here
U can try add the key's in the same file.
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location; //exePath
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
ConnectionStringsSection section = config.GetSection("connectionStrings") as ConnectionStringsSection;
string connectionFile = section.ConnectionStrings["nameConnection"].ConnectionString;
Here is a snippet of my app.config file.
<MyProject>
<ConnectionStrings>
...
<Operational providerType="SqlServer" provider="" dataSource="<ServerName>" initialCatalog="<DBName>" security="" userName="<SomeUser>" password="<Password>" />
...
</ConnectionStrings>
</MyProject>
In my C# code, I have this value available in some variable, like...
string OperationalConnectionConfiurationPath = "MyProject/ConnectionStrings/Operational";
How can I get my SqlConnection object out of this?
connectionStrings are a child of the configuration section. Get rid of that <MyProject>stuff.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Operational" connectionString="Data Source=SERVER_NAME;Initial Catalog=DB_NAME;User Id=USER_ID;Password=PASSWORD;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
You can get the connection string by:
using System.Configuration;
var connectionString = ConfigurationManager.ConnectionStrings["Operational"].ConnectionString;
I am making a windows service.
It contain one App.config file as below:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SQLConnectionStr" value="Data Source=192.168.1.116;Initial Catalog=Conezone;User Id=sa;Password=saadmin#123;"/>
<add key="FilePath" value="D:\Autoparts Guru\LINES2\"/>
</appSettings>
</configuration>
To get FilePath value in service code, I am writing
ConfigurationManager.AppSettings["FilePath"].ToString()
But it generates NullReferenceException.
Hope i get the answer quickly.
This code may help you:
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="SQLConnectionStr" value="Data Source=000.000.000.000;Initial Catalog=mydb;User Id=sa;Password=sa;"/>
<add key="FilePath" value="myfilepath"/>
</appSettings>
</configuration>
Namespace
using System.Configuration;
Code:
string filepath = string.Empty;
filepath = ConfigurationManager.AppSettings["FilePath"];
I'm using Mindspace.Lightspeed in my C# desktop application for the first time but I'm getting errors and unable to connect to database.
My App.config contains the following code:
<?xml version="1.0" encoding="utf-8" ?><configuration>
<configSections>
<section name="lightSpeedContext" type="Mindscape.LightSpeed.Configuration.LightSpeedConfigurationSection,Mindscape.LightSpeed" /></configSections><connectionStrings>
<add name="DefectsConStr" connectionString="Data Source=.\sqlexpress2k5;Initial Catalog=Defects;Persist Security Info=True;User ID=sa;Password=123"
providerName="System.Data.SqlClient" /></connectionStrings> <lightSpeedContext>
<add name="Development" connectionStringName="DefectsConStr" dataProvider="SqlServer2005"/> </lightSpeedContext> </configuration>
I have managed to reproduce your problem, you need to change your configuration file so that the section name is
lightSpeedContexts and not lightSpeedContext
see my configuration file
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="lightSpeedContexts" type="Mindscape.LightSpeed.Configuration.LightSpeedConfigurationSection,Mindscape.LightSpeed"/>
</configSections>
<connectionStrings>
<add name="Blog" connectionString="Data Source=Blog.dat"/>
</connectionStrings>
<lightSpeedContexts>
<add name="Blog" connectionStringName="Blog" dataProvider="SQLite3" identityMethod="KeyTable" pluralizeTableNames="false"/>
</lightSpeedContexts>
</configuration>
I'm doing a c# course at university, and now I'm beginning linQ to xml, linQ to Sql-Server, etc.
I work with the example projects in two PCs (university PC and office PC)
Is there an easy way to change connection string (from app.config) at runtime, or designtime (using a constant in main.cs) so I can use a connection string at university, and a connection string at the office easily?
Thanks a lot in advance,
You could try something like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Environment" value="Home"/>
</appSettings>
<connectionStrings>
<add name="Work" connectionString="..."/>
<add name="Home" connectionString="..."/>
</connectionStrings>
</configuration>
and, later:
string environment = ConfigurationManager.AppSettings["Environment"];
ConfigurationManager.ConnectionStrings[environment].ConnectionString;
Another way of doing it:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="HomeEnvironment"
connectionString="Data Source=**HOME-COMPUTER**\SQLEXPRESS;Initial Catalog=**HomeDatabase**;Integrated Security=True;"
providerName="System.Data.SqlClient" />
<add name="WorkEnvironment"
connectionString="DataSource=**WORK-COMPUTER**\SQLEXPRESS;Initial Catalog=**WorkDatabase**;Integrated Security=True;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
and use it like this:
var environment = Environment.MachineName == "HOME-COMPUTER" ? "HomeEnvironment" : "WorkEnvironment";
var connectionString = ConfigurationManager.ConnectionStrings[environment].ConnectionString;
var dbContext = new **Databasename**ContextDataContext(connectionString);
Bold strings should be customised as required