我试图直接从我的WCF服务在Oracle数据库上运行查询并返回结果,但似乎在winform程序中查询并返回结果的代码对于wcf服务。 我在conn.Open和cmd的代码中遇到了几个错误。说他们是一个领域但是像一个类型一样使用 ...我想我可能把我的代码放在了错误的地方或容器中,所以我尝试将它包装在一个类中并且没有&似乎也可以工作。出了什么问题?
[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();
}
答案 0 :(得分:0)
我相信,以下是您正在寻找的。 要创建wcf服务,首先要定义一个合同(包含您需要的所有操作签名),然后与您的实现实现合同。
有关详细信息,请参阅Walkthrough: Creating and Accessing WCF Services
接口(IMyService.cs):
namespace WcfServiceLibrary
{
[ServiceContract]
public interface IMyService
{
[OperationContract]
List<string> GetMeterBlinkData();
}
}
接口实现(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;
}
}
}