0% found this document useful (0 votes)
39 views16 pages

SqlCommand Class

The SqlCommand class in ADO.Net allows .NET applications to execute SQL statements against a SQL Server database. It provides constructors to initialize a command with or without a connection, and methods like ExecuteReader, ExecuteNonQuery, and ExecuteScalar to execute queries and retrieve results. ExecuteReader is used for queries returning multiple rows, ExecuteNonQuery for INSERT/UPDATE/DELETE, and ExecuteScalar for queries returning a single value. The CommandText, Connection, and CommandType properties help configure the SQL command.
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)
39 views16 pages

SqlCommand Class

The SqlCommand class in ADO.Net allows .NET applications to execute SQL statements against a SQL Server database. It provides constructors to initialize a command with or without a connection, and methods like ExecuteReader, ExecuteNonQuery, and ExecuteScalar to execute queries and retrieve results. ExecuteReader is used for queries returning multiple rows, ExecuteNonQuery for INSERT/UPDATE/DELETE, and ExecuteScalar for queries returning a single value. The CommandText, Connection, and CommandType properties help configure the SQL command.
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/ 16

PART-3

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

.Net Application Database


Read

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()

Select With Aggregate


Or Scalar Functions
.Net Application Database
Important Methods Of SqlCommand Class
• Among all specified methods, following are the most commonly used
methods of the SqlCommand class.
• ExecuteReader() - It is used when the T-SQL statement returns more than
a single value. For example, if the query returns rows of data like all
employee details.
• ExecuteNonQuery() - it is used when you want to perform an Insert,
Update or Delete operation like update or Delete an employee detail or
insert employee data.
• ExecuteScalar() - it is used when the query returns a single(scalar) value.
For example, queries that return the total number of rows in a table, or
the employee who gets highest salary etc.

You might also like