While using ADO.NET first thing you must understand is the SqlConnection class that is used for making connection with application and with the database.
SqlConnection class takes parameters as the database name called as initial catalog ,the server name the user id and the password.
The first thing you need to understand is there are many ways for defining SqlConnection.
First Way:
Add this entry to your Web.Config.
<add connectionString="Data Source=localhost;Initial Catalog=demodatabase;User ID=sa;Password=demopassword" name="DBConnectionString" providerName="System.Data.SqlClient" />
After this make the connection object and pass connection string name into it.
SqlConnection Conn=new SqlConnection(ConfigurationManager.ConnectionString["DBConnectionString"].ConnectionString);
Second Way:
The second way is to pass the entire string into the connection object directly.
SqlConnection Conn=new SqlConnection("Data Source=localhost;Initial Catalog=demodatabase;User ID=sa;Password=demopassword" name="DBConnectionString" providerName="System.Data.SqlClient");
Third Way:
The third way is to take a string variable and pass string into it and pass that string into the connection object.
static string connString="Data Source=localhost;Initial Catalog=demodatabase;User ID=sa;Password=demopassword" name="DBConnectionString" providerName="System.Data.SqlClient";
SqlConnection Conn=new SqlConnection(connString);
0 Comment(s)