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

Which type if caching will be used if we want to cache the portion of a page instead of whole page?

493


Explain model, view and controller represent in an mvc application? : asp.net mvc

504


What is autopostback in asp net?

573


How will you load dynamic assembly?

526


Please brief not about xsd,xslt & xml?

536






Define application state variable and session state variable?

550


What is the difference between stored procedure vs function?

562


How can we Validate a Controls in ASP.NET page using JavaScript?

622


Why session is necessary in web application?

503


How can we apply themes to an asp.net application?

544


What are the versions of garbage collection?

1448


What does a switch do?

527


What is routing in MVC?

584


How do I use response redirect?

544


Elaborate differentiation between ViewState and SessionState?

603