Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C#: Need help with reading connection string from file

Status
Not open for further replies.

salooo

Programmer
Sep 11, 2003
30
US
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

Salman.
 
1. Put the following as the first line in the C:\connPath.txt file :
data source = Saloo\\VSdotNET;database=Test_DB;trusted_connection=yes

2. Make sure it is the first line.
3. Make sure the connPath.txt is located on C:\ folder
4. The following code should work e.g. the path will have exactly the first line of the file:

string path = null;
SqlConnection sqlConn = null;

try
{
FileStream fs = new FileStream(@"C:\connPath.txt" , FileMode.Open, FileAccess.Read);
StreamReader m_streamReader = new StreamReader(fs);
path = m_streamReader.ReadLine();
m_streamReader.Close();

sqlConn = new SqlConnection(path);
sqlConn.Open();

}
catch (Exception e)
{
string sMsg = e.GetType() + e.Message;
MessageBox.Show(sMsg);
}

A solution to get the connection string is to put it in the .exe.config and retrive it from there.
-obislavu-
 
Hi obislavu,
Thank you for your quick response but alas the solution you provided still does not work I get the message saying the SQL Server does not exist or access denied. This is the same thing that I saw previously. I looked at the contents of the string variable before it went into the SqlConnection, it does go in and read the correct info but still it does not work. Please help !!!!

Appreciate your kind help.

Salmaan.
 
Salmaan -

This is separate problem - the contents of the file were read correctly, but SQL Server isn't letting you in. Can you contact your DBA to find out what the correct values should be for your connect string?

Also -
The connect string format changed in ADO.NET (from the previous generation COM-based ADO library). The format is detailed in the .NET help file.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Hi Chip H,

Thanks for the response buddy. I have been waiting for someone to respond. Anyways may be you are right, but I get the message saying the "SQL Server does not exist or access denied"

Well if you look at my code above I mentioned that if I pass the information after reading from a file I cant get it to work, but if I pass the same value by assigning it to the string variable it works just fine i.e.
******* 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);
*******
this works fine. Also I am using the SQL Server desktop engine.

Please let me know if you come up with a solution or have any further questions.

Thanks for responding.

Salmaan.
 
Why don't you just use an app.config file? This is a whole lot easier than file reading. If not I would still recommend you put your stuff in xml and just read it in through DataSet.ReadXml.

foo
 
Here is a string connection for SQL server that I am using :
string connectionstring="user id=imfserver;Password=xxxxx;Initial Catalog=IMF;Data Source=ATWCIA;Connect Timeout=10";

You should provide:
-valid userid id/password (here imfserver/xxxxx)
-The name of the DB (here is IMF)
-The name of the machine (server) (here is ATWCIA). This machine must be reacheable from your network.

If use exe.config then add in a section , let say <sql>
<sql>

<add key=&quot;connectionstring&quot; value=&quot;user id=imfserver;Password=;Initial Catalog=IMF;Data Source=ATWCIA;Connect Timeout=10&quot; />
</sql>

-obislavu-
 
Hi guys,

I would like to thank you all for your kind feedback on the issue. I have found the problem...its very simple. I was using 2 slashes in my data saource's name and that is what caused the problem. We only need 1 slash when we are reading from file. Everything is working fine now.

Thanks again for the helpfull suggestions.

Salmaan.
 
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 &quot;.fil&quot; 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? I would really appreciate if anyone can provide a brief code example.

Any help would be greatly appreciated.

Salmaan.
 
If your .fil file is a text file, there should not be a problem raeding it due to the extension (.fil).
To get some feedback, give as details about this file.

-obislavu-
 
Hi,

The file opens just fine in notepad without any garbage caharcters. The information in the file is exactly the way it is in the text file that I was using earlier. This means that the file is in ASCII format right ? So in that case it makes perfect sense it should open just as the text file but alas it does not. If you understand the problem can you please give me a brief code example as to how I can read from such a file ???

I would really appreciate your help.

Let me know if this expalins things a little bit.

Thanks.

Salmaan.
 
What sort of app created this .fil file?
It's not an extension I'm familiar with.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Hi Salmaan,
Put here the code you are trying to read this text file and also if it is possible , few records of this file (the first two).
-obislavu-

 
Hi,

I am not sure as to what program is creating these files at the moment. I think these are called Filled-in Form Files which are ASCII or UTF8 (I think UTF8 format) text files. The .fil file can either be edited by opening up its form and opening the .fil file, or by editing in a text editor.

I have found the aolution. I am pasting it here for future reference. Thanks a lot all you guys for the help.
******************************
FileStream fs = new FileStream(@&quot;C:\connPath.fil&quot; , 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();
*****************************

Salmaan.
 
OK, if they're UTF-8, then you'll need to run the byte array you read through the UTF8Encoding class to convert it to a String that you can use in your program. It's in the System.Text namespace.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top