Home | Contact Us

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

ADO.Net Command

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

<< Back to ADO

ADO.NET SqlCommand Class

This class is used to store and execute SQL statement for SQL Server database. It is a sealed class so that cannot be inherited.

SqlCommand Signature

public sealed class SqlCommand : System.Data.Common.DbCommand, ICloneable, IDisposable


This class provides the following constructors.

Constructor Description
SqlCommand() It is used to initialize a new instance of the SqlCommand class.
SqlCommand(String) It is used to initialize a new instance of the SqlCommand class with a string parameter.
SqlCommand(String, SqlConnection) It is used to initialize a new instance of the SqlCommand class. It takes two parameters, first is query string and second is connection string.
SqlCommand(String, SqlConnection, SqlTransaction) It is used to initialize a new instance of the SqlCommand class. It takes three parameters query, connection and transaction string respectively.
SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting) It Initializes a new instance of the SqlCommand class with specified command text, connection, transaction, and encryption setting.


Method Description
BeginExecuteNonQuery() It is used to Initiate the asynchronous execution of the SQL statement described by this SqlCommand.
Cancel() It tries to cancel the execution of a SqlCommand.
Clone() It creates a new SqlCommand object that is a copy of the current instance.
CreateParameter() It creates a new instance of a SqlParameter object.
ExecuteReader() It is used to send the CommandText to the Connection and builds a SqlDataReader.
ExecuteXmlReader() It is used to send the CommandText to the Connection and builds an XmlReader object.
ExecuteScalar() It executes the query and returns the first column of the first row in the result set. Additional columns or rows are ignored.
Prepare() It is used to create a prepared version of the command by using the instance of SQL Server.
ResetCommandTimeout() It is used to reset the CommandTimeout property to its default value.


In this example, we are creating a SqlCommand instance and executing a SQL statement.

// Program.cs

using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication
    class Program
        static void Main(string[] args)
            new Program().CreateTable();
        public void CreateTable()
            SqlConnection con = null;
                // Creating Connection
                con = new SqlConnection("data source=.; database=student; integrated security=SSPI");
                // writing sql query
                SqlCommand cm = new SqlCommand("select * from student", con);
                // Opening Connection
                // Executing the SQL query
                SqlDataReader sdr = cm.ExecuteReader();
                while (sdr.Read())
                    Console.WriteLine(sdr["name"]+" "+ sdr["email"]);
            catch (Exception e)
                Console.WriteLine("OOPs, something went wrong." + e);
            // Closing the connection


Execute this program by combination of Ctrl+F5 and it will produce the following output.

ADO Net Sqlcommand Class 1

It prints name and email of the student.

Next TopicADO.NET DataReader

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