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 _dopostback in asp net?

604


Dataset is the disconnected environment. suppose if you are binding records to gridview (disconnected environment) and you are making changes to the the grid but before updating the database if any other user modify the data, how will you avoid such problem?

1810


What is a server cookie?

592


What is the request flow used for asp.net mvc framework? : asp.net mvc

617


Describe the differences between the lifecycles of Windows services and Standard EXE?

685






What are different types of authentication techniques that are used in connection strings to connect .net applications with microsoft sql server?

624


What does mvc represent in asp.net? : asp.net mvc

624


Differentiate between authentication and authorization.

643


How is it possible for .NET to support many languages?

386


Can we use html in asp.net?

590


Is sql backend or frontend?

644


Explain significance of routing? : asp.net mvc

649


What is asp.net futures?

653


What is asp.net and ado net?

637


What is cross page posting in asp net?

612