简体   繁体   中英

Run Oracle Query in WCF Service

I'm trying to run a query on an Oracle Database directly from my WCF service and return the result but it seems that the code that would query and return a result in a winform program is not doing the same for the wcf service. I'm getting several errors in the code from the conn.Open and cmd. saying that they are a field but used like a type ... I think I've probably got my code in the wrong place or containers so I've tried wrapping it in a class and that didn't seem to work either. What is going wrong?

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    // TODO: Add your service operations here
    [OperationContract]
        string oradb = "Data Source=cecc-db1;User Id=dcsi;Password=dcsi;";
        OracleConnection conn = new OracleConnection(oradb);  // C#
        conn.Open();
        OracleCommand cmd = new OracleCommand();
        cmd.Connection = conn;
        cmd.CommandText = "select t2.meternumber, t1.blinkdate, t1.blinkcount from (select * from cecc_processed_blinks where trunc(blinkdate) between to_date('01-may-15', 'dd-mon-yy') and to_date('08-may-15', 'dd-mon-yy')) t1 left join meteraccts t2 on t1.serialnumber = t2.serialnumber order by t1.blinkdate desc";
        cmd.CommandType = CommandType.Text;
        OracleDataReader dr = cmd.ExecuteReader();
        dr.Read();
        //TODO display the results...
        conn.Dispose();
}

I believe, following is what you are looking for. To create a wcf service, first define a contract (with all operation signatures that you need) and then implement the contract with your implementations.

For more details refer to Walkthrough: Creating and Accessing WCF Services

Interface (IMyService.cs):

namespace WcfServiceLibrary
{  
    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        List<string> GetMeterBlinkData();
    }
}

Interface Implementation (MyService.cs):

namespace WcfServiceLibrary
{  
    public class MyService: IMyService
    {
        public List<string> GetMeterBlinkData()
        { 
           List<string> result = new List<string>(); 

           string oradb = "Data Source=cecc-db1;User Id=dcsi;Password=dcsi;";
           OracleConnection conn = new OracleConnection(oradb);  // C#
           conn.Open();
           OracleCommand cmd = new OracleCommand();
           cmd.Connection = conn;
           cmd.CommandText = "select t2.meternumber, t1.blinkdate, t1.blinkcount from (select * from cecc_processed_blinks where trunc(blinkdate) between to_date('01-may-15', 'dd-mon-yy') and to_date('08-may-15', 'dd-mon-yy')) t1 left join meteraccts t2 on t1.serialnumber = t2.serialnumber order by t1.blinkdate desc";
           cmd.CommandType = CommandType.Text;
           OracleDataReader dr = cmd.ExecuteReader();
           dr.Read();
            //TODO loop through results and fill the results object
           conn.Dispose();

           return result;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM