Insert a row to the Informix database using .NET

The example in this article shows how to insert a row to the specified informix table by running an informix INSERT statement, how to use IfxCommand.ExecuteNonQuery to perform an insert and also how to execute an IfxCommand inside a local transaction.


IfxCommand.ExecuteNonQuery executes an SQL statement.
For UPDATE, INSERT, and DELETE statements the return value is the number of rows affected.
For all other statements, it is -1.


IfxTransaction


The following example shows how to perform an insert within a local transaction.
The command cmd.Transaction = trans; assigns the active transaction to the Transaction property of the IfxCommand object.


using IBM.Data.Informix;

namespace Portal
{
	public static class Class1
	{

		public static void Insert( string query )
		{
			try
			{
				IfxConnection conn = new IfxConnection();
				conn.ConnectionString = "Host=myhost;Service=1541;Server=myifxserver;Database=stores_demo;User ID=mylogin;password=mypassword";
				conn.Open();

				try
				{
				
					IfxTransaction trans = conn.BeginTransaction();

					IfxCommand cmd = new IfxCommand();
					cmd.Transaction = trans;
					cmd.CommandText = "INSERT INTO customer (FirstName, LastName, City, Country, Phone) Values(\"Tom\", \"Anderson\", \"London\", \"England\", \"+ 00 123 4567\")";
					cmd.ExecuteNonQuery();

					trans.Commit();
					trans.Dispose();

					cmd.Dispose();

				}
				catch{}
				
				conn.Close();
				conn.Dispose();
			}
			catch(IfxException e)
			{
				//e.M
			}
		}

	}
}