TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ADO.Net Connection

ADO Net Connection with introduction, data providers, sql server connectivity, connection, command, datareader, dataset, dataadapter, datatables, web form examples, mvc examples etc.

<< Back to ADO

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

public sealed class SqlConnection : System.Data.Common.DbConnection, ICloneable, IDisposable

SqlConnection Constructors

Constructors Description
SqlConnection() It is used to initializes a new instance of the SqlConnection class.
SqlConnection(String)0 It is used to initialize a new instance of the SqlConnection class and takes connection string as an argument.
SqlConnection(String, SqlCredential) It is used to initialize a new instance of the SqlConnection class that takes two parameters. First is connection string and second is sql credentials.

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 open SqlConnection.
ChangePassword(String, String) It changes the SQL Server password for the user indicated in the connection string.
Close() It is used to close the connection to the database.
CreateCommand() It enlists in the specified transaction as a distributed transaction.
GetSchema() It returns schema information for the data source of this SqlConnection.
Open() It is used to open a database connection.
ResetStatistics() It resets all values if statistics gathering is enabled.

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.

using (SqlConnection connection = new SqlConnection(connectionString))  
{  
  connection.Open();       
}

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

using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            new Program().Connecting();
        }
        public void Connecting()
        {
            using (
                     // Creating Connection
                     SqlConnection con = new SqlConnection("data source=.; database=student; integrated security=SSPI")
                 )
            {
                con.Open();
                Console.WriteLine("Connection Established Successfully");
            }
        }
    }
}

Output:

ADO Net SqlConnection Class 1

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

using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            new Program().Connecting();
        }
        public void Connecting()
        {
            SqlConnection con = null;
            try
            {
                // Creating Connection
                con = new SqlConnection("data source=.; database=student; integrated security=SSPI");
                con.Open();
                Console.WriteLine("Connection Established Successfully");
            }
            catch (Exception e)
            {
                Console.WriteLine("OOPs, something went wrong.\n"+e);
            }
            finally
            {   // Closing the connection
                con.Close();
            }
        }
    }
}

Output:

ADO Net Sqlconnection Class 2
Next TopicADO.NET Command




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf