C# 数据库查询,如@Bearcat9425所述。

C# 数据库查询,如@Bearcat9425所述。,c#,wpf,database,data-binding,wpf-controls,C#,Wpf,Database,Data Binding,Wpf Controls,您似乎没有使用视图模型,因此可以直接将位图设置为图像: private void button_ok_Click(object sender, RoutedEventArgs e) { Funcionario funcionario = new FuncionarioDAO().buscaFuncionario(textBox.Text); textBox2.Text = funcionario.Nome.Substring(0,funcionario.Nome.IndexOf(

您似乎没有使用视图模型,因此可以直接将位图设置为图像:

private void button_ok_Click(object sender, RoutedEventArgs e)
{
    Funcionario funcionario = new FuncionarioDAO().buscaFuncionario(textBox.Text);
    textBox2.Text = funcionario.Nome.Substring(0,funcionario.Nome.IndexOf(" "));

    using (var stream = new MemoryStream(funcionario.ImageFunc))
    {
        BitmapImage bmp = new BitmapImage();
        bmp.BeginInit();
        bmp.CacheOption = BitmapCacheOption.OnLoad;
        bmp.StreamSource = stream;
        bmp.EndInit();

        image.Source = bmp;
    }

    // If you don't see image, perhap your image is invalid
    // Save it to a file and open this file in Windows Explorer to check
    File.WriteAllBytes("D:\\img.jpg", funcionario.ImageFunc);
}
这里没有必要使用IValueConverter。您需要将其从图像中删除:

...Source="{Binding Path=ImageFunc, Converter={StaticResource BinaryImageConverter}}"...
顺便说一下,您的
BinaryImageConverter
缺少
ConvertBack
方法。你是在邮政编码前剪掉的吗?因为如果你没有它,你的代码就不会编译

如果决定使用视图模型,可以执行以下操作:

ConvertBack
方法添加到您的
BinaryImageConverter

public class BinaryImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        byte[] ByteArray = value as byte[];
        BitmapImage bmp = new BitmapImage();
        using (var stream = new MemoryStream(ByteArray))
        {
            bmp.BeginInit();
            bmp.CacheOption = BitmapCacheOption.OnLoad;
            bmp.StreamSource = stream;
            bmp.EndInit();
        }
        return bmp;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}
设置数据上下文:

private void button_ok_Click(object sender, RoutedEventArgs e)
{
    Funcionario funcionario = new FuncionarioDAO().buscaFuncionario(textBox.Text);
    DataContext = funcionario;
    // If textBox2.Text bind to Funcionario you can remove bellow line, move IndexOf to buscaFuncionario function
    textBox2.Text = funcionario.Nome.Substring(0,funcionario.Nome.IndexOf(" "));
}

最后,您不应该像@Bearcat9425所说的那样,在数据库查询中使用String.Concat。

有几件事我不确定您是否需要调用cmd.ExecuteNonQuery(),因为您直接在后面调用cmd.ExecuteReader()。您的SQL不是更新、插入、删除。不需要非查询。对于SQL命令,也使用SQL命令参数而不是字符串concat。查找SQL注入的原因。对于图像转换,是否尝试返回显式BitmapImage类型vs对象?另外,您是否在XAML中正确设置了datacontext,以便图像控件知道从何处获取ImageFunc。这是一个MVVM项目吗?你是对的,不需要ExecuteOnQuery():D这是我关于返回显式BitmapImage类型的上下文。我怎么能说…这是我使用c#的第二天,我有点迷茫,但数据库似乎为我返回了一个“图像”数据,我应该用什么样的变量来获取它?我指的是你的转换器。在转换器中,您有公共对象转换,我是说您是否尝试过公共BitmapImage转换。您似乎没有使用任何类型的MVVM,因此您可能需要在按钮中对XAML进行绑定。\u确定\u单击后,您可能需要显式地将图像设置为image1。Source=(此处将字节数组转换为BitmapImage);或者我认为在绑定中可能是{binding Funcionario.ImageFunc},但您需要将其作为主窗口类的公共属性,并在按钮上设置它。\u ok\u click。有几件事我不确定您是否需要调用cmd.ExecuteNonQuery(),因为您直接在后面调用cmd.ExecuteReader()。您的SQL不是更新、插入、删除。不需要非查询。对于SQL命令,也使用SQL命令参数而不是字符串concat。查找SQL注入的原因。对于图像转换,是否尝试返回显式BitmapImage类型vs对象?另外,您是否在XAML中正确设置了datacontext,以便图像控件知道从何处获取ImageFunc。这是一个MVVM项目吗?你是对的,不需要ExecuteOnQuery():D这是我关于返回显式BitmapImage类型的上下文。我怎么能说…这是我使用c#的第二天,我有点迷茫,但数据库似乎为我返回了一个“图像”数据,我应该用什么样的变量来获取它?我指的是你的转换器。在转换器中,您有公共对象转换,我是说您是否尝试过公共BitmapImage转换。您似乎没有使用任何类型的MVVM,因此您可能需要在按钮中对XAML进行绑定。\u确定\u单击后,您可能需要显式地将图像设置为image1。Source=(此处将字节数组转换为BitmapImage);或者我认为在绑定中可能是{binding funciario.ImageFunc},但需要将其作为主窗口类的公共属性,并在单击按钮时设置它。
private void button_ok_Click(object sender, RoutedEventArgs e)
{
    Funcionario funcionario = new FuncionarioDAO().buscaFuncionario(textBox.Text);
    DataContext = funcionario;
    // If textBox2.Text bind to Funcionario you can remove bellow line, move IndexOf to buscaFuncionario function
    textBox2.Text = funcionario.Nome.Substring(0,funcionario.Nome.IndexOf(" "));
}