How do I store / retrieve the connection string in / from a text file?
You can store the connection string in the text file or an xml file and later retrieve it. Let’s see some example code to write a connection string to the text file and read it back
C# Version
string connStr = "";
// get connection string in the connStr variable
// Write connection string to text file
StreamWriter sw = new StreamWriter(@"C:\ConnectionString.txt");
sw.WriteLine(connStr);
// ...
// Read connection string from the text file
StreamReader sr = new StreamReader(@"C:\ConnectionString.txt");
connStr = sr.ReadLine();
VB.Net Version
Dim connStr As String = ""
' get connection string in the connStr variable
' Write connection string to text file
Dim sw As New StreamWriter("C:\ConnectionString.txt")
sw.WriteLine(connStr)
' ...
' Read connection string from the text file
Dim sr As New StreamReader("C:\ConnectionString.txt")
connStr = sr.ReadLine()
Back