【问题标题】:Binding listbox to a SQL database image field将列表框绑定到 SQL 数据库图像字段
【发布时间】:2016-03-25 06:50:10
【问题描述】:

我想使用 C# 和 WPF 在 SQL 数据库中显示图像字段中的图像。我尝试了以下代码,但它只检索图像字段数据以外的所有字段。

<ListBox x:Name="listBox" HorizontalContentAlignment="Left" FontFamily="Arial" FontSize="18" HorizontalAlignment="Left" Height="552" Margin="10,198,0,0" VerticalAlignment="Top" Width="754" ItemsSource="{Binding}" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <DockPanel >
                    <TextBlock Text="{Binding ID}" FontWeight="Bold"
                        DockPanel.Dock="Left"
                        Margin="5,0,10,0" Width="20" />
                    <TextBlock Text="{Binding Name}" FontWeight="Bold"
                        DockPanel.Dock="Left"
                        Margin="5,0,10,0" Width="150" />
                    <Image Source="{Binding PhotoID}"
                           DockPanel.Dock="Left"

                         Margin="5,0,10,0" Width="150" Height="150" Stretch="Uniform"/>

                </DockPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

在 C# 部分我也使用以下代码:

Private void RetrieveData()
    {
        string ConString = conn;
         using (SqlConnection con = new SqlConnection(ConString))
        {

            SqlCommand cmd = new SqlCommand(CmdString, con);
            sda = new SqlDataAdapter(cmd);
            dt = new DataTable();
            sda.Fill(dt);

        }

public MainPage()
    {
        InitializeComponent();
        try
        {
            CmdString = "Select ID,Name,PhotoID from Employees";
            RetrieveData();
            listBox.ItemsSource = dt.DefaultView;


        }
        catch
        {
            MessageBox.Show("db error");
        }
    }

请告诉我如何解决这个问题。

【问题讨论】:

    标签: c# sql image xaml listbox


    【解决方案1】:

    我解决了这个问题。实际上,上面提到的代码没有问题。我只是将 SQL 数据库中的字段数据类型从图像更改为二进制,这样问题就解决了。

    我不知道为什么会这样,但似乎图像数据类型有问题。

    【讨论】:

      猜你喜欢
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      • 2011-04-25
      • 1970-01-01
      相关资源
      最近更新 更多