Prepare
Practice
Interview
Aptitude
Reasoning
English
GD
Placement papers
HR
Current affairs
Engineering
MCA
MBA
Online test
Login
Command Object Methods - ADO.NET
Home
>>
Category
>>
ASP.NET (MCQ) questions and answers
>>
ADO.NET
Q. What are the Command Object Methods?
- Published on 28 Jul 15
a.
ExecuteNonQuery
b.
ExecuteReader
c.
ExecuteScalar
d.
All of the above
ANSWER: All of the above
Related Content
ASP.NET Introduction (
27
)
ASP.NET HTML and Web Server Control (
13
)
Validation Control and Web page navigation (
13
)
Master Pages (
14
)
ASP.NET Security (
26
)
State Management System (
22
)
ASP.NET AJAX (
18
)
ADO.NET (
52
)
Caching pages and data (
17
)
ASP.NET Exception Handling (
0
)
ASP.NET Globalization and Localization (
14
)
Basic concept of ASP.NET (
12
)
Csharp.Net (
27
)
LINQ (
26
)
Discussion
Nihal
-Posted on 19 Oct 15
Following are the methods of command object.
ExecuteNonQuery: This method is used,If you are using insert,update,delete SQL statement.Its return type is Integer.This indicates the no of effected records.
ExecuteReader: This method is used,If you are using Select SQL statement.Its return type is DataReader.
ExecuteScalar: If you need to return a single value from a sql query, you
can use the ExecuteScalar method. This method always returns the value
of the first column from the first row. All other values are ignored. If the result set is empty it will return a null. Its return type is Object.
ExecuteXMLReader: It returns XML formatted data. Returns a System.Xml.XmlReader
Object.
Example of ExecuteScalar Method
public void CalculateSalary()
{
SqlConnection conn = new SqlConnection();
SqlCommand cmd;
conn.ConnectionString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
try
{
cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT SUM(SAL) SAL FROM EMPLOYEE";
cmd.CommandType = CommandType.Text;
conn.Open();
int TotalSalary = Convert.ToInt32(cmd.ExecuteScalar());
if(TotalSalary>0)
{
Label1.Text= "Total Salary is : " + TotalSalary.ToString());
}
cmd.Dispose();
conn.Dispose();
}
catch (Exception ex)
{
Label1.Text =ex.Message;
}
}
In the above example connection string is configured a for a Data Source in the App.config file
➨
Post your comment / Share knowledge
Required!
Required!
Invalid Email Id!
Required!
Enter the code shown above:
Please enter the code shown above
(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)
MCQs
English
Tutorials
Download
▲