SqlCommand Class
SqlCommand Class
SqlCommand Class
in ADO.Net
PRESENTER: MOHAMMAD ADIL
SqlCommand SqlClient ADO.Net
Operations
Insert
.Ne Da
tA ta
pp Update
b
lica as
tio e
n
Delete
SqlCommand Class in ADO.Net
• The ADO.NET SqlCommand class in C# is used to store and execute
the SQL statement against the SQL Server database.
• SqlCommand class is used to prepare an SQL statement
or StoredProcedure that we want to execute on a SQL Server
database.
SqlCommand Signature
• It is a sealed class means it cannot be inherited.
• public sealed class SqlCommand : System.Data.Common.DbCommand,
ICloneable, IDisposable
ADO.NET SqlCommand Class
• If a .NET application (Web, Desktop, Console etc.) has to retrieve
data, then the application needs to
• 1. Connect to the Database
• 2. Prepare an SQL Command
SqlCommand
• 3. Execute the Command
• 4. Retrieve the results and display in the application
• This is the normal flow which is used by any .Net application, not
only to retrieve data but also to insert, update or Delete data.
SqlCommand Class in ADO.Net
• The SqlCommand class in C# provides the following five Overloaded
constructors.
SqlCommand Class in ADO.Net
• SqlCommand()
• This constructor is used to initializes a new instance of the
System.Data.SqlClient.SqlCommand class.
• SqlCommand(string cmdText)
• It is used to initializes a new instance of the
System.Data.SqlClient.SqlCommand class with the text of the query.
Here, the cmdText is the text of the query that we want to execute.
SqlCommand Class in ADO.Net
• SqlCommand(string cmdText, SqlConnection connection):
• It is used to initializes a new instance of the
System.Data.SqlClient.SqlCommand class with the text of the query
and a System.Data.SqlClient.SqlConnection.
• Here, the cmdText is the text of the query that we want to execute
and the parameter connection is the connection to an instance of
SQL Server.
SqlCommand Class in ADO.Net
• SqlCommand(string cmdText, SqlConnection connection,
SqlTransaction transaction):
• It is used to initializes a new instance of the
System.Data.SqlClient.SqlCommand class with the text of the query,
a SqlConnection instance, and the SqlTransaction instance.
• Here, the parameter cmdText is the text of the query.
• The parameter connection is a SqlConnection that represents the
connection to an instance of SQL Server and the parameter
transaction is the SqlTransaction in which the SqlCommand
executes.
SqlCommand Class in ADO.Net
• SqlCommand(string cmdText, SqlConnection connection,
SqlTransaction transaction, SqlCommandColumnEncryptionSetting
columnEncryptionSetting):
• It is used to initializes a new instance of the
System.Data.SqlClient.SqlCommand class with specified command
text, connection, transaction, and encryption setting.
• We already discussed the first three parameters which are the same
as the previous. Here, the fourth parameter i.e.
columnEncryptionSetting is the encryption setting.
Important Methods Of SqlCommand Class
• Among all specified methods, following are the most commonly
used methods of the SqlCommand class.
• ExecuteReader()
• ExecuteNonQuery()
• ExecuteScalar()
Important Methods Of SqlCommand Class
• ExecuteReader()
Select
SqlDataReader
SqlCommand Class in ADO.Net
• 3 Properties
• CommandText
• Connection
• CommandType
Important Methods Of SqlCommand Class
• ExecuteNonQuery()
Insert
Update
.Net Application Database
Delete
Important Methods Of SqlCommand Class
• ExecuteScalar() Count(), Min(),
Max(), Avg(), Sum()