简体   繁体   English

使用 WCF 将 SQL Azure 连接到 Windows Phone 7

[英]Connecting SQL Azure to Windows Phone 7 using WCF

I'm new in programming windows phone 7 but get really tired cuz i spend 3 days on one problem.我是 windows 电话 7 编程的新手,但我真的很累,因为我花了 3 天时间解决一个问题。 I search all inte.net and get some good explanions but without luck - it doesn't work on me program.我搜索了所有 inte.net 并得到了一些很好的解释,但没有运气——它对我的程序不起作用。

I create in SQL Azure one table which i called dbo.Messenger with structure:我在 SQL Azure 中创建了一张表,我将其称为dbo.Messenger ,其结构如下:

  • id (PK, not null) id(PK,不为空)
  • category (nvarchar(30), null)类别(nvarchar(30),空)
  • message (nvarchar(max), null)消息(nvarchar(最大值),空)
  • description (nvarchar(200), null)说明(nvarchar(200),空)

Then i make for it WCF wchich should bring me a list of it:然后我为它做了 WCF 应该给我一份清单:

 [OperationContract] List<NoteDto> GetNotes();
    public List<NoteDto> GetNotes()
    {
        using (var context = new WP7mgrEntities())
        {
            var notes = (from eachNote in context.Messenger
                         orderby eachNote.id ascending
                         select new NoteDto
           {
               id = eachNote.id,
               category= eachNote.category,
               description= eachNote.description,
               message= eachNote.message,
           }
                ).ToList();
            return notes;
        }
    }

of cource got for each DataMember like this on extra class NoteDto:当然,每个 DataMember 在额外的 class 上都得到了这样的 NoteDto:

  [DataMember] 
    public int id {get; set; }

So after this i make wp7 apps which get listbox which should also be fill afert i click button2所以在这之后我制作了 wp7 应用程序,它得到了列表框,它也应该在我点击按钮 2 之后被填充

        <ListBox Height="431" HorizontalAlignment="Left" Margin="12,199,0,0" Name="listBox1" VerticalAlignment="Top" Width="438"
                 ItemsSource="{Binding Notes}">
         <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding category}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>         
        </ListBox> 

And this code behind of this:这背后的代码:

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        Service1Client client = new Service1Client();
        client.GetNotesCompleted += new EventHandler<GetNotesCompletedEventArgs>(client_GetNotesCompleted);
        this.Notes = new ObservableCollection<NoteDto>();

    }
    private ObservableCollection<NoteDto> _notes;
    public ObservableCollection<NoteDto> Notes
    {
        get { return _notes; }
        set { _notes = value;
        this.RaisePropertyChanged("Notes");
        } 
    }

public event PropertyChangedEventHandler PropertyChanged;公共事件 PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged(string propertyName) { PropertyChangedEventHandler propertyChanged = this.PropertyChanged; private void RaisePropertyChanged(string propertyName) { PropertyChangedEventHandler propertyChanged = this.PropertyChanged; if ((propertyChanged,= null)) { propertyChanged(this; new PropertyChangedEventArgs(propertyName));如果 ((propertyChanged,= null)) { propertyChanged(this; new PropertyChangedEventArgs(propertyName)); } } } }

    void client_GetNotesCompleted(object sender, GetNotesCompletedEventArgs e)
    {this.Notes = e.Result; }

When i click button 2 my listbox isn't fill by records from database.当我单击按钮 2 时,我的列表框未被数据库中的记录填充。
Any idea?任何的想法? Plz help?请帮助?

What kind of binding are you using?您使用的是哪种绑定? recall that only wshttpbinding is not available for WP7.回想一下,只有 wshttpbinding 不适用于 WP7。 On the other hand, why don't you expose such database with WCF Data Service as OData .另一方面,为什么不将此类数据库与 WCF 数据服务公开为OData

Check it out. 看看这个。

Hope it helpful,希望有帮助,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM