0% found this document useful (0 votes)
68 views5 pages

Sqlcommand Signature: Constructors

The SqlCommand class is used to execute SQL statements against a SQL Server database. It cannot be inherited. The class provides constructors to initialize command instances with or without a connection and transaction. Methods like ExecuteReader, ExecuteScalar allow executing queries and returning results. An example demonstrates creating a SqlCommand instance, opening a connection, executing a query, and printing the results.

Uploaded by

manigandan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views5 pages

Sqlcommand Signature: Constructors

The SqlCommand class is used to execute SQL statements against a SQL Server database. It cannot be inherited. The class provides constructors to initialize command instances with or without a connection and transaction. Methods like ExecuteReader, ExecuteScalar allow executing queries and returning results. An example demonstrates creating a SqlCommand instance, opening a connection, executing a query, and printing the results.

Uploaded by

manigandan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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
1. public sealed class SqlCommand : System.Data.Common.DbCommand
, ICloneable, IDisposable  

Constructors
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, It is used to initialize a
SqlTransaction) new instance of the
SqlCommand class. It
takes three parameters
query, connection and
transaction string
respectively.

SqlCommand(String, SqlConnection, It Initializes a new


SqlTransaction, instance of the
SqlCommandColumnEncryptionSetting) SqlCommand class with
specified command text,
connection, transaction,
and encryption setting.

Methods

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.

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

// Program.cs

1. using System;  
2. using System.Data.SqlClient;  
3. namespace AdoNetConsoleApplication  
4. {  
5.     class Program  
6.     {  
7.         static void Main(string[] args)  
8.         {  
9.             new Program().CreateTable();  
10.         }  
11.         public void CreateTable()  
12.         {  
13.             SqlConnection con = null;  
14.             try  
15.             {  
16.                 // Creating Connection  
17.                 con = new SqlConnection("data source=.; database=stude
nt; integrated security=SSPI");  
18.                 // writing sql query  
19.                 SqlCommand cm = new SqlCommand("select * from stude
nt", con);  
20.                 // Opening Connection  
21.                 con.Open();  
22.                 // Executing the SQL query  
23.                 SqlDataReader sdr = cm.ExecuteReader();  
24.                 while (sdr.Read())  
25.                 {  
26.                     Console.WriteLine(sdr["name"]+" "+ sdr["email"]);  
27.                 }  
28.             }  
29.             catch (Exception e)  
30.             {  
31.                 Console.WriteLine("OOPs, something went wrong." + e);  
32.             }  
33.             // Closing the connection  
34.             finally  
35.             {  
36.                 con.Close();  
37.             }  
38.         }  
39.     }  
40. }  

Output:

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


following output.

It prints name and email of the student.

You might also like