【问题标题】:Silverlight and MySQL ProblemSilverlight 和 MySQL 问题
【发布时间】:2011-03-29 00:01:32
【问题描述】:

我目前正在考虑开发一个带有 MySQL 后端的 silverlight 应用程序,但是我遇到了很大的问题。

下面是我的 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 对象执行。

我不确定我可以尝试什么来解决这个问题。

感谢您提供的任何帮助。

【问题讨论】:

  • 是我,还是应该是json_encode($returnItems);
  • 我尝试了你的建议,但是当我在浏览器中手动运行 PHP 脚本时,它没有从 MySQL 中找到任何数据,它只是说 {"returnType":"Names","re​​sults ":null}
  • Boardy,如果您使用 error_reporting to E_ALL 和 display_errors 进行开发,您会看到 $returnItems 未定义。解决方案是:$returnItems = array("returnType"=&gt;"Names","results"=&gt;$array);
  • @Wrikken 我尝试了您建议的行,但它仍然在 Silverlight 中显示相同的错误消息
  • 好的,但是您现在实际上在 json 中有一个“returnType”条目,并带有一个填充的“结果”数组?我能为你做的就是这些了,不知道Silverlight的傻事,所以这由其他人来确定:)

标签: c# php mysql silverlight


【解决方案1】:

为什么不用xml模式执行查询?

在 MSSQL 中选择命令后添加 FOR XML PATH('Node')

那么 xml.linq.xelement 测试 = Xml.Linq.Xelement.Parse(e.result)

提兹

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多