如何从SQLCe Db检索数据并将其绑定到ListBox

时间:2011-07-22 11:24:32

标签: linq-to-sql windows-phone-7 sql-server-ce

我已使用以下代码将数据插入到SqlCe数据库中:


using (DBContacts context = new DBContacts(ConnectionString))
            {
                TblContacts tblCtc = new TblContacts();

                tblCtc.FirstName = txtBoxFirstNm.Text;
                tblCtc.LastName = txtBoxLastNm.Text;
                tblCtc.Mobile1 = txtMobile1.Text;
                tblCtc.Email1 = txtEmail1.Text;

                context.TblContacts.InsertOnSubmit(tblCtc);
                context.SubmitChanges();

                MessageBox.Show("Inserted Ok.");


            }

问题:

1)如何使用Linq To SQL中的select并显示检索到的数据?


 using (DBContacts context = new DBContacts(ConnectionString))
            {
              IEnumerable  ctc = from c in context.TblContacts select c;   


        // what to get the result and display ??

               // txtBlkFirstname.Text =   ctc.FirstName  ??
               // txtBlkLastname.Text = ctc.LastName    ??
               .....     

            }

1a)使用哪种? IQueryable或IEnumerable?

1b)Select语句返回什么?如何处理数据返回?

1c)如何从Iqueryable或IEnumerable中获取数据并为TextBlock控件赋值?

------如何将数据绑定到ListBox                                                                                     

1 个答案:

答案 0 :(得分:2)

public List<TblContacts> GetAllContact()
    {
        using (DBContacts context = new DBContacts(ConnectionString))
        var contacts = from m in context.TblContacts select m;
        return contacts.Take(100).ToList();
    }

我在WCF服务中使用的类似内容,也应该在您的应用中使用,我希望,

尝试将列表框源绑定到GetAllContacts(),然后在该列表框中将其数据绑定到各个字段

       <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel Name="contentP" Opacity="1">
            <ListBox Height="623" Margin="-20,0,0,0" Name="listBox1" Width="473">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="132">
                            <Image Source="{Binding ImageSource}" Height="75" Width="75" VerticalAlignment="Top" Margin="15,10,10,0"/>
                            <StackPanel Width="380">
                                <ContentControl Margin="0,10,0,0" HorizontalAlignment="Left">
                                    <HyperlinkButton HorizontalAlignment="Left" Style="{StaticResource HyperlinkButtonStyle}" Content="{Binding Title}" Foreground="#FFC8AB14" NavigateUri="{Binding Link}" TargetName="_blank"/>
                             </ContentControl>
                             <TextBlock Margin="10,10,30,0" Text="{Binding Message}" Foreground="{StaticResource PhoneAccentBrush}" TextWrapping="Wrap" FontSize="20" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>           
    </Grid> 

在您的代码中,您将“listbox1”ItemsSource设置为您的DBQuery的源代码,不知道如何使用我的WCF服务在您的情况下执行此操作,但它的内容可能是这个

listbox1.ItemsSource = (result of your query ("e.Result;");