Questions tagged [executenonquery]

executes an SQL statement against the Connection object of a .NET Framework data provider, and returns the number of rows affected.

From MSDN: You can use the ExecuteNonQuery to perform catalog operations (for example, querying the structure of a database or creating database objects such as tables), or to change the data in a database without using a DataSet by executing UPDATE, INSERT, or DELETE statements.

Although the ExecuteNonQuery does not return any rows, any output parameters or return values mapped to parameters are populated with data.

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1.

243 questions
64
votes
6 answers

Get affected rows on ExecuteNonQuery

I am currently working on a C# project and I am running an insert query which also does a select at the same time, e.g.: INSERT INTO table (SELECT * FROM table WHERE column=date) Is there a way I can see how many rows were inserted during this…
Boardy
  • 35,417
  • 104
  • 256
  • 447
28
votes
1 answer

ExecuteScalar vs ExecuteNonQuery when returning an identity value

Trying to figure out if it's best to use ExecuteScalar or ExecuteNonQuery if I want to return the identity column of a newly inserted row. I have read this question and I understand the differences there, but when looking over some code I wrote a…
techturtle
  • 2,519
  • 5
  • 28
  • 54
23
votes
2 answers

ExecuteNonQuery() returns -1 always

I am using a stored procedure to insert some value in table. CREATE PROCEDURE [dbo].[Sp_InsertValue] @Val1 as nvarchar(50) @Val2 as nvarchar(50) as BEGIN IF NOT EXISTS(SELECT * FROM @mytable WHERE ID=@Val1) INSERT INTO @mytable…
Lali
  • 2,816
  • 4
  • 30
  • 47
21
votes
3 answers

Why the "Non" in "ExecuteNonQuery"?

I know this is not a hell of an useful question but I can't help being bugged by it. So, Why said method (in *Command classes) is called ExecuteNonQuery instead of ExecuteQuery? Aren't those SQL statements we throw at DBs, queries?
Camilo Martin
  • 37,236
  • 20
  • 111
  • 154
20
votes
5 answers

Inserting values into a SQL Server database using ado.net via C#

I have created a simple program to insert values into the table [regist], but I keep getting the error Incorrect syntax near ')' on cmd.ExecuteNonQuery();: private void button1_Click(object sender, EventArgs e) { SqlConnection cn = new…
abhinayr
  • 221
  • 1
  • 2
  • 8
17
votes
4 answers

c#: ExecuteNonQuery() returns -1

Ive used this method before to return the amount of rows changed. I am it to run an insert method, the insert runs fine in the stored procedure, but the return value from the ExecuteNonQuery always returns -1. Here is my C# code: int ret = 0; using…
Funky
  • 12,890
  • 35
  • 106
  • 161
8
votes
3 answers

Update statement with Entity Framework

Simple question, is it possible to achieve this query this with Entity Framework when updating one entity? update test set value = value + 1 where id = 10
Drevak
  • 867
  • 3
  • 12
  • 26
8
votes
2 answers

How to get number of rows affected by ExecuteNonQuery and ignore rows of triggers?

I am using ExecuteNonQuery to run an insert proc, it returns 2, but in actual I am inserting only 1 record. I am getting 1 extra due to trigger. Is there anyway that I get only actual number of rows affected. I do not want the rows affected by…
Girish Gupta
  • 1,241
  • 13
  • 27
6
votes
2 answers

Cannot detect SQL error when using ExecuteNonQuery()

I have the following bit of code which runs a SQL statement: int rowsEffected = 0; using (SqlConnection dbConnection = new SqlConnection(dbConnectionString)) { try { dbConnection.InfoMessage += new…
millie
  • 2,642
  • 10
  • 39
  • 58
5
votes
1 answer

The Transaction property of the command has not been initialized

I am trying to run ExecuteNonQuery using transaction, but I keep getting this error OleDbException : ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The…
Amit Kumar
  • 591
  • 2
  • 8
  • 24
5
votes
2 answers

Row count of a stored procedure from another stored procedure

I have various stored procedures. I need a stored procedure to execute a stored procedure and then return only the row count (number of returned rows by the called procedure) and I need to receive it in c# code. What's the best way to do this?
user1711245
4
votes
1 answer

ExecuteNonQuery returning a value of 2 when only 1 record was updated

Running thru examples of Enterprise Library 5.0 and when I use ExecuteNonQuery to run an update sproc, it returns 2. The update is based on ProductID, the table's Primary Key (yes, I checked, and it is unique). Here is the simple sproc: ALTER…
JM.
  • 678
  • 12
  • 23
4
votes
5 answers

VB.NET SQL Server Insert - ExecuteNonQuery: Connection property has not been initialized

In the form load event, I connect to the SQL Server database: Private Sub AddBook_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load myConnection = New…
Tepken Vannkorn
  • 211
  • 3
  • 4
  • 7
4
votes
4 answers

OracleCommand command, ExecuteNonQuery issue

I have to clear certain tables in the oracle database however when I'm having issues with running the following code public static void ClearDataTables(IList tableNames) { string connectionString = "CONNECTIONSTRING"; …
ChickSentMeHighE
  • 1,706
  • 6
  • 21
  • 30
4
votes
1 answer

C# & SQL Server : ExecuteNonQuery returning -1, when UPDATE works?

Apologies if this has been asked before. I have searched for an hour and not found the exact problem I am having. I am using SMO to run some queries against SQL Server, because I have read this can handle GO statements, as opposed to…
1
2 3
16 17