2

I'm rather new both to C# and SQL. I am trying to read the connection string from an external .text file when the program loads, and then use it as a variable whenever I need it in the code, however I'm getting an error I have never seen before. What could be going wrong?

The connection string is this:

@"Data Source=.\wintouch;Initial Catalog=bbl;User ID=sa;Password=Pa$$w0rd";

And the code I am using to transform that into a string is this:

    private void Form1_Load_1(object sender, EventArgs e)
    {

        string connectionString;
        var path = @"C:\Users\Administrator\Desktop\connstring.txt";
        using (StreamReader sr = new StreamReader(path))
        {
            connectionString = sr.ReadLine();
        }

        var connection = new SqlConnection(connectionString);


        SqlConnection conn = connection;

However, as I mentioned I'm getting this error:

An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: Keyword not supported: '@"data source'.

Community
  • 1
  • 1
  • The text file does not need the @, the quotes, or the ending ; – Ron Beyer Jul 30 '15 at 14:45
  • Btw, you could use `File.ReadAllText(path)` for reading, if it's the only line in your file. Looks clearer, as for me. (`File.ReadAllLines`, if it's not the only) – Uladzislaŭ Jul 30 '15 at 14:54
  • possible duplicate of [SQL reading the connection string](http://stackoverflow.com/questions/31703781/sql-reading-the-connection-string) – user2366842 Jul 30 '15 at 15:08

1 Answers1

2

Take the @ symbol and the double quotes out of your text file. Those aren't needed.

Your text file should read:

Data Source=.\wintouch;Initial Catalog=bbl;User ID=sa;Password=Pa$$w0rd;

Important note

You shouldn't be storing your user credentials (especially for sa!!) as plain text in a file.

Thomas Stringer
  • 5,682
  • 3
  • 24
  • 40
  • 1
    @RonBeyer I did specify the double quotes. As for the ending semicolon, that won't cause a problem. In fact, I consistently keep the semi-colon at the end of my connection string just to prevent future issues if I ever append my connection string. – Thomas Stringer Jul 30 '15 at 14:46
  • My comment about the ; was directed at the one that was outside the quotes, as if he just copy/pasted code into the text file, and I think you edited your question to include the quotes just as I had posted the comment... – Ron Beyer Jul 30 '15 at 14:49
  • @Ron, i tried with the semicolons and without and it's working in both ways. Also thank you for your answers gentleman. – LesterNotTheMolestor Jul 30 '15 at 14:49
  • @RonBeyer Nope, omitting the double quotes was in my original answer, take a look at the edit history. Either way, inconsequential. – Thomas Stringer Jul 30 '15 at 14:50
  • Oh, i forgot to mention, this is only an academical exercise, as mentioned, i'm learning both SQL and C#, this is just something that i'm doing to train the languages. – LesterNotTheMolestor Jul 30 '15 at 14:50
  • To add to the important note mentioned in the answer, the better, and more standardized way of dealing with this is to read the connection string in from either web.config (in a web app) or app.config (in a standalone application). Reading in from a plain text file is asking for trouble, as it could very easily be seen from an outside user. – user2366842 Jul 30 '15 at 15:04