WCF Rest Service将Json对象列表作为参数

时间:2017-10-04 18:42:28

标签: c# json rest wcf oracle12c

我正在尝试使用WCF Rest服务将假期列表插入到我的Oracle 12c数据库中的假期表中。当我运行我的服务时,我没有收到错误,但是在运行服务时没有插入数据。 我相信我的cmdStr和UpdateHoliday()存在问题。任何帮助都很受欢迎。感谢。

IService1.cs

public interface IService1
{
    [OperationContract()]
    [WebInvoke(UriTemplate = "UpdateHoliday", ResponseFormat = WebMessageFormat.Json, Method = "POST")]
    void UpdateHoliday(List<Holiday> Holidays);
}


[DataContract]
public class Holiday
{
    [DataMember(Order = 0)]
    public string HOLIDAY { get; set; }

    [DataMember(Order = 1)]
    public string DESCRIPTION { get; set; }

    public List<Holiday> Holidays { get; set; }
}

public class ListofHoliday
{
    [DataMember]
    List<Holiday> Holidays { get; set; }
}

Service1.cs

    public class Service1 : IService1
{
    public void UpdateHoliday(List<Holiday> Holidays)
    {
        List<ListofHoliday> firstStringList = new List<ListofHoliday>();
        string cmdStr = String.Format("INSERT INTO HOLIDAY (HOLIDAY, Description)" +
                                   " VALUES('HOLIDAY','Description')");

        foreach (var item in firstStringList)
        {
            (new DbHelper()).SqlExecute(cmdStr);
        }
    }


}

DbHelper.cs

 class DbHelper
    {
        private static string ConnectionString
        {
            get
            {
                return System.Configuration.ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
            }
        }

        public DataTable GetResultSet(string sql)
        {
            DataTable dt = new DataTable();
            using (OracleDataAdapter da = new OracleDataAdapter(sql, ConnectionString))
            {
                da.Fill(dt);
            }
            return dt;
        }

        public void SqlExecute(string sql)
        {
            using (OracleCommand cmd = new OracleCommand(sql, new OracleConnection(ConnectionString)))
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
        }

    }

我终于找到了让它发挥作用的方法。更改如下: Service1.cs

public class Service1 : IService1
{
    public void UpdateHoliday(List<Holiday> Holidays)
    {
        if (Holidays == null)
            throw new ArgumentNullException("Holidays");

        foreach (var item in Holidays)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("INSERT INTO HOLIDAY (HOLIDAY, DESCRIPTION) VALUES ");
            sb.AppendFormat("( '{0}', '{1}')",
                item.HOLIDAY, item.DESCRIPTION);
            //sb.AppendFormat(";");
            (new DbHelper()).SqlExecute(sb.ToString());
        }
    }


}

1 个答案:

答案 0 :(得分:1)

如何在服务操作中尝试此代码。如果下面的代码有效,那么你仍然需要做一些工作才能让你的DBHelper接受带参数的命令。

public class Service1 : IService1
{
    public void UpdateHoliday(List<Holiday> Holidays)
    {
        if(Holidays==null)
           throw new ArgumentNullException("Holidays");

        string cmdStr = String.Format("INSERT INTO HOLIDAY (HOLIDAY, Description)" +
                                   " VALUES('HOLIDAY','Description')");

        foreach (var item in Holidays)
        {
            (new DbHelper()).SqlExecute(cmdStr);
        }
    }


}