使用WCF将Windows应用商店应用程序连接到SQL Server数据库

时间:2013-10-06 16:04:17

标签: c# sql-server wcf

您好我正在尝试使用WCF作为中间件将Windows商店应用程序连接到SQL Server数据库。我成功地做到了。但我无法将valus插入数据库。请有人帮我这个吗?

这是我的代码:

这是MainPage.xaml

<Page
    x:Class="WindowsStoreToSql.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WindowsStoreToSql"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" RenderTransformOrigin="0.508,0.484">
        <TextBox HorizontalAlignment="Left" Name="IdTextbox" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="108,162,0,0" Width="246"/>
        <TextBox HorizontalAlignment="Left" Name="NameTextbox" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="762,162,0,0" Width="246"/>
        <TextBox HorizontalAlignment="Left" Name="AgeTextBox"  TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="762,314,0,0" Width="246"/>
        <TextBox HorizontalAlignment="Left" Name="GenderTextBox"  TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="108,314,0,0" Width="246"/>
        <TextBox HorizontalAlignment="Left" Name="EmailTextBox"  TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="762,241,0,0" Width="246"/>
        <TextBox HorizontalAlignment="Left" Name="PasswordTextBox"  TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="108,241,0,0" Width="246"/>
        <Button Content="ShowData" Name="ShowData" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="590,422,0,0" Click="ShowData_Click"/>
        <Button Content="InsertData" Name="InsertData" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="352,422,0,0" Click="InsertData_Click"/>
        <GridView HorizontalAlignment="Left" Name="EmpGridview"  VerticalAlignment="Top" Width="150" Margin="646,162,0,0" Grid.Column="1" />

    </Grid>
</Page> 

这是MainPage.xaml.cs:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Popups;

namespace WindowsStoreToSql
{
    public sealed partial class MainPage : Page
    {
        App2.ServiceReference1.Service1Client MyService;
        public MainPage()
        {
            this.InitializeComponent();
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            MyService = new App2.ServiceReference1.Service1Client();
        }
        private async void InsertData_Click(object sender, RoutedEventArgs e)
        {
            await MyService.InsertEmployeeAsync(new App2.ServiceReference1.Employee() {EmpId = Convert.ToInt32(IdTextbox.Text), EmpName = NameTextbox.Text, EmpAge = AgeTextBox.Text, EmpGender = GenderTextBox.Text, EmpEmail = EmailTextBox.Text, EmpPassword = PasswordTextBox.Text });
        }
        private async void ShowData_Click(object sender, RoutedEventArgs e)
        {
            var EmpList = await MyService.GetEmployeeAsync();
            foreach (var Emp in EmpList)
            {
                GridViewItem EmpView = new GridViewItem();
                StackPanel Spanel = new StackPanel();
                Spanel.Children.Add(new TextBlock() { Text = Convert.ToString(Emp.EmpId) });
                Spanel.Children.Add(new TextBox() { Text = Emp.EmpName });
                Spanel.Children.Add(new TextBlock() { Text = Emp.EmpAge });
                Spanel.Children.Add(new TextBlock() { Text = Emp.EmpGender });
                Spanel.Children.Add(new TextBox() { Text = Emp.EmpEmail });
                Spanel.Children.Add(new TextBlock() { Text = Emp.EmpPassword });
                EmpView.Content = Spanel;
                EmpGridview.Items.Add(EmpView);
            }
        }
    }
}

我的问题:

在这一行:

await MyService.InsertEmployeeAsync(new App2.ServiceReference1.Employee() { EmpId = Convert.ToInt32(IdTextbox.Text), EmpName = NameTextbox.Text, EmpAge = AgeTextBox.Text, EmpGender = GenderTextBox.Text, EmpEmail = EmailTextBox.Text, EmpPassword = PasswordTextBox.Text });

我收到了EndpointNotFound异常:

> There was no endpoint listening at
> http://localhostname/Design_Time_Addresses/WcfServiceLibrary1/Service1/
> that could accept the message. This is often caused by an incorrect
> address or SOAP action. See InnerException, if present, for more
> details.

我无法解决这个问题。有人可以帮我这个吗?

谢谢。

0 个答案:

没有答案