By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,927 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,927 IT Pros & Developers. It's quick & easy.

Write to config file

P: n/a
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<connectionStrings>

<add name="DataBaseConnection"
connectionString="server=localhost;database=SalesD B;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
Sep 7 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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 T wrote:
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<connectionStrings>

<add name="DataBaseConnection"
connectionString="server=localhost;database=SalesD B;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
Sep 7 '06 #2

P: n/a
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

<sk*******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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 T wrote:
><?xml version="1.0" encoding="utf-8" ?>

<configuration>

<connectionStrings>

<add name="DataBaseConnection"
connectionString="server=localhost;database=Sales DB;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

Sep 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.