Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Here is my database connection class.I want to access 'oracleCommand' object reference from another class.

<pre>public class DBConnection
        { 
            //public OracleCommand oracleCommand;
            public string cmd = "";
            public void makeConnection()
            {
                //Opening connection
                string connectionString = "XXXX";
                OracleConnection con = new OracleConnection();
                con.ConnectionString = connectionString;
                con.Open();
                OracleCommand oracleCommand = con.CreateCommand();
                cmd = oracleCommand.CommandText;
                //Clossing resources
                con.Close();
                oracleCommand.Dispose();
            }

Then I want to execute the following query.

dbConnection.cmd = "SELECT COUNT(JOB_ID) FROM EmployeeTable WHERE STATUS='Pending'";
                OracleDataReader Reader = dbConnection.oracleCommand.ExecuteReader();
                Reader.Read();

But 'dbConnection.oracleCommand.ExecuteReader()' does not hit when debugging. Does anyone has an idea?

What I have tried:

I created '
public OracleCommand oracleCommand;
object and tried to pass.But it does not execute.
Posted
Updated 12-Mar-18 3:09am

1 solution

oracleCommand only exists inside makeConnection so only code inside that function can access it, so I don't understand how your code even compiles. If you want to make a connection available you'll need to write a function like

public static OracleCommand GetConnection()
{
    string connectionString = "XXXX";
    OracleConnection con = new OracleConnection();
    con.ConnectionString = connectionString;
    con.Open();
    OracleCommand oracleCommand = con.CreateCommand();
    return oracleCommand;
}


Something like that anyway, and you'd call it like

var c = DBConnection.GetConnection();


Note the "static" on the method declaration.

The calling code would need to handle the cleaning up of the resources which is why code like this is usually a bad idea. Just create the command in any code that needs it.
 
Share this answer
 
v2
Comments
Maciej Los 12-Mar-18 9:36am    
GetConnection method returns OracleCommand instead of OracleConnection
You've gathered speed too much ;)
F-ES Sitecore 12-Mar-18 9:57am    
Updated :o I'm sure the OP could have work that out for themselves lol
Maciej Los 12-Mar-18 10:02am    
;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900