I have developed a Windows application with the backend of SQL Server to insert employee names. I am going to insert the employee details on three databases one by one. So, I like to get connecting values from text file. Whenever I want to change the connection, I just want to enter the login details in the text file.
How can I get a connection string from a text file?
Right-click on your connection and select "Properties". You will get the Properties window for your connection. Find the "Connection String" property and select the "connection string". So now your connection string is in your hands; you can use it anywhere you want.
In computing, a connection string is a string that specifies information about a data source and the means of connecting to it. It is passed in code to an underlying driver or provider in order to initiate the connection.
Use an app.config
(MSDN) file.
Allows you to configure multiple named connection strings which you can access via the System.Configuration.ConfigurationManager class' ConnectionStrings property
Plaese try like this
using System;
using System.IO;
class Test
{
public static void Main()
{
string txtpath = @"c:\textfile.txt";
try
{
if (File.Exists(txtpath))
{
using (StreamReader sr = new StreamReader(txtpath))
{
while (sr.Peek() >= 0)
{
string ss = sr.ReadLine();
string [] txtsplit = ss.Split(';');
//now loop through array
string server=txtsplit[0].Tostring();
string userid= split[1].Tostring(); // user id
string password= split[2].Tostring(); // password
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.ToString());
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With