Monday, April 23, 2012

How to connecting to SQLserver from asp.net application

Title: How to create connection string in asp.net using C#.net

Description:
If you want to connect with SQLServer from asp.net application ,you can either use the OLEDB class as well as SQL Client classes.While connecting with OLEDB Connection to mention the target data source we specify the name of the provider which has be to be used.But when you use SQL connection or Oracle connection classes the connection string does not require the provider name to be specified.Here i will show an example to get the data from database using connection object

Example
Using system.Data.SqlClient
SqlConnection cn;
SqlCOmmand cmd;
SqlDataReader dr;
under page load
cn=New SqlConnection("user id=sa;password=123;Database=asp.net");
cmd=New SqlCommand("select*from students",cn);
cn.open();
dr=cmd.ExcuteReader();
if(dr.Read())
{
Response.Write(dr[0].Tostring());
}

No comments:

Bel