0% found this document useful (0 votes)
78 views23 pages

SqlConnection Class

The SqlConnection class in ADO.Net is used to establish a connection to a SQL Server database. It represents a session to the database and acts as the entry point. A connection object is created by providing a connection string with details like data source, database name, authentication etc. The connection must be opened to perform operations and closed afterwards to release resources. Using blocks can be used to ensure automatic closing of connections.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views23 pages

SqlConnection Class

The SqlConnection class in ADO.Net is used to establish a connection to a SQL Server database. It represents a session to the database and acts as the entry point. A connection object is created by providing a connection string with details like data source, database name, authentication etc. The connection must be opened to perform operations and closed afterwards to release resources. Using blocks can be used to ensure automatic closing of connections.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

PART-2

SqlConnection Class
in ADO.Net
PRESENTER: MOHAMMAD ADIL
Open Close

SqlConnection

Connection
.Net Application SQL Server
Database
Insert
SQLConnection Class of ADO.Net
• It is used to establish an open connection to the SQL Server
database.
• A SqlConnection object represents a unique session to a SQL Server
data source.
• ADO.NET connection is an object that provides database
connectivity and the entry point to a database.
• It is a sealed class so that cannot be inherited.
SQLConnection Class of ADO.Net
• Definition
• Namespace:
• System.Data.SqlClient
• Assembly:
• System.Data.SqlClient.dll
SQLConnection Class of ADO.Net
• public sealed class SqlConnection : System.Data.Common.DbConnection,
ICloneable
• Inheritance: Object -> DbConnection -> SqlConnection
• Implements: ICloneable
Creating Object Of SQLConnection Class of ADO.Net
• SqlConnection con = new SqlConnection(cs);
• cs is a string variable which holds connection string.
• The connection string that includes the source database name, and
other parameters needed to establish the initial connection.
• The default value is an empty string.
Things in Connection String
• Connection String has:
• 1. Data Source (Database Server Name)
• 2. Initial Catalog (Database Name)
• 3. Integrated Security = True Windows Authentication
• 4. Username
SQL Server Authentication
• 5. Password
Things in Connection String
• Data Source: This identifies the Server name, which could be the local
machine, machine domain name or IP address
• Initial Catalog: This identifies the database name.
• Integrated Security: When you have started database authentication login
with Windows authentication, Integrated Security specifies Integrated
Security=”True” in connection string, else when you have started the
database authentication login with Server authentication Integrated
Security specifies Integrated Security=”false” in the connection string
• User Id: Name of the user configured in SQL Server.
• Password: Password matching SQL Server User ID.
Creating Object Of SQLConnection Class of ADO.Net
• The following example illustrates a typical connection string.
• "Data Source=MyServerName;Initial Catalog=MyDatabaseName;Integrated
Security=true;“
• Connecting to SQL Server using SQL authentication
• "Data Source=MyServerName;Initial Catalog=MyDatabaseName;User
Id=MyUserName;Password=MyPassword;"
Creating Object Of SQLConnection Class of ADO.Net
• String cs = “Data Source=MyServerName;Initial
Catalog=MyDatabaseName;Integrated Security=true;”;
• SqlConnection con = new SqlConnection(cs);
SQLConnection Class of ADO.Net
• When we have to perform an operation on database, we must have to
Open and close the connection.
• Example:
• SqlConnection con = new SqlConnection(cs);
• Con.Open();
• ….Execute Database Queries Here…
• Con.Close();
• At this stage we have to use State Property Of Connection Class.
Voting Result
SQLConnection Class of ADO.Net
• To ensure that connections are always closed, open the connection
inside of a using block, as shown in the following code fragment.
• Doing so ensures that the connection is automatically closed when
the code exits the block.
• using (SqlConnection con = new SqlConnection(connectionString))
• {
• con.Open();
• // Do work here; connection closed on following line.
• }
Interview Questions Related to Using Keyword
• Tell the 2 usage of using keyword ?
• 1. Using is used for adding namespace in a file.
• Example: Using System.Data.SqlClient
• 2. Using is used for automatically closing of the Connection.
• 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.
Constructors Of SQLConnection Class of ADO.Net
• SqlConnection()
• Initializes a new instance of the SqlConnection class.
• SqlConnection(String)
• Initializes a new instance of the SqlConnection class when given a string that
contains the connection string.
• SqlConnection(String, SqlCredential)
• Initializes a new instance of the SqlConnection class given a connection
string, that does not use Integrated Security = true and a SqlCredential
object that contains the user ID and password.
SQLConnection Class of ADO.Net
• SqlConnection class uses SqlDataAdapter and SqlCommand classes
together to increase performance when connecting to a Microsoft
SQL Server database.
• Connection does not close implicitly even it goes out of scope.
Therefore, you must explicitly close the connection by calling
Close() method.
SQLConnection Class of ADO.Net
• When the connection of an object is instantiated, the constructor
takes a connection string that contains the information about the
database server, server type, database name, connection type, and
database user credentials.
• Once the connection string is passed and the connection object is
created, you can establish a connection with the database.
• A connection string is usually stored in the web.config file or
app.config file of an application.
Connection String in Configuration file
• Now, we have to declare a connection string, which is usually
defined in the App.Config or Web.Config file, so its availalbe in your
application. The typical entry of a connection string is written
below:
• <connectionStrings>
• <add name="" connectionString="" providerName=""/>
• </connectionStrings>
Connection String in Configuration file
• Establish connection string in Web Config file using the below code:
• <connectionStrings>
• <add name=“dbcs" connectionString="Data Source= AdilSERVER;Initial
Catalog= EmployeeDataBase;User ID=sa,pwd=sa123"
providerName="System.Data.SqlClient"/>
• </connectionStrings>
ConnectionString Property
• SqlConnection.ConnectionString Property
• SqlConnection.ConnectionString Property :Gets or sets the string
used to open a SQL Server database.
What namespace or provider is used for connection
class?
• ADO.NET provides connection to multiple providers. Each provider
has a functionality to connect with different database. Here is a list
of data providers in ADO.NET and their purpose.
• Data Provider for SQL Server (System.Data.SqlClient).
• Data Provider for MS ACCESS (System.Data.OleDb).
• Data Provider for MYSQL (System.Data.Odbc).
• Data Provider for ORACLE (System.Data.OracleClient).
How to use connection class with this provider is given
below-
• Connection object for SQL Server (SqlConnection).
• Connection object for MSACCESS (OleDbConnection).
• Connection object for MYSQL (OdbcConnection).
• Connection object for ORACLE (OracleConnection).
Connection to an ADO.NET Database
• Before working with the database, you must import a data provider
namespace, by placing the following in the beginning your code
module.
• For SqlClient .NET data provider namespace import code:
• Using System.Data.SqlClient

You might also like