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
What is the namespace to create thread in .net?
What is csrf attack in asp.net?
What is difference between inproc and outproc?
What's the use of response.output.write()?
What is anonymous authentication?
Explain the difference between array and linkedlist?
What are the advantages and limitations of query string?
Define repository pattern in asp.net mvc? : asp.net mvc
Which authentication uses a combination of windows and iis authentication?
What is css and what is it used for?
Whats the difference between registerclientscriptblock, registerclientscriptinclude and registerclientscriptresource?
What does asp in asp.net stand for?
Explain asp.net page life cycle?
explain code with datachaching with example
What is a session government?