Working with Connection Object:-


  • Connection object is used to establish connection to the database
  • Using connection object it is not possible to perform any kind of operations like inserting, updating, deleting the records on database objects like Tables, Views, Stored Procedures etc
 Properties and Methods with Connection Class:-

SNO
Property and options
      Type
         Description
01
ConnectionString
     string
Gets or sets the connection string value used to open connection to the database
02
ConnectionTimeout
    int
Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error. Default time is 15 seconds
03
DataSource
  string
Gets the name of the instance of the Database Server to which to connect
04
PacketSize
  int
Gets  the size (in bytes) of network packets used to communicate with an instance of the Database Server
05
State
  Closed
  Open
  Connecting
  Executing
  Fetching
  Broken
  enum
Indicates the state of the connection Object, all the connection state values are available in ConnectionState enumeration
06
ServerVertion
string
Gets a string that contains the version of the instance of the Database to which the client is connected

Methods with Connection Class:-

SNO
Methods
Description
01
Begin Transaction( )
Starts a database transaction
02
Close( )
Used to Close an opened connection to the database. This is the preferred method of closing any open connection
03
CreateCommand( )
Creates and returns a Command object associated with the respective Connection class
04
GetSchema( )
Returns schema information for the data source of this Connection
05
Open( )
Opens a database connection with the property settings specified by the ConnectionString

Steps to work Connection Object:-

Step1: Declare Connection Object
Syntax: Class Name Object Name;
Ex: SqlConnection Con;

Step2: Define Connection Object
Syntax: Object Name=new Class Name(“Connection String”);
Ex: Con=new SqlConnection(“Connection String”);

Step3: Open the Connection
Syntax: Connection Object Name.Open( );
Ex: Con.Open( )

Connection Strings to connect to SqlServer Using SqlClient Provider:

Syntax:
Server=ServerName;User..Id=UserName;Password=UserPassword; Database=DBName

      Ex:Server=SampathServer;User Id=sa;Password=sampath;Database=Employe

No comments:

Post a Comment