Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

What will be the output. Either 0 or 1
when i executed cmd.ExecuteScalar()
Posted

Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.

See : MSDN[^]
 
Share this answer
 
Comments
CPallini 19-Jan-14 11:51am    
5. You beat me.
Krunal Rohit 19-Jan-14 11:52am    
Just timings :)
The documentation[^] states it clearly:
Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
 
Share this answer
 
Okay, just go like this :
C#
var commandStr= "If not exists (select name from sysobjects where name = 'Customer') CREATE TABLE Customer(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date datetime)";

using (SqlCommand command = new SqlCommand(commandStr, con))
command.ExecuteNonQuery();
 
Share this answer
 
Use ExecuteNonQuery if no return value is required. Use ExecuteScalar to return just one value and capture it ( like a COUNT or a SUM )
 
Share this answer
 
ExecuteScalar - Executes a command that returns a single value.
ExecuteReader - Executes a command that returns a set of rows.
ExecuteNonQuery - Executes a command that updates the datebase or change the database structure. This method returns the number of rows affected.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900