Write to config file

  • Thread starter Thread starter Alan T
  • Start date Start date
A

Alan T

<?xml version="1.0" encoding="utf-8" ?>

<configuration>



<connectionStrings>

<add name="DataBaseConnection"
connectionString="server=localhost;database=SalesDB;User
ID=sales1;Password=sales1"/>

</connectionStrings>


</configuration>



This is the code I use to read the whole string value:

string sConn =
config.ConnectionStrings.ConnectionStrings["DataBaseConnection"].ToString();

I want to do the followings:

1) retrieve each value of the "server", "database", "User" and "Password"

2)write back to configuration file:

server=localhost;database=SalesTestDB;User ID=salesTest1;Password=salesTest1
 
Hi Alan,
You will have to either use xml parser or write your own logic to read
the elements inside the configuration tags.
Same is true for writing back the configuration settings. I havent
found any way by which C# will allow you to write back to the
configuration file. We are using XmlDocument to read and write to
configuration file of our application.

-Sac
 
If your using .net 2 you've got the ConnectionStringSettings and
ConnectionStringsSection types which provider programmatic access without
the need to handle XML.

As for parsing out values from a connection string .net 2 also includes
connection string builder classes which you can supply a connection string
in the constructor (or not) and then access individual values from
properties.

OracleConnectionStringBuilder o = new OracleConnectionStringBuilder( "Data
Source=emp;password=scott;user id=tiger" );

Console.WriteLine( o.UserID );
Console.WriteLine( o.Password );

Although this was done using ODP.NET, I believe similiar classes are
available for all database types included with ADO.NET.

HTH

Glenn

Hi Alan,
You will have to either use xml parser or write your own logic to read
the elements inside the configuration tags.
Same is true for writing back the configuration settings. I havent
found any way by which C# will allow you to write back to the
configuration file. We are using XmlDocument to read and write to
configuration file of our application.

-Sac

Alan said:
<?xml version="1.0" encoding="utf-8" ?>

<configuration>



<connectionStrings>

<add name="DataBaseConnection"
connectionString="server=localhost;database=SalesDB;User
ID=sales1;Password=sales1"/>

</connectionStrings>


</configuration>



This is the code I use to read the whole string value:

string sConn =
config.ConnectionStrings.ConnectionStrings["DataBaseConnection"].ToString();

I want to do the followings:

1) retrieve each value of the "server", "database", "User" and "Password"

2)write back to configuration file:

server=localhost;database=SalesTestDB;User
ID=salesTest1;Password=salesTest1
 
Back
Top