2013-05-13 123 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; 
}