C# 在WCF服务中运行Oracle查询
我试图直接从我的WCF服务在Oracle数据库上运行查询并返回结果,但在winform程序中查询并返回结果的代码似乎与WCF服务不一样。我从conn.Open和cmd的代码中得到了几个错误。说它们是一个字段,但像一个类型一样使用。。。我想我可能把代码放错了地方或容器,所以我试着把它包装在一个类中,但这似乎也不起作用。出什么事了C# 在WCF服务中运行Oracle查询,c#,oracle,wcf,C#,Oracle,Wcf,我试图直接从我的WCF服务在Oracle数据库上运行查询并返回结果,但在winform程序中查询并返回结果的代码似乎与WCF服务不一样。我从conn.Open和cmd的代码中得到了几个错误。说它们是一个字段,但像一个类型一样使用。。。我想我可能把代码放错了地方或容器,所以我试着把它包装在一个类中,但这似乎也不起作用。出什么事了 [OperationContract] CompositeType GetDataUsingDataContract(CompositeType comp
[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();
}
我相信,以下就是你要找的。 要创建wcf服务,首先定义一个包含所有所需操作签名的协定,然后使用实现实现该协定 有关更多详细信息,请参阅 接口IMyService.cs:
namespace WcfServiceLibrary
{
[ServiceContract]
public interface IMyService
{
[OperationContract]
List<string> GetMeterBlinkData();
}
}
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;
}
}
}
接口实现MyService.cs:
namespace WcfServiceLibrary
{
[ServiceContract]
public interface IMyService
{
[OperationContract]
List<string> GetMeterBlinkData();
}
}
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;
}
}
}
提供所有的代码,而不仅仅是服务的接口,包括上面的代码是如何包装在服务的实现中的。从PHP到C,我花了更长的时间。全新的世界!