Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在UWP中从数据库中检索数据_C#_Wcf_Azure_Win Universal App_Uwp - Fatal编程技术网

C# 在UWP中从数据库中检索数据

C# 在UWP中从数据库中检索数据,c#,wcf,azure,win-universal-app,uwp,C#,Wcf,Azure,Win Universal App,Uwp,我正在尝试从Azure上的SQLDatabase检索数据列表,并将其绑定到我的windows 10通用应用程序的listview中。我所做的是创建一个用于检索数据的WCF服务,然后在azure上发布,然后在我的应用程序上调用此服务 我当前的代码中有错误。 这是我的IService1.cs代码 namespace WcfService2 { // NOTE: You can use the "Rename" command on the "Refactor" menu to change

我正在尝试从Azure上的SQLDatabase检索数据列表,并将其绑定到我的windows 10通用应用程序的listview中。我所做的是创建一个用于检索数据的WCF服务,然后在azure上发布,然后在我的应用程序上调用此服务

我当前的代码中有错误。 这是我的IService1.cs代码

namespace WcfService2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        // TODO: Add your service operations here
        [OperationContract]
        List<NYPUnlocking> NYP_GetLockerStatus();
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class NYPUnlocking
    {
        [DataMember]
        public int lockerID { get; set; }
        [DataMember]
        public string lockStatus { get; set; }
    }

}
名称空间WcfService2
{
//注意:您可以使用“重构”菜单上的“重命名”命令同时更改代码和配置文件中的接口名称“IService1”。
[服务合同]
公共接口IService1
{
//TODO:在此处添加服务操作
[经营合同]
列出NYP_GetLockerStatus();
}
//如下面的示例所示,使用数据协定将复合类型添加到服务操作中。
[数据合同]
公共类NYPUnlocking
{
[数据成员]
public int lockerID{get;set;}
[数据成员]
公共字符串锁状态{get;set;}
}
}
Service1.svc.cs:

namespace WcfService2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
       public List<NYPUnlocking> NYP_GetLockerStatus()
        {
            SqlConnection conn = new SqlConnection("Copy ADO.net connection string");
            string sqlStr = "SELECT * FROM dbo.lockerStatus";
            SqlCommand cmd = new SqlCommand(sqlStr, conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            List<NYPUnlocking> ret = new List<NYPUnlocking>();
            while (dr.Read())
            {
                NYPUnlocking unlock = new NYPUnlocking()
                {
                    lockerID = Int32.Parse(dr["lockerID"].ToString()),
                    lockStatus = dr["lockStatus"].ToString()
                };

                ret.Add(unlock);
            }
            conn.Close();

            return ret;
        }
    }
}
名称空间WcfService2
{
//注意:您可以使用“重构”菜单上的“重命名”命令来同时更改代码、svc和配置文件中的类名“Service1”。
//注意:要启动WCF测试客户端以测试此服务,请在解决方案资源管理器中选择Service1.svc或Service1.svc.cs并开始调试。
公共类服务1:IService1
{
公共列表NYP_GetLockerStatus()
{
SqlConnection conn=新的SqlConnection(“复制ADO.net连接字符串”);
string sqlStr=“SELECT*FROM dbo.lockerStatus”;
SqlCommand cmd=新的SqlCommand(sqlStr,conn);
conn.Open();
SqlDataReader dr=cmd.ExecuteReader();
List ret=新列表();
while(dr.Read())
{
NYPUnlocking解锁=新NYPUnlocking()
{
lockerID=Int32.Parse(dr[“lockerID”].ToString()),
lockStatus=dr[“lockStatus”].ToString()
};
重新添加(解锁);
}
康涅狄格州关闭();
返回ret;
}
}
}
然后,我将其发布到Azure上,并在我的UWP应用程序中将服务引用添加到Azure网站。以下代码是我的XAML和cs代码

我在x:DataType=“data:NYPUnlocking”处出错,错误是“名称“NYPUnlocking”在命名空间“using:NYPUnlock.ServiceReference1”中不存在


我的cs文件在等待任务1时出错,表示“无法隐式地将类型”System.Collections.ObjectModel.ObservableCollection“转换为”NYPUnlock.ServiceReference1.NYPUnlocking[]

namespace NYPUnlock
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            getStatus();
        }
        public async void getStatus()
        {
            try
            {
                ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(ServiceReference1.Service1Client.EndpointConfiguration.BasicHttpBinding_IService1);
                var task1 = client.NYP_GetLockerStatusAsync();
                ServiceReference1.NYPUnlocking[] returnResult = await task1;
                lvLocker.ItemsSource = returnResult;
            }
            catch(Exception ex)
            {
                tbError.Text = ex.Message;
            }
        }
    }
}
名称空间NYPUnlock
{
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
getStatus();
}
公共异步void getStatus()
{
尝试
{
ServiceReference1.Service1Client client=新的ServiceReference1.Service1Client(ServiceReference1.Service1Client.EndpointConfiguration.BasicHttpBinding\u IService1);
var task1=client.NYP_GetLockerStatusAsync();
ServiceReference1.NYPUnlocking[]returnResult=等待任务1;
lvLocker.ItemsSource=返回结果;
}
捕获(例外情况除外)
{
tbError.Text=例如消息;
}
}
}
}
我认为我做的每件事都是正确的,是什么导致了问题?非常感谢您的帮助,谢谢

试试看:

  • 在XAML文件的名称空间WcfService2中添加一个名为“whatevername”的引用,并在
    x:DataType=“data:NYPUnlocking”

  • 将returnResult的声明替换为var类型,而不是
    servicerence1.NYPUnlocking[]
    type

试试看:

  • 在XAML文件的名称空间WcfService2中添加一个名为“whatevername”的引用,并在
    x:DataType=“data:NYPUnlocking”

  • 将returnResult的声明替换为var类型,而不是
    servicerence1.NYPUnlocking[]
    type


感谢您的快速回复,我不太明白您第一点的意思。将类型更改为var确实解决了问题,谢谢!谢谢你的快速回复,我不太明白你第一点的意思。将类型更改为var确实解决了问题,谢谢!
namespace NYPUnlock
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            getStatus();
        }
        public async void getStatus()
        {
            try
            {
                ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(ServiceReference1.Service1Client.EndpointConfiguration.BasicHttpBinding_IService1);
                var task1 = client.NYP_GetLockerStatusAsync();
                ServiceReference1.NYPUnlocking[] returnResult = await task1;
                lvLocker.ItemsSource = returnResult;
            }
            catch(Exception ex)
            {
                tbError.Text = ex.Message;
            }
        }
    }
}