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 good practice to implement validations in aspx page?

628


What are the session variables?

512


what are the web form events available in asp.net?

556


What is state management react?

505


What is the difference between session and viewstate in asp.net?

606






What is the differences between a primary key and a unique key in sql server?

534


In early binding will the method invoked on com component will verify it?s existance in the system or not ?

2134


List some of the important session state modes of asp.net.

500


Is sql backend or frontend?

553


Where sessions are stored in asp.net?

532


How to store checkbox value in database in asp.net mvc? : Asp.Net MVC

509


1.how to encrpt query string in asp.net? 2.there are 10000 records then i wnat display 5000 records one gridview and 5000 records another grid view what is the process?

1660


when a request is made in Life cycle of ASP.NET page .

527


Why session is necessary in web application?

509


What is a multilingual website?

561