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

Why web api is better than wcf?

570


What is recordset asp?

555


What are the different validators in asp.net?

568


How many types of server controls do we have?Also explain differance between them taking an example of ASP.NET?

2374


How you can return View from ASP.NET Web API method?

555






Write a code snippet to implement the indentation in json in web api.

584


What is client side state management?

513


What is asp.net and ado net?

526


Apart from IDE what are the enhancements in asp.net 2.0?

1637


What is data caching?

571


How you will improve web application performance?

564


How to integrate angular 8 with asp.net mvc 5? : Asp.Net MVC

504


Where is the session stored?

644


What is a web based system?

474


How we implement web farm and web garden concept in asp.net?

550