[英]Retrieving data from database in UWP
I'm trying to retrieve a list of data from SQLDatabase on Azure and bind it on a listview in my universal application for windows 10. What I've done is creating a WCF service for retrieving the data, then publish on azure, after that calling this service on my application. 我正在尝试从Azure上的SQLDatabase中检索数据列表,并将其绑定到我在Windows 10的通用应用程序中的列表视图上。我所做的是创建一个WCF服务来检索数据,然后在azure上发布,之后在我的应用程序上调用此服务。
There are errors in my current code. 我当前的代码中有错误。 This is my code for IService1.cs
这是我的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; }
}
}
Service1.svc.cs: 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;
}
}
}
I then publish it on Azure and add a service reference to the azure website on my UWP application. 然后我在Azure上发布它,并在我的UWP应用程序上添加对azure网站的服务引用。 The following codes are my XAML and cs codes.
以下代码是我的XAML和cs代码。
I'm having error at x:DataType="data:NYPUnlocking", the error saying "The name "NYPUnlocking" does not exist in the namespace "using:NYPUnlock.ServiceReference1". 我在x处有错误:DataType =“data:NYPUnlocking”,错误说“名称”NYPUnlocking“在命名空间中不存在”使用:NYPUnlock.ServiceReference1“。
<Page.Resources>
<DataTemplate x:DataType="data:NYPUnlocking" x:Key="UserTemplate">
<StackPanel Orientation="Horizontal" Padding="0,0,0,0">
<TextBlock FontSize="12" Text="{x:Bind lockerID}" HorizontalAlignment="Left" Width="200" />
<TextBlock FontSize="12" Text="{x:Bind lockStatus}" HorizontalAlignment="Right" Width="200" />
</StackPanel>
</DataTemplate>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Width="354">
<ListView x:Name="lvLocker" ItemTemplate="{StaticResource UserTemplate}"/>
<TextBox x:Name="tbError" TextWrapping="Wrap" Text="Error"/>
</StackPanel>
</Grid>
And my cs file is having error at await task1 saying "Cannot implicitly convert type 'System.Collections.ObjectModel.ObservableCollection' to 'NYPUnlock.ServiceReference1.NYPUnlocking[]' 并且我的cs文件在等待task1时出错“无法将类型'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;
}
}
}
}
I think I am doing everything correctly, what could be causing the problem? 我想我正在做的一切正确,可能导致问题的原因是什么? Any help is greatly appreciated, thanks!
非常感谢任何帮助,谢谢!
Try that: 试试看:
Add a reference called "whatevername" to the namespace WcfService2 in your XAML file and use this prefix instead of "data:" in x:DataType="data:NYPUnlocking"
将名为“whatevername”的引用添加到XAML文件中的命名空间WcfService2,并在
x:DataType="data:NYPUnlocking"
使用此前缀而不是“data:” x:DataType="data:NYPUnlocking"
Replace the declaration of returnResult with var type instead of ServiceReference1.NYPUnlocking[]
type 将returnResult的声明替换为var类型而不是
ServiceReference1.NYPUnlocking[]
类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.