0

我正在尝试从 wcf 服务返回数据表。我写过服务。它的代码如下

 [WebInvoke(Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "TableData")]
    public DataTable GetData(Parameters param)
    {
        try
        {


            var Id = int.Parse(param.Id);
            var tableName = param.TableName;
            var listSelectCol = new ArrayList();
            bool bFirst = true;
            var sSelectColList = new StringBuilder();
            var Query ="";
            var List = from TableEntity in _TableEntityRepository.AsQueryable()
                                  join ColumnEntity in _ColumnEntityRepository.AsQueryable() on TableEntity.Id equals ColumnEntity.Id
                                  where sysTableEntity.TableName.Equals(tableName)
                                  select ColumnEntity.Name;



            foreach (var x in List 
            {
                if (bFirst)
                    bFirst = false;
                else
                    sSelectColList.Append(", ");

                sSelectColList.Append("t." + x.ToString(CultureInfo.InvariantCulture));
                listSelectCol.Add(x.ToString(CultureInfo.InvariantCulture));


            }

                 Query =
                    "select " + sSelectColList + " ,  t.Name as MyName  from  " + tableName +
                    " t   Where t.Id = " + Id;





            var cmdQuery = new SqlCommand(Query, new SqlConnection(ConfigurationManager.AppSettings.Get("connectionString")));
            var sdaQuery = new SqlDataAdapter(cmdQuery);
            var diffTableFrom = new DiffTable();
            var colValues = new DataColumn("ColValues", typeof(System.String));

            var dt = new DataTable();
            dt.Columns.Add(colValues);

            sdaQuery.Fill(dt);

            diffTableFrom.ReturnDiffTable = dt;

            foreach (DataRow row1 in dt.Rows) 
            {
                foreach (string i in listSelectCol)
                {
                    row1["ColValues"] = row1["ColValues"] + row1[i].ToString() + ",";

                }
                row1["ColValues"] = row1["ColValues"].ToString().Remove(row1["ColValues"].ToString().Length - 1);


            }


            return dt;

        }

        catch (Exception e)
        {



            throw new WebFaultException<string>(
        string.Format(
            "{0}{1}",
            e.Message, (e.InnerException != null ? e.InnerException.Message : string.Empty)), HttpStatusCode.BadRequest);
        }


    }

DiffTable 的类是

  public class DiffTable
    {
        public DataTable ReturnDiffTable {get;set;}
    }

从我的视图模型调用的代码是

function loadData(id, tablename) {
        debugger;

        var input =
       {

           TableName: tableName,
           Id: id

       };


        return $.ajax({
            url: "../Service/TableData",
            type: "PUT",
            contentType: 'application/json',
            processData: false,
            data: JSON.stringify(input),
            error: function (XMLHttpRequest, textStatus, errorThrown) {

                alert(errorThrown);
            },
           success: function (allData) {
                var mappedData = $.map(allData, function (item) {
                    return new TableChangesDetail(item);

                });

                self.TableChangesDetailList(mappedData);


            }

        });

    }

当我尝试使用 ajax 从我的视图模型中调用此服务时,我在提琴手中遇到错误[Fiddler] ReadResponse() failed: The server did not return a response for this request.

如何从 wcf 服务返回数据表?当我调试服务并在 return dt 上放置断点时;然后我得到 1 行。所以数据是从数据库返回的,而不是返回给 viewmodel。

我在这里做错了什么?

更新1

在我更改了写在我的服务中的这一行之后

var dt = new DataTable(); 

var dt = new DataTable("Test");

我现在没有收到任何错误。但是提琴手没有以 json 格式接收数据。当我打开提琴手并可以在 TextView 选项卡下以某种奇怪的格式查看数据时。这就是我没有进入淘汰赛模型的原因。我现在该如何解决这个问题?

4

1 回答 1

0

我设法找到了另一种方法。我不确定这是否聪明。我所做的是将该数据表转换为列表并返回列表。所以我在我的服务中添加了以下代码

foreach (DataRow row1 in dt.Rows) 
            {
                var list = new LookUpTable();
                list.Name = (string) row1["Name"];
                list.Id = row1["Id"];
                myReturnList.Add(list);


            }


return myReturnList.ToList();
于 2013-07-22T15:36:09.593 回答