2013-05-13 80 views
0

一个DataTable我有返回一个DataTable这样的功能:我怎么能读的JavaScript

public DataTable SendOnlineContacts() 
{ 
... 
    for (int i = 0; i < FriendsDt.Rows.Count; i++) 
     { 
      int FriendID = Convert.ToInt16(FriendsDt.Rows[i][0]); 
       DataRow[] FriendisOnlineRow = ConnectedClientDt.Select("ClientID=" + FriendID); 

       if (FriendisOnlineRow.Length > 0) // friend is online 
       { 
        // new SQLHelper(SQLHelper.ConnectionStrings.WebSiteConnectionString).Update("Update clients set USER_STATUS='O' where CLIENT_ID=" + FriendsDt.Rows[i][0]); 
        FriendsInfo.Rows.Add(FriendsDt.Rows[i][0] + "," + FriendsDt.Rows[i][1] + "," + FriendsDt.Rows[i][2] + "," + "O"); 
        } 
      } 
      return FriendsInfo; 
    } 

客户端:

$.ajax({ 
    type: 'POST', 
    url: 'ChatPageTest.aspx/SendOnlineContacts', 
    data: '{}', 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'json', 
    success: function (data) { 
    // what to do here to read the DataTable ?? 
    } 
     ... 

请帮助和感谢ü

+0

你必须把它转换为JSON。 – Tigran 2013-05-13 09:41:23

+0

可以提供一个例子请 – Sora 2013-05-13 09:42:15

+0

嗯,我怀疑它的工作原理..你在哪里创建数据行的JSON对象?如果是这样,那么只需在成功函数中迭代数据收集即可。 – Oscar 2013-05-13 09:42:36

回答

1

试试这个:

public object[][] SendOnlineContacts() 
{ 
    //... 
    for (int i = 0; i < FriendsDt.Rows.Count; i++) 
    { 
     int FriendID = Convert.ToInt16(FriendsDt.Rows[i][0]); 
     DataRow[] FriendisOnlineRow = ConnectedClientDt.Select("ClientID=" + FriendID); 

     if (FriendisOnlineRow.Length > 0) // friend is online 
     { 
      // new SQLHelper(SQLHelper.ConnectionStrings.WebSiteConnectionString).Update("Update clients set USER_STATUS='O' where CLIENT_ID=" + FriendsDt.Rows[i][0]); 
      FriendsInfo.Rows.Add(FriendsDt.Rows[i][0] + "," + FriendsDt.Rows[i][1] + "," + FriendsDt.Rows[i][2] + "," + "O"); 
     } 
    } 

    var rows = FriendsInfo.Rows 
     .OfType<DataRow>() 
     .Select(row => row.ItemArray) 
     .ToArray(); 
    return rows; 
}