Solved

C#: Need help with reading connection string from file

Posted on 2003-11-06
18
420 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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
 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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