Resources » Code Snippets » ASP.NET WebForms
How to write the connection string and place it on to web.config file
In this article, I am clearly explaining about how we can get the correct connection string without any error or mistake and also without writing anything and then we can use this connection string in our configuration files or in code files as per our requirements.
Today i came across a simple but very interesting and fantastic idea about how to write/get our
connection string.. to use it in our application's configuration file or at anywhere code files wherever we want.
As most of the time many of us have lot of confusion regarding the connection string they use to place in the configuration files or in their code files-
-- Whether the server name is correct or not?
-- Am I writing the correct user name or not?
-- Is password correct or not?
-- Will it take the connection string in capital letters?
-- Or do I need to pass the connection string in small letters?
-- Do I need to write the provider name? Is It required? If required then which provide name I need to write?
-- The provider name which I am writing is having the correct spelling or not?
-- Whether the name of the the provider is correct which I have written in the connection string?
-- where do we need "," and where we required ";" in the connection string? this is also a big task and always we need to keep in mind...
These are the common question which comes in to our mind when we write the connection string mainly in our configuration files....and believe me most of the time, if we forget anything out of the above, we won't be able to conenct to the database...and then we keep on doing some R&D with our database...by thinking that why its not connecting....
so today i am going to explain some steps and tips by which anybody can get the connection string very easily without writing any thing:
just copy and paste...which is our favourite...
cool job..so.. lets start..with the steps
Step #1: Go to Start --> Run --> type notepad or simply open a notepad
Step #2: go to File --> Save as --> "filename.udl"
here are we just saving the notepad with the extension .udl.
keep in mind here that, you should save the file with the name.udl in double quotes then only it will be able to recognise, else your txt file with this name will be saved.
Step #3: Now the location or the path where you have saved the fileName.udl, you can see a new file is getting created. It's not the text file..magic starts.
cool...we are just goign on..
Step #4: Open the fileName.udl by double clicking
Step #5: There are 4 tabs in the file: Provider, Connection, Advanced and All
Step #6: First go the provides tab. Choose the provide name by which you want to connect to your database e.g Microsoft OLe Db Provider for Sql Server
Step #7: In the Connection tab, you need to choose the database name, if you are not able to find out the database name then you can enter it. also if you dont remember the database name then simpley put "." (dot) in the Server Name. It means whatever the databases in the system it will show all.
Step #8: Enter user name and password if required. if you want to connect with oyur windows authentication without user name and password, then simply choose the first option.
Step #9: Select the database on this server option is to choose the database for which you want the connection or if you dont remember then enter it.
Step #10: click on Test Connection button to check that whether the information whoch you entered is correct or not...whether you can use this connection or not. If you get the message "Connection tested successfully". it means you cn use this connection.
Click on OK.
now you have done all the stuffs whichare required to check your database, connection and all..
Now you need to get the connection string:
Follow the below steps:
Step #1: Open the same file fileName.udl in notepad.
Step #2: it will show your connection string which you just need to copy and paste in you application's configuration file.
Important Note: One more important thing here is that the connection string which you will get will contain the Provider name, your user id, your password and some other security attributes etc.
But keep in mind that whenever you are using the SqlClient as the provider name form the Provider tab of the udl file, there is no need to use the provider name in the connection string becasue as you might be knowing that SQLProvider is directly linked with our visual studio and there is no need to use the provider to link to the datbase.
The reason is that it works based on the TDS(Tabular Data Stream) protocol which is directly linked with the visual studio so its fast comapre to other databases. so there is no need to create a layer of provider to connect to the database.
But if you are using any other provider except SQLClient like ODBC or OLEDB, the provider is required.
So you can ignore the provider name in the connection string in case of the SQLClient and use rest of the connection string.
So no need to think about the questions which are mentioned in the staring of the post.
So be cool...and use it...
Did you like this resource? Share it with your friends and show your love!
|Author: Deepak Negi 06 May 2012||Member Level: Silver Points : 0|
After going through this post, now creating connection strings will not be a hactic job any more...
Great piece of information..
Follow us on Twitter: https://twitter.com/dotnetspider