When ever I create any new ASP.NET Empty Application, first I setup things like arranging folder structure,Creating Database,Finding connection sting for setting up web.conf,So on. Most of the times I forgot the connection string syntax.(I mean Initial Catalog,Integrated Security,so on). Of-course we can copy it form web. But it is always better to have an idea how to create it. For example What if we don’t have internet connection.
In this post I will show you how to create Connection string for our asp.net application using very simple methods.
Note: You don’t Need any new tools for this.
How to add Connection String
First of all lets see how to add connection string to our asp.net application.
If you just create new Empty asp.net application your Solution explorer will look like below.
Now open web.cofig. It will contain xml as below.
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> </configuration>
To add connection string create a new open/close connection strings tags
<add name="ConnectionName" providerName="providername" connectionString="You-connection-string"/>
Finally your web.config look like
<?xml version="1.0"?> <configuration> <connectionStrings> <add name="ConnectionName" providerName="providername" connectionString="You-connection-string"/> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> </configuration>
In general most of the time providerName=”System.Data.SqlClient” and name=”” is connection name we are going to use in your application. We can choose what ever we like. Now, In the reaming post I will explain how to generate connectionString.
This method is very simple and generic one.
1)create new genCon.txt file on your desktop and rename it as genCon.udl(you can choose what ever name you like but extension should be .udl)
2)Now double click on genCon.udl you should get as below.
By default it selected “Microsoft OLE DB Provider for SQL Server”.
3)Now click on Connection Tab. You will get
Enter your Sql server name and select database.
5)Now open genCon.udl in a text file. Viola! here is your connection string.
In this method we will create or find connection string using visual studio.
1)Open database explorer(crtl+alt+s).
2)Click on connect to database icon.
3)Enter server details.
4)Now right click on you database in database explorer and select properties.
5)There you can find connectionString under connection section.