2011-03-29 50 views
0

我目前正在研究開發一個帶有MySQL後端的Silverlight應用程序,但是我遇到了很大的問題。Silverlight和MySQL問題

下面是我的PHP代碼:

<?php 
    include("config.php"); 
    $query = "SELECT * FROM test_table"; 
    $result = mysql_query($query) or die(mysql_error()); 

    $array=array(); 

    while ($row = mysql_fetch_array($result)) 
    { 
     $array[] = array("FirstName"=>$row['firstName'], 
      "LastName"=>$row['lastName'], 
      "Age"=>$row['age']); 

    } 
    mysql_close(); 

    $returnItems = array("returnType"=>"Names", 
     "results"=>$returnItems); 

    $JSONResult = json_encode($array); 

    echo $JSONResult; 
?> 

下面是我的C#的Silverlight代碼

WebClient wc; 
     public MainPage() 
     { 
      InitializeComponent(); 
      wc = new WebClient(); 
      wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); 
     } 

     private void btnGetData_Click(object sender, RoutedEventArgs e) 
     { 


      wc.DownloadStringAsync(new Uri("/getData.php")); 

     } 

     void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
     { 
      MessageBox.Show("Downloading Data 2"); 
      try 
      { 
       JsonValue completeResult = JsonPrimitive.Parse(e.Result); 
       string resultType = completeResult["returnType"].ToString().Replace('"', ' ').Trim(); 

       JsonArray arrayJson = (JsonArray)completeResult["results"]; 
       foreach (JsonValue item in arrayJson) 
       { 
        string firstName = arrayJson["FirstName"].ToString().Replace('"', ' ').Trim(); 

        MessageBox.Show("First Name: " + firstName); 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 

    } 

當我點擊它應該就行

string resultType = completeResult["returnType"].ToString().Replace('"', ' ').Trim(); 

檢索數據的按鈕它進入捕獲說 '此方法或屬性不支持System.Json.JsonArray類型的JSON值。有些操作只能在JSON數組上執行,有些只能在JSON對象上執行。

我不知道我可以嘗試解決此問題。

感謝您提供的任何幫助。

+0

是我還是應該是'json_encode($ returnItems);'? – Wrikken 2011-03-29 00:06:51

+0

我嘗試了你的建議,但後來當我在瀏覽器中手動運行PHP腳本時,它沒有提供任何來自MySQL的數據,它只是說{「returnType」:「姓名」,「結果」:null} – Boardy 2011-03-29 00:14:17

+0

Boardy,如果你使用error_reporting開發了'E_ALL'&display_errors,你會看到'$ returnItems'沒有被定義。解決方法是:'$ returnItems = array(「returnType」=>「Names」,「results」=> $ array)'; – Wrikken 2011-03-29 00:17:09

回答

0

爲什麼不在xml模式下執行查詢?

在MSSQL選擇命令之後添加FOR XML路徑( '節點')

然後 xml.linq.xelement測試= Xml.Linq.Xelement.Parse(e.result)

TIZ