简体   繁体   English

在WCF服务中运行Oracle查询

[英]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. 我正在尝试直接从WCF服务在Oracle数据库上运行查询并返回结果,但是似乎在winform程序中查询并返回结果的代码对于wcf服务的作用不同。 I'm getting several errors in the code from the conn.Open and cmd. 我从conn.Open和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. 要创建wcf服务,请首先定义一个合同(带有所需的所有操作签名),然后使用您的实现来实施该合同。

For more details refer to Walkthrough: Creating and Accessing WCF Services 有关更多详细信息,请参阅演练:创建和访问WCF服务。

Interface (IMyService.cs): 接口(IMyService.cs):

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

Interface Implementation (MyService.cs): 界面实现(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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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