Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to connect to oracle database in windows service?
I want to access say temp database and emp table from it.
username=xyz
pwd=abcd

suggest the solution with select query.
Posted

1 solution

Hello,

Download & Install Oracle Data Provider for Net[^]. Use code similar to one shown below to connect to Oracle & Retrieve the data.
C#
string strPass;
string strCon = "USER_NAME/PASSWORD@//DB_SERVER_HOST_OR_IP:PORT/SERVICE_NAME";
using (OracleConnection conn = new OracleConnection(strCon))
{
    conn.Open();
    using (OracleCommand cmd = new OracleCommand())
    {
        cmd.Connection = conn;
        cmd.CommandText = "SELECT * FROM emp_table WHERE username = :userId";
        cmd.CommandType = CommandType.Text;
        OracleParameter pUser = new OracleParameter();
        pUser.OracleDbType = OracleDbType.Varchar2;
        pUser.Value = "xyz";
        cmd.Parameters.Add(pUser);
        using (OracleDataReader dr = cmd.ExecuteReader())
        {
            dr.Read();
            //strPass = dr["pwd"].ToString();
            strPass = dr.GetOracleString(1)); // Assuming that the pwd is a second column.
        }
    }
}

Regards,
 
Share this answer
 

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