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 are the server control tags in asp.net.?

811


IN an ASP.NET Web application if there is any error, how can you debug?

819


In which event of the page viewstate is available?

652


What are the elements of a website?

727


Explain the main differences between asp and asp.net?

730


if i want to give an alert message like "try after sometime" to a web page which is being seen by other person.if a web page is not seen by anyone then it should display otherwise it show a display a message stating that other person is viewing so try after some time........how can i implement this.

2024


Where you store Connection string in "Web.Config" file in ASP.NET?

789


Write some code using interfaces, virtual methods, and an abstract class`

1833


Where do the cookie state and session state information be stored?

705


Explain about the Class view window?

787


What are the ways to show data grid inside a data grid for a master details type of tables? If we write any code for DataGrid methods, what is the access specifier used for that methods in the code behind file and why?

789


i want the asp.net technical questions and answeres

1804


What is csrf attack in asp.net?

714


What is the difference between an htmlinputcheckbox control and an htmlinputradiobutton control?

865


Describe the Server Control Events of ASP.NET?

889