Building Connection string (ADO.NET)
Building Connection string
Introduction:
As we know we use to build the connection strings by copying the existing connection string and renaming the DatsourceName,UserName,Password like that but here we will be discussing about how to build the connection string without copy paste.
Approach:
There is a way to build the connection string for any database which DotNet supports.
1.Open a notepad
And save it as somename.udl where udl (Universal Data Link) is the extension the udl files are used to build the connection string we can directly use the udl file to open the connection to the database
2. Then you can find the file with the following icon as shown below
3. Now open this file by double click it will be as shown below
Now select the provider what ever you like from the list and click next in my case I am selecting the Oledb SqlProvider and clicking next
4.Then you can see the screen as below
then we need to provide the server name ,username if you are not using windows authentication and check the allow saving password option for saving the password in the file now select the database to which you need the connection and then click ok.
Note: – You can find the database list if the details provided by you are valid
5. Now the connection string is ready
How to use this:
1.You can directly use the udl file to open the connection and connect to the database but the limitation is it is supported
by oledb Providers only.Sample code
OleDbConnection connection = new OleDbConnection("File Name= D:\\connection.udl");
OleDbCommand command = new OleDbCommand("select * from Cities",connection);
OleDbDataReader reader;
try
{
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
ListBox1.Items.Add(reader["Cities"].ToString());
ListBox2.Items.Add(reader["Cities"].ToString());
}
ListBox1.DataBind();
ListBox2.DataBind();
}
catch (Exception error)
{ }
finally
{
connection.Close();
}
2. You can directly get the connection string from the file and we can use directly in our code
Open the file with notepad as shown below
And there you can find your connection string like this
[oledb]
; Everything after this line is an OLE DB initstring
Provider=SQLOLEDB.1;Password=mypassword;Persist Security Info=True;User ID=username;Initial
Catalog=datbasename;Data Source=servername
Conclusion: -
By using this udl files we can build connection string for any type of database easily.
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.







Hi,
Thanks for the post.. It greatly helped me..
Thanks,
PRiya.M