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

using stream reader to read and store it in a variable in C#

P: 21
Hi Everyone,
I am having a txt file config.txt on application start up path.I need to read that file line by line using a delimiter ; or , and store the values in a variable to connect to the database. Currently I am hardcoding the initial database connection.

Expand|Select|Wrap|Line Numbers
  1. //code
  2. // Mysql database initialize and connection to Profile    
  3.             string strprofile = "DRIVER={MySQL ODBC 5.1 Driver};" + "SERVER=;" + "DATABASE=profile;" + "UID=root;" + "PASSWORD=selva123;" + "OPTION=3";
  4. //code
I have to read the following from txt file and store in varable so that I can pass this to the connection string.If anyone have any other way of doing this will be great.Thanks in advance.

Dec 29 '08 #1
Share this Question
Share on Google+
7 Replies

Expert 5K+
P: 7,872
Use a StreamReader, then ReadLine() then split on ":" ?
Dec 29 '08 #2

P: 21
HI A simple example will help me more.I am really new to this stuff.
Dec 29 '08 #3

Expert 100+
P: 190
If you are writing the application in .NET, then you can add a config file to your project (Project -> Add New Item -> Config file).

Within this file you can add a connectionStrings element with all this information in text/xml format. It will be copied to your output directory and you can change it as needed without recompiling your application. .Net knows how to read this file and create your database connection from it.

See: ConnectionStringSettings Class for examples and explanation.
Dec 29 '08 #4

P: 21
I am developing a windows application and I dont have that option.Please give me an example to read and store in variable with the delimiter ; .Thanks
Dec 29 '08 #5

Expert 100+
P: 190
Yes you do. The new item is called "Application Configuration File." This is the preferred way to store connection string data and passwords. Sometimes the terminology is different for web application or windows applications, but the concept and practice is exactly the same for both.

Please slow down and read the above MSDN section and some of the links it contains. If you are "really new to this stuff" as you say, then now is a good time to learn the correct way to store and retrieve connection string items.
Dec 29 '08 #6

Expert 100+
P: 190
Here is a better link for starting with connection strings:

Working with Connection Strings.
Dec 29 '08 #7

P: 21
Thanks everyone will do what you suggested
Dec 29 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.