Confused with ExecuteScalar method of SQLCommand

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I have one stored procedure in SQL server in which i have written one
insert statement. Now in my cs file i pass the parameters require to execute
that stored procedure and finaly by mistaken I used command.ExecuteScalar
instead of command.ExecuteNonQuery. Surprisingly i am able to insert the data
in table. Can anyone please tell me how i am able to insert data using
ExecuteScalar of SQLCommand class.

Help is really appriciated.

Thx in advance
 
Hi there,

ExecuteScalar return the first row's first column. The rest is ignored.
ExecuteNonQuery returns the number of records afected by your query.

You can insert the data because it is in a stored procedure, and I haven't
tested it, but maybe you could ExecuteScalar with an insert statement as
well with a select statement..

Best regards

Steven Lewis
 
Dinesh said:
Surprisingly i am able to insert the data in table. Can anyone please
tell me how i am able to insert data using ExecuteScalar of
SQLCommand class.

ExecuteScalar() can execute anything. Should your query return any rows of
data, it will return the value in the first column of the first returned
row; should it not, it will return null.

This should not be surprising if you consider the fact SELECT isn't
guaranteed to return data either -- it is quite normal for a query to match
no rows.
 
Back
Top