Saturday, August 21, 2010

How can i connect database from Sql server to asp.net pages?

I have created the web page using asp.net and also database using Sql server want to input through text box from asp.net and insert the same to database i am new to this please guide (created data base using windows authentication in sql server)How can i connect database from Sql server to asp.net pages?
1) Create an SqlConnection object. Supply a connection string to the object





SqlConnection cn = new SqlConnection(';server=YourServer; Database=YourDB; Integrated Security = True'; );





2) Create a command object to hold and execute your query


SqlCommand cmd = cn.CreateCommand();


cmd.CommandText = ';Your Query Here';;








3)Open the connection:


cn.Open();





4)Execute your query


cmd.ExecuteReader(); //if the query retrieves a record set


cmd.ExecuteNonQuery(); //if the query is a Data modification SQL





5) Close the connection


cn.Close();





Hope this helps.

No comments:

Post a Comment