views:

113

answers:

3

Hi,

I am trying to invoke ExecuteNonQuery() method of two different objects of OracleCommand class. Both the objects use same connection object but have different commandText and parameters. I am using ODP.net, C# (.net 2.0 framework) and Oracle 10g.

Code snippet is as follows :

// OpenDatabaseConnection() methods checks and opens database connection
bool connectionOpened = OpenDatabaseConnection();
if (connectionOpened)
{
    command.ExecuteNonQuery();
    commitCommand.ExecuteNonQuery();
}

Before executing above two statements, I am checking whether connection is open or not.If its not open, then I am opening connection. My problem is, after command.ExecuteNonQuery(); gets executed, connection gets closed and I get exception of 'Connection must be open to perform this operation' when control tries to execute second statement. Why does connection gets close automatically after performing ExecuteNonQuery() method?

Can anyone please tell me how to tackle this situation? In second command object, I am just trying to commit the changes, nothing else. How to commit changes without using transactions?

Thanks in Advance

**

EDIT

** Just wanted to know, what is the best practice for opening and closing the connection? Shall we open connection at each ExecuteNonQuery(), ExecuteScalar(),etc. methods and close connectio as long as are done or open connection at application startup and keep the connection open until application ends? Please enlighten !!

A: 

Create both commands from one Connection.

command = connection.CreateCommand();
commitCommand = connection.CreateCommand();

command.Connection.Open();
command.Execute...
command.Connection.Close();

commitCommand.Connection.Open();
commitCommand.Execute...
commitCommand.Connection.Close();

I hope it was useful!

Jalal Amini
@Jalal, thanks a lot. Cant we create command objects using its new method as we create other objects? I have created command objects using statement like below :OracleCommand commitCommand = new OracleCommand();and then I use CommandText and connection properties.Is there any difference between my way and your way?
Shekhar
It makes no sense to do this. Since it has just opened a connection, the commitCommand has nothing to commit.
Winston Smith
+2  A: 

How to commit changes without using transactions?

This doesn't make any sense. If you're not explicitly using a transaction, changes are committed automatically.

Winston Smith
Oh..ok, thanks Winston.
Shekhar
+1  A: 

What is your commit command? Is that just to commit the work? If so you would not need to do so as a transaction would be implicitly created and committed on running the first query whether you like it or not.

If both queries need to be run and committed as a whole then it sounds like you want to might want to use transactions

using(var connection = new OracleConnection(connectionString))
{
    var firstCommand = new OracleCommand(firstCommandString);
    var secondCommand = new OracleCommand(secondCommandString);

    var transaction = connection.BeginTransaction("SampleTransaction");

    firstCommand.Connection = connection;
    firstCommand.Transaction = transaction;

    secondCommand.Connection = connection;
    secondCommand.Transaction = transaction;

    firstCommand.ExecuteNonQuery();
    secondCommand.ExecuteNonQuery();

    transaction.Commit();
}
Ian Johnson
@Ian, Thanks. Actually I tried to use transaction but I was getting problem in commit method. So I thought of commiting updates using another command object.
Shekhar
@Ian, if connection object is null because of some exception or something, then shouldnt we check the connection object first and then use it?
Shekhar
I do not believe that the connection object would ever be null "new OracleConnection" will guarantee to return an object or throw an exception. If an exception is raised then the rest of the code will not execute. You may want to wrap the transaction in a try-catch block and rollback the transaction if an error has occurred
Ian Johnson