ADO.NET - SPLessons

ADO.Net SqlConnection

Home > Lesson > Chapter 5
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

ADO.Net SqlConnection

ADO.Net SqlConnection

shape Description

Connection  object is used to establish the connection.

shape Properties

The following are the properties of the Connection object.
  1. Connection String: Used to get/set the required connection string value to connect with the database(SQL). ConnectionString belongs to the string datatype.
  2. Connection timeout: Used to set/get the timeout period in sessions i.e frontend is required to wait to get the response from the backend.
    • Default connection timeout period is "20sec".
    • If the timeout does not get any response from the backend within this time period then the compiler raises the timeout error.Credential: Used to set/get the required credential value to the connection object. Here, the credential is nothing but user id and password.
  3. Datatype for the credential property is "SQLCredentialClass".
  4. Database: Used to set/get the required database name to which connection is to be established.
    • The supported SQL client server provider is "SQLServerdb".
  5. DataSource: Used to get/set the required server or server instance name to which connection is to be established.
  6. Packet Size: Used to get/set the size of the packet i.e the size of the data transferred to the server.
  7. Server Version: Used to get the version of the server to which connection is established.
  8. State: Used to get the current state of the connection object. All the connection states are available within an enumeration connection state.
  9. Work Station Id: The property will get the id of the client machine to which connection is established.

Method with the Connection String

shape Description

  • Close(): Used to close an opened connection to the database.
  • Open():Used to establish connection to the database.

shape Syntax

The following is the syntax to connect to Sql Server using SqlClient Provider. [csharp]("Server=Servername;Userid=username;Password=password;database=Dbname")[/csharp]

shape Examples

[csharp]("Server=iToolsInfo-pc;Userid=sa;Password=123;database=Employee")[/csharp]
Name Example
SqlServerType database engine
Servername iToolsInfo-pc
Authentication SqlServer Authentication/Windows Authentication
login/userid sa
password 123
database Employee

Steps to work with the Connection object.

 

shape Description

Follow the below steps to work with the connection object. Step 1: Include the following namespaces.
  • System.Data;
  • System.Data.SqlClient;
Step 2: Declare the connection string.

shape Syntax

[csharp]ClassName ObjectName;[/csharp]

shape Examples

[csharp]SqlConnection con;[/csharp] Step 3: Define the Connection Object

shape Syntax

[csharp]ObjectName = new ClassName(string ConnectionString)[/csharp]

shape Examples

[csharp]con = new SqlCOnnection("Server=iToolsInfo-pc;userid=sa;password=123;database=Employee")[/csharp] Step 4: Establish the Connection.

shape Syntax

[csharp]Objectname.Open();[/csharp]

shape Examples

[csharp]con.Open();[/csharp]