【问题标题】:Calling Web Service Function returns a string[] from Windows Phone调用 Web 服务函数从 Windows Phone 返回一个字符串 []
【发布时间】:2014-04-17 19:08:40
【问题描述】:

我有一个 Web 服务和一个返回字符串列表的方法。当我在 Windows Phone 应用程序中调用它并将其放入 ListBox 时,结果是 System.Data.SqlClient.SqlDataReader。我是网络服务的新手,我不知道问题是来自网络服务还是来自 Windors 电话。 这是我的代码:

来自网络服务的方法:

    public string [] GetActions(string cnp)
    {
        string[] lista = new string[300];
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select Denumire from Actiuni where cnp='"+cnp+"'", con);

            SqlDataReader re = cmd.ExecuteReader();
            int k = 0;
            while (re.Read())
            {
                lista[k++] = re.ToString();

            }
            re.Close();

        }
        catch (SqlException exception)
        {

        }
        finally
        {
            con.Close();
        }


        return lista;
    }

这是我的 Windows Phone 代码:

public void DesplayActions()
    {

        ServiceReference1.Service1SoapClient client = new ServiceReference1.Service1SoapClient();
        client.GetActionsCompleted += client_ValidareActiuniCompleted;
        client.GetActionsAsync(cnp);

    }



    void client_GetActionsCompleted(object sender, ServiceReference1.GetActionsCompletedEventArgs e)
    {


        for(int i=0;i<e.Result.Count;i++)
            Listbox1.Items.Add(e.Result[i]);


    }



    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        DesplayActions();

    }

【问题讨论】:

  • 请添加更多细节 - 不清楚您要解决什么问题。

标签: c# web-services windows-phone-8 sql-server-2012


【解决方案1】:
lista[k++] = re.ToString();

是你做错的地方。您已经在数据读取器上调用了 ToString。

你想这样做:

lista[k++]  = rdr.GetString(0);

0 表示结果集中的第一列,作为字符串。谷歌 SqlDataReader 类以获取更多信息,或按名称而不是索引选择列。

【讨论】:

    【解决方案2】:

    谢谢。现在在 Web 服务中工作。但不适用于 WF 应用程序。我认为 ListBox 有同样的问题,但我不知道它的语法。就像在 WFA 中一样,我声明一个 ListViewItem,然后添加子项。

    【讨论】:

      猜你喜欢
      • 2019-04-03
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 2011-07-06
      • 2011-05-29
      • 1970-01-01
      相关资源
      最近更新 更多