Npgsql.NpgsqlCommand.ExecuteNonQuery Method
Executes a query against the database backend without expecting data back from it

Syntax

public int ExecuteNonQuery ()

Returns

A int containing the number of records affected on the database by the query

Remarks

The NpgsqlCommand.ExecuteNonQuery method is used to execute a query against the database without exepecting a result back. This is useful for running data manipulation commands, etc.

The following example demonstrates the use of the method against a database. For the example to work, a user 'joe' with the password 'secret' needs to have access to the 'table1' table in the 'joedata' database created with the SQL

CREATE TABLE table1 (int1 INTEGER, int2 INTEGER);

C# Example

/* Compiling:
 * 	mcs -r:Npgsql -r:System.Data NpgsqlExecuteNonQuery.cs
 */
using System;
using System.Data;
using Npgsql;

public class NpgsqlExecuteNonQuery
{
	public static void Main(String[] args)
	{
		NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=joe;Password=secret;Database=joedata;");
		conn.Open();
		
		NpgsqlCommand command = new NpgsqlCommand("insert into table1 values(1, 1)", conn);
		Int32 rowsaffected;
		
		try
		{
			rowsaffected = command.ExecuteNonQuery();
			Console.WriteLine("Just added {0} lines in the table table1", rowsaffected);
		}
		finally
		{
			conn.Close();
		}
	}
} 
			

Executing the query should give the output

Just added 1 lines in the table table1

Requirements

Namespace: Npgsql
Assembly: Npgsql (in Npgsql.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0