Solved

C#: Need help with reading connection string from file

Posted on 2003-11-06
18
399 Views
Last Modified: 2006-11-17
Hi,
I am using VS.NET 2003 and C#. I am trying to create a SqlConnection by reading in the connection string from a text file. But this does not seem to work. I would really appreciate some help as to how I can accomplish this task. Here is my example code:

FileStream fs = new FileStream(@"C:\connPath.txt" , FileMode.Open, FileAccess.Read);

StreamReader m_streamReader = new StreamReader(fs);
path = m_streamReader.ReadLine();

SqlConnection sqlConn = new SqlConnection(path);

******* If I use the following it works just fine:

path = "data source = Saloo\\VSdotNET;database=Test_DB;trusted_connection=yes";

SqlConnection sqlConn = new SqlConnection(path);
*******
I have no clue at this moment as to how to fix this problem. I would really appreciate some help.

Thanks
__________________
Salmaan

When there is a bridge...There is a way

0
Comment
Question by:Salooo
  • 10
  • 6
  • 2
18 Comments
 

Author Comment

by:Salooo
ID: 9696505
Also I have checked the string variable's contents in a message box and it includes everything in it. But Like I said when I assign the variables value to the SqlConn it crashes. I have no clue as to what's the problem.

Any help would be greatly appreciated.

Thanks
__________________
Salmaan

When there is a bridge...There is a way
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 80 total points
ID: 9697577


in your code... "Saloo\\VSdotNET"

remove one of the slashes   "\"

When constructing a string in .Net you need two backslashes so .Net recognizes that it needs to iterpret the second slash as an actual character not an escape sequence.

When the program is pulling in data from somewhere else (like a file or a win API call) it treats the entire string as a string literal without escape sequences.
0
 

Author Comment

by:Salooo
ID: 9701549
Hi,

Thanks a lot dude. That was the problem....everything is working fine now.

Thanks.

Salmaan.
0
 

Author Comment

by:Salooo
ID: 9701723
Hi again,

I have a related question if you could please help me out. I am trying to read from a file that has a type of    ".fil"   I am unable to read this file through the stream reader. Can anyone please tell me how I have to cahnge my code in order to be able to read from a similar file type?

Any help would be greatly appreciated.

Salmaan.
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9702002
It really depends on what format .fil is

If .fil is a simple ascii format, then you can simply read from the file using ASCII or UTF-8 formats.

However, if the file is a serialized object (like word.doc), then you can't read the file without serializing it back into and object (and you have to know the object's format for that).

Open the file in Notepad...

if the file is readable with no extra characters, then it is in ASCII format.

If it is readable, but has a couple of garbage characters in it, then it is probably in UTF-7 or UTF-8 format.


If it is just plain garbage, then it is either encrypted, or serialized.  You will need to get the encryption code, or find out what the object format is so you de-serialize it into that object.
0
 

Author Comment

by:Salooo
ID: 9702068
Hi,

The file opens just fine in notepad without any garbage caharcters. Can you please give me a brief code example as to how I can read from such a file ??? The information in the file is exactly the way it is in the text file that I was using earlier.

I would really appreciate your help.

Thanks.

Salmaan.
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9702665


The code you have above should be able to open and read the file just fine.


*********************

FileStream fs = new FileStream(@"C:\connPath.fil" , FileMode.Open, FileAccess.Read);

StreamReader m_streamReader = new StreamReader(fs);
path = m_streamReader.ReadLine();

SqlConnection sqlConn = new SqlConnection(path);

*********************



However, it is obvious that you are having a couple of problems, I'll post some code in the next hour or so that will explicitly use ASCII or UTF-8 encoding/decoding and that should clear things up for you.

0
 

Author Comment

by:Salooo
ID: 9702677
Yes you are exactly right...I am using the same code but it does not work. I will wait for your further response. Thanks for your patience and help.

Salmaan.
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9702748

Here is some code that I was able to use to open and read from a file with the extension .fil.

Note that I created this fil file with notepad which I know uses ASCII text format (but interpreted it with UTF-8, it is more flexible).



                  FileStream fs = new FileStream(@"C:\temp\test.fil", FileMode.Open, FileAccess.Read);
                  StreamReader sr = new StreamReader(fs,Encoding.UTF8, true);

                  while(sr.Peek()>0)
                        Console.WriteLine(sr.ReadLine());
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Salooo
ID: 9702826
Thank you for the code man, but unfortunately my program is still not working. Here is my code:

FileStream fs = new FileStream(@"C:\connPath.fil" , FileMode.Open, FileAccess.Read);
StreamReader m_streamReader = new StreamReader(fs,Encoding.UTF8, true);
while (m_streamReader.Peek()>0)
      path = m_streamReader.ReadLine();
m_streamReader.Close();
MessageBox.Show(path);
sqlConn = new SqlConnection(path);
sqlConn.Open();

Tell me where am I going wrong here ??

Thanks
0
 

Author Comment

by:Salooo
ID: 9702847
Also the error message says "System.IO.FileNotFoundExceptionCould not find the file"C:\connPath.fil"
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9703358


first thing...

if you file has more than one line (even a carriage return) you are going to end up with an empty path string.

Make sure your file has exactly one line, and one carraige return.

If your file does not contain a carriage return, your "readline" command will not read any data

as for your error...
"System.IO.FileNotFoundExceptionCould not find the file"C:\connPath.fil"


This simply means that it could nto find the file.  The file is in a different directory or it is spelled differently.  I would double check everything.

I would also check to make sure that you are not hiding common file extensions (folder options).


The file might be called connPath.fil.txt, but if you hide common file extensions you would only see the connPath.fil
0
 

Expert Comment

by:razziel
ID: 9703382
FileStream fs = new FileStream(@"C:\connPath.txt" , FileMode.Open, FileAccess.Read);
0
 

Expert Comment

by:razziel
ID: 9703386
Try like this

FileStream fs = new FileStream(@"C:\\connPath.txt" , FileMode.Open, FileAccess.Read);
0
 

Author Comment

by:Salooo
ID: 9704384
Hi guys,
Thank you all for the response. I have resolved the matter I was making a very minor mistake which is not even worth mentioning :-)
Anyways, I have something else I wanted to ask. Is there a utility that I can use with my program that can go and search a file's complete path if I just provide the file's name and type ???

Thanks again for the help TAD.

Salmaan.
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9704495


nothing like that exists straight from .Net, but it is pretty simple to create a recursive algorithm using the
System.IO.Path Class and System.IO.FileInfo class
0
 

Author Comment

by:Salooo
ID: 9714935
Hi _TAD_,
If possible can you please explain how I can create such an algorith. Also if possible can you please give me a brief code example too ????

I would really appreciate your kind help. Thanks.

Salmaan.
0
 

Author Comment

by:Salooo
ID: 9715428
Hi,
I tried the following but it does not provide the correct path:
******************
string fileName = "connPath.fil";
string incompPath = @"\Enterprise\";
string fullPath;
fullPath = Path.GetFullPath(incompPath);
fullPath = Path.GetFullPath(fileName);
MessageBox.Show(fullPath);
**************************

waithing for your kind reply.

Thanks.

Salmaan.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now