MCQs Test!! VB Basic Test !! Essentials of VB !! Creating User Services !! function !! Anatomy !! GK March-2019 !! DATA TYPES IN JAVA !! Classes ,Constructor ,Methods,Inheritance !! Ecology-I !! Biology 2019 !! Gk april 2019 !! Biology (living world) !! Plant anatomy !! CELL STRUCTURES AND FUNCTION !! General biology !! Human physiology !! Data structure !! Ecology-II !! GK TEST !! Reproduction in plants !! Human reproduction !! The ABC of C !! Operating system set1 !! Operating system set2 !! Gk and current affairs !! Animal kingdom !! Number system !! Animal kingdom-II !! Gk TEST-III !! Number series !! Microorganisms !! Interview question in c !! Time and Work !! Percentage !! General history !! Average !! Cells !! Locomotion and Movement !! paging !! Bacteriology !! Page replacement policy !! Secondary memory and Disk scheduling !! Introduction of Nervous System and neurons !! Plant harmones !! Transport and Mineral Nutrition in Plants !! Neurotransmitters !! Modern Indian History !! Photosynthesis in Plants !! Modern Indian History-II !! Plant respiration !! Environment & Biodiversity !! Environment & Biodiversity - II !! Algae, Bryophytes, Pteridophytes !! Area !! Cricket !! Days of the Year (India and the World) !! Plant Pathology - I !! Indian Transport System !! Plant Pathology - II !! Aptitude !! Biology !! Botany !! C language !! Computer Science !! Core Java !! Gate !! GK !! Mysql !! Operating system !! Physics !! VB-Visual Basic !! Zoology


ADO.NET Projects

ADO.NET Project 1

ADO.NET Examples


Previous Home Next

A SqlConnection is an object,as any other C# object. we just declare and instado.netiate the SqlConnection all at the same time, as shown below:

SqlConnection con = new SqlConnection(
                 "Data Source=(local);
                  Initial Catalog=Emp;
            Integrated Security=sspi");

The SqlConnection object instado.netiated above by using a constructor with a single argument of type string and this argument is called a connection string.

There are four Connection String Parameter Name:-

  1. Data Source: Data Source Identifies the server and it Could be local machine, machine domain name, or IP Address.
  2. Initial Catalog: Database name
  3. Integrated Security: Integrated Security set to SSPI to make connection with user's Windows login.
  4. User ID: Name of user configured in SQL Server.
  5. Password: Password matching SQL Server User ID.
The following shows a connection string, using the User ID and Password parameters:
SqlConnection conn = new SqlConnection(
             "Data Source=DatabaseServer;
	     Initial Catalog=Northwind;
	            User ID=YourUserID;

Using a SqlConnection

The aim of creating a SqlConnection object is so we can enable other ADO.NET code to work with a database and SqlCommand and a SqlDataAdapter take it a connection object as a parameter. The sequence of operations occurring in the lifetime of a SqlConnection are given below:

  1. Instado.netiate the SqlConnection.
  2. Open the connection.
  3. Pass the connection to other ADO.NET objects.
  4. Perform database operations with the other ADO.NET objects.
  5. Close the connection.

The Open() Method:

The Open() method of the Connection object establishes a connection to the data source.,because database connections are a very expensive resource memory-wise, so we should only call the Open() method just before we're ready to retrieve the data. This ensures that the connection is not open any longer than it needs to be.

The Close() Method

After we are done retrieving data,we should call the Close() method of the Connection object. This closes the connection to the database.


Example Using SQLCONNECTION Object

using System;
using System.Data;
using System.Data.SqlClient;
class sqlConnectivityexample
static void Main()
SqlConnection conn = new SqlConnection(
"Data Source=(local);Initial Catalog=
 Emp;Integrated Security=SSPI");
SqlDataReader rdr = null;
// 2. Open the connection
// 3. Pass the connection to a command object
SqlCommand cmd = new SqlCommand
("select * from emp", conn);
// 4. Use the connection
// get query results
rdr = cmd.ExecuteReader();
// print the CustomerID of each record
while (rdr.Read())
// close the reader
if (rdr != null)
// 5. Close the connection
if (conn != null)

Previous Home Next