detail code for sql data connections?

Answer Posted / nitin kumar

Steps for 1. Using a SqlConnection
// Fully Commented Code
using System;
using System.Data;
using System.Data.SqlClient;

class SqlConnectionDemo
{
static void Main()
{
// 1. Instantiate the connection
SqlConnection conn = new SqlConnection(
"Data Source=(local);Initial
Catalog=Northwind;Integrated Security=SSPI");

SqlDataReader rdr = null;

try
{
// 2. Open the connection
conn.Open();

// 3. Pass the connection to a command object
SqlCommand cmd = new SqlCommand("select * from
Customers", conn);

//
// 4. Use the connection
//

// get query results
rdr = cmd.ExecuteReader();

// print the CustomerID of each record
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
}
finally
{
// close the reader
if (rdr != null)
{
rdr.Close();
}

// 5. Close the connection
if (conn != null)
{
conn.Close();
}
}
}
}

Is This Answer Correct ?    7 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the use of dispose method?

776


In which event are the controls fully loaded?

778


How dataadapter.fill works?

772


What is validation in asp.net?

791


List the major built-in objects in asp.net?

759


Explain about the .NET framework?

847


What is the basic purpose of the required field validator? How can you use a required field validator to check that the user changes the initial value of a text box? a listbox?

1655


What is directive in asp net?

766


List all templates of the repeater control.

775


What are the built-in objects in asp.net?

764


How do active server pages work?

756


Fetch one page value to another page without using state-managment ?

717


How will you load dynamic assembly? How will create assesblies at run time?

723


Describe the difference between inline and code behind - which is best in a?

833


What is ascx?

754