将所有结果数据从数据库检索到列表视图

时间:2019-05-16 10:01:33

标签: c# database listview uwp

我有一个listview,其数据是从db上的数据中检索的。

XAML:

<ListView x:Name="sqlList" 
                          AutomationProperties.AutomationId="ItemsListView"
                          AutomationProperties.Name="Items" 
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <Grid Margin="0,20,0,0" HorizontalAlignment="Stretch" Background="{x:Null}">
                                    <ScrollViewer Grid.Row="0" Margin="0,0,5,5" VerticalAlignment="Top" HorizontalAlignment="Left" Height="45" Background="{x:Null}" VerticalScrollBarVisibility="Auto">
                                        <TextBlock Margin="0,0,10,10" Text="{Binding identifier}" Grid.Row="0" Foreground="Black" FontSize="17" VerticalAlignment="Top" HorizontalAlignment="Left" Height="auto" FontFamily="Segoe UI Black" TextWrapping="Wrap" TextAlignment="Justify" FontWeight="SemiBold"/>
                                    </ScrollViewer>
                                </Grid>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

代码:

string DB_PATH = Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, "uuthemis.db"));
                    ObservableCollection<undang2> idenO = new ObservableCollection<undang2>();
                    using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DB_PATH))
                    {
            var iden = conn.Query<undang2>("SELECT identifier FROM undangundang WHERE identifier like 'inpres no%' and identifier like '%th 2018").FirstOrDefault();

                        undang2 uu1 = new undang2();
                        uu1.identifier = iden.identifier;
                        idenO.Add(uu1);
                        sqlList.ItemsSource = idenO;
                        foreach (var a in idenO)
                        {
                            Debug.WriteLine("iden: " + a.identifier);
                        }
                    }

undang2.cs:

public class undang2
    {
        [SQLite.Net.Attributes.PrimaryKey,SQLite.Net.Attributes.AutoIncrement]
        public int uu_id { get; set; }

        public string identifier { get; set; }

        public undang2()
        {
        }

        public undang2(int UU_ID, string Identifier)
        {
            uu_id = UU_ID;
            identifier = Identifier;
        }
    }

我有一个问题,它只能首先检索数据。什么是汽车,所以您可以检索所有数据结果?

1 个答案:

答案 0 :(得分:0)

查询已返回IEnumerable<undang2>,因此没有理由创建另一个查询并将其添加到列表中。只需对结果调用.ToList()

string DB_PATH = Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, "uuthemis.db"));
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DB_PATH))
{
    var iden = conn.Query<undang2>("SELECT identifier FROM undangundang WHERE identifier like 'inpres no%' and identifier like '%th 2018")
        .ToList();

    sqlList.ItemsSource = new ObservableCollection<undang2>(iden);
}