Sqlconnection Signature

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

ADO.

NET SqlConnection Class


It is used to establish an open connection to the SQL Server database. It is a
sealed class so that cannot be inherited. SqlConnection class uses
SqlDataAdapter and SqlCommand classes together to increase performance
when connecting to a Microsoft SQL Server database.

Connection does not close explicitly even it goes out of scope. Therefore,
you must explicitly close the connection by calling Close() method.

SqlConnection Signature
1. public sealed class SqlConnection : System.Data.Common.DbConnecti
on, ICloneable, IDisposable  

SqlConnection Constructors

Constructors Description

SqlConnection() It is used to initializes a new instance of the SqlC

SqlConnection(String)0 It is used to initialize a new instance of the


connection string as an argument.

SqlConnection(String, It is used to initialize a new instance of the Sq


SqlCredential) parameters. First is connection string and second

SqlConnection Methods

Method Description

BeginTransaction() It is used to start a database transaction.


ChangeDatabase(String) It is used to change the current database for an

ChangePassword(String, It changes the SQL Server password for the


String) string.

Close() It is used to close the connection to the databas

CreateCommand() It enlists in the specified transaction as a distrib

GetSchema() It returns schema information for the data sour

Open() It is used to open a database connection.

ResetStatistics() It resets all values if statistics gathering is enab

SqlConnection Example
Now, let's create an example that establishes a connection to the SQL
Server. We have created a Student database and will use it to connect.
Look at the following C# code.

1. using (SqlConnection connection = new SqlConnection(connectionStrin
g))    
2. {    
3.   connection.Open();         
4. }  

Using block is used to close the connection automatically. We don't need to


call close () method explicitly, using block do this for ours implicitly when
the code exits the block.

// Program.cs

1. using System;  
2. using System.Data.SqlClient;  
3. namespace AdoNetConsoleApplication  
4. {  
5.     class Program  
6.     {  
7.         static void Main(string[] args)  
8.         {  
9.             new Program().Connecting();  
10.         }  
11.         public void Connecting()  
12.         {  
13.             using (  
14.                      // Creating Connection  
15.                      SqlConnection con = new SqlConnection("data source=
.; database=student; integrated security=SSPI")  
16.                  )  
17.             {  
18.                 con.Open();  
19.                 Console.WriteLine("Connection Established Successfully");  
20.             }  
21.         }  
22.     }  
23. }  

Output:

What, if we don't use using block.

If we don't use using block to create connection, we have to close


connection explicitly. In the following example, we are using try-block
instead of using block.
// Program.cs

1. using System;  
2. using System.Data.SqlClient;  
3. namespace AdoNetConsoleApplication  
4. {  
5.     class Program  
6.     {  
7.         static void Main(string[] args)  
8.         {  
9.             new Program().Connecting();  
10.         }  
11.         public void Connecting()  
12.         {  
13.             SqlConnection con = null;  
14.             try  
15.             {  
16.                 // Creating Connection  
17.                 con = new SqlConnection("data source=.; database=stude
nt; integrated security=SSPI");  
18.                 con.Open();  
19.                 Console.WriteLine("Connection Established Successfully");  
20.             }  
21.             catch (Exception e)  
22.             {  
23.                 Console.WriteLine("OOPs, something went wrong.\n"+e);  
24.             }  
25.             finally  
26.             {   // Closing the connection  
27.                 con.Close();  
28.             }  
29.         }  
30.     }  
31. }  

Output:

You might also like