Problem in using the database connection string mentioned in a text file


Nov 27, 2017
Programming Experience
Dear friends,
plz help to solve my problem.
I want to place the database connection string in a text file
and i want to read the text from windows form of c#.
the connection string in a text file named "server.txt" is as follows

Data Source=MADHU\\SQLEXPRESS;Initial Catalog=GSTNDB;User ID=sa;Password=admin123

my code in the button click is
    private void button1_Click(object sender, EventArgs e)
        string Conn;
        string connetionString = null;
        SqlConnection connection;
        SqlDataAdapter adapter;
        SqlCommand command = new SqlCommand();
        DataSet ds = new DataSet();
        string srv = System.IO.File.ReadAllText(@"d:server.txt");
        connetionString = srv;
        connection = new SqlConnection(connetionString);
        command.Connection = connection;
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "gst_proc";
        adapter = new SqlDataAdapter(command);

On executing it showing the error message.......

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

Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL

If I use the same connection string as static value it is working fine, but if i read the text file it showing
the above error message.

thanks and regards

Last edited by a moderator:
Firstly, I've formatted your code snippet for readability. Please do so for us in future. You can type the tags yourself or use the tool bar button on the advanced editor.

As for the issue, your actual connection string should be:
Data Source=MADHU[B][COLOR="#008000"]\[/COLOR][/B]SQLEXPRESS;Initial Catalog=GSTNDB;User ID=sa;Password=admin123
rather than:
Data Source=MADHU[B][COLOR="#FF0000"]\\[/COLOR][/B]SQLEXPRESS;Initial Catalog=GSTNDB;User ID=sa;Password=admin123
In C# code, the forward slash is an escape character, i.e. it gives other characters special meaning. For instance, to write a literal string with a line break in it, you do this:
var str = "FIRST LINE\r\nSECOND LINE";
The '\r' is a carriage return and the '\n' is a line feed. Because a forward slash has special meaning in code, if you want to include a forward slash character in your string then you have to escape it, which is done using... a forward slash. For instance, if you wanted to specify the file path 'C:\SomeFolder\SomeSubFolder\SomeFile.txt' you would actually do it like this:
var filePath = "C:\\SomeFolder\\SomeSubFolder\\SomeFile.txt";
In each case, the first forward slash of a pair is not actually part of the text, but rather it is an escape character that indicates that the second forward slash is part of the text. Note that you can now use verbatim string literals if you don't need to any other escape characters which can make things more clear. To do that, you can prefix a string literal with a @ symbol and then every forward slash is assumed to be a literal character:
var filePath = @"C:\SomeFolder\SomeSubFolder\SomeFile.txt";
In your case, if you write your connection string as a string literal then you need to either escape the forward slash:
var connectionString = "Data Source=MADHU\\SQLEXPRESS;Initial Catalog=GSTNDB;User ID=sa;Password=admin123";
or use a verbatim string literal:
var connectionString = @"Data Source=MADHU\SQLEXPRESS;Initial Catalog=GSTNDB;User ID=sa;Password=admin123";
I would recommend the second option. If you're reading text from a file or some other source though, you're not using a string literal, i.e. a string written directly in your code. In that case, you don't use escape characters because they are for literals only.
Top Bottom