C# 使用SQL(SDF)数据库中的项填充WPF列表框

C# 使用SQL(SDF)数据库中的项填充WPF列表框,c#,sql,wpf,listbox,populate,C#,Sql,Wpf,Listbox,Populate,我一直在寻找如何做到这一点很长时间,我还没有设法得到一个关于这个问题的直接答案,所以希望您的StackOverflow用户之一将能够在这里帮助我。我有一个名为CategoryList的WPF列表框和一个名为ProgramsList.SDF的SDF数据库(有两个名为CategoryList和ProgramsList的表)。我希望我的程序做的是从CategoryList表中获取类别名称,并在名为CategoryList的ListBox控件中列出它们 这是我尝试过的代码,但它只会导致我的程序崩溃

我一直在寻找如何做到这一点很长时间,我还没有设法得到一个关于这个问题的直接答案,所以希望您的StackOverflow用户之一将能够在这里帮助我。我有一个名为CategoryList的WPF列表框和一个名为ProgramsList.SDF的SDF数据库(有两个名为CategoryList和ProgramsList的表)。我希望我的程序做的是从CategoryList表中获取类别名称,并在名为CategoryList的ListBox控件中列出它们

这是我尝试过的代码,但它只会导致我的程序崩溃

    SqlConnection myConnection = new SqlConnection("Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "ProgramsList.sdf");
    SqlDataReader myReader = null;

    myConnection.Open();
    CategoryList.Items.Clear();
    SqlDataReader dr = new SqlCommand("SELECT Name FROM CategoryList ORDER BY Name DESC", myConnection).ExecuteReader();

    while (myReader.Read())
    {
        CategoryList.Items.Add(dr.GetInt32(0));
    }
    myConnection.Close();

有人能帮我吗?提前谢谢

我想试试这样的东西:

var myConnection = new SqlConnection("Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "ProgramsList.sdf");
var cmd = new SqlCommand("SELECT Name FROM CategoryList ORDER BY Name DESC", myConnection);

myConnection.Open();
CategoryList.Items.Clear();

var sda = new SqlDataAdapter(cmd);
var ds = new DataSet();
sda.Fill(ds);

CategoryList.ItemsSource = ds.Tables["CategoryList"];

myConnection.Close(); 
<ListBox>
    <ListBox.Resources>
        <DataTemplate x:Key="DataTemplateItem">
            <Grid Height="Auto" Width="Auto">
                <TextBlock x:Name="Name" Text="{Binding Name}" />
            </Grid>
        </DataTemplate>
    </ListBox.Resources>
</ListBox>
请注意,您将需要在CategoryList对象中设置正确的绑定,可能需要通过以下XAML:

var myConnection = new SqlConnection("Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "ProgramsList.sdf");
var cmd = new SqlCommand("SELECT Name FROM CategoryList ORDER BY Name DESC", myConnection);

myConnection.Open();
CategoryList.Items.Clear();

var sda = new SqlDataAdapter(cmd);
var ds = new DataSet();
sda.Fill(ds);

CategoryList.ItemsSource = ds.Tables["CategoryList"];

myConnection.Close(); 
<ListBox>
    <ListBox.Resources>
        <DataTemplate x:Key="DataTemplateItem">
            <Grid Height="Auto" Width="Auto">
                <TextBlock x:Name="Name" Text="{Binding Name}" />
            </Grid>
        </DataTemplate>
    </ListBox.Resources>
</ListBox>

也许你的意思是:


..

更好的方法是将列表绑定到您创建的对象。这样,您就可以为DisplayMemberPath(您看到的)和SelectedValuePath(您的程序内部值)指定属性

这是您的主要XAML代码。注意,按钮的单击方法将显示组合框的当前选定值。这样以后事情就容易多了。希望这不是矫枉过正,但它展示了一些使WPF变得简单的原则

namespace WPFListBoxSample {

public partial class Window1 : Window
{

}

XAML

    <Grid>
    <StackPanel>
        <ComboBox x:Name="myCombo" ItemsSource="{Binding Categories}" DisplayMemberPath="CategoryName"  SelectedValuePath="Id" />
        <Button x:Name="myButton" Content="Show Product" Click="myButton_Click"/>
    </StackPanel>
</Grid>
注意{get;set;}的

最后,让很多事情变得简单的一点胶水是将所有数据放在一个模型中并绑定到该模型。这是使用WPF的方法

using System.Collections.Generic;
namespace WPFListBoxSample
{
    public class WPFListBoxModel
    {
        private IList<Category> _categories;
        public IList<Category> Categories
        {
            get
            {
                if (_categories == null)
                    _categories = new List<Category>();
                return _categories; }
            set { _categories = value; }
        }
    }
}
使用System.Collections.Generic;
命名空间WPFListBoxSample
{
公共类WPFListBoxModel
{
私有IList_类别;
公共图书馆分类
{
得到
{
如果(_categories==null)
_类别=新列表();
返回_categories;}
设置{u categories=value;}
}
}
}

我尝试过,但在ds.Tables[“CategoryList”]下出现了一个错误。我相信您可能已经混淆了数据表和集合。编辑:它应该是ds.Table[“CategoryList”].AsEnumerable()。我会回信看看是否有效。对不起。无论我做什么,它仍然崩溃,并给我一个错误26,即:错误定位指定的服务器/实例。有什么帮助吗?PS:同样,应该是“ds.Tables[0].AsEnumerable();”:)实际上,ds.Tables不应该需要.AsEnumerable(),因为“.Tables”是用于数据集的,对吗?为什么Visual Studio要求进行此转换?听起来您的问题是连接字符串的问题?你确定计算的路径是正确的吗?我希望如此。为了确定,您可以通过在数据库资源管理器中选择sdf并查看connection string属性来访问连接字符串,对吗?编辑:连接字符串属性仅包含基本字符串;你还有什么可以推荐的参数吗?我终于找到了答案,天哪,我真傻!我在我的后端代码中使用了SQL Server,这是一个用于联网客户机服务器应用程序的框架,而我本应该在本地桌面程序中使用SQL Compact Edition!我没有意识到MDF是针对SQL Server的,SDF是针对MDF的。不管怎样,我结合了Nate和stratton的代码,并对它们进行了修改,以与SQL CE一起使用。谢谢大家的帮助!这里,如果您需要加载字符串值,则意味着您需要放置
dr.GetString(1)
。如果需要加载int值,则意味着需要放置
dr.GetInt32(0)
。这对我有用。
using System.Collections.Generic;
namespace WPFListBoxSample
{
    public class WPFListBoxModel
    {
        private IList<Category> _categories;
        public IList<Category> Categories
        {
            get
            {
                if (_categories == null)
                    _categories = new List<Category>();
                return _categories; }
            set { _categories = value; }
        }
    }
}