简体   繁体   English

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

[英]WCF Rest Service to Take a list of Json Objects as parameter

I am trying to insert a list of holidays into a Holiday Table inside my Oracle 12c DB using WCF Rest services. 我试图使用WCF Rest服务将假日列表插入Oracle 12c DB内的假日表中。 When I am running my service, I am not getting an error, but the data is not being inserted when I run the service. 运行服务时,没有出现错误,但是运行服务时未插入数据。 I believe there is an issue with my cmdStr and UpdateHoliday() . 我相信我的cmdStr和UpdateHoliday()有问题。 Any help is appreciated much. 非常感谢任何帮助。 Thanks. 谢谢。

IService1.cs 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 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 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();
            }
        }

    }

I finally was able to found a way to make it work. 我终于找到了使它工作的方法。 The changes are below: Service1.cs 更改如下: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());
        }
    }


}

Howa about trying out this code in your service operation. 如何在服务操作中尝试此代码。 If the code below works then you still have some work to do in order to get your DBHelper to accept a command with parameters. 如果下面的代码有效,那么您仍然需要做一些工作才能使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);
        }
    }


} 

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

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