1
我正在創建一個程序,這裏基本上都是它的代碼。這個想法很簡單,當有人點擊button_ok_Click時,我想顯示他們的名字和照片(根據他們插入的數字)。每件事情都可以正常工作,除了我曾嘗試過的一件事情沒有成功之外:當有人點擊button_ok_Click時,從我的數據庫中顯示圖像。 感謝您的幫助。WPF不顯示任何圖像..爲什麼?
<Image x:Name="image" HorizontalAlignment="Left" Height="386" Margin="480,120,0,0" VerticalAlignment="Top" Width="259" Source="{Binding Path=ImageFunc, Converter={StaticResource BinaryImageConverter}}"/>
namespace WpfApplication1
{
class FuncionarioDAO : IFuncionarioDAO
{
private DbConnection conn;
private Exception erro;
private string sql;
public FuncionarioDAO()
{
try
{
this.conn = DAOConexaoFactory.getConexao(3, "TEST", "TEST");
}
catch
{
erro = DAOConexaoFactory.getErro();
}
}
public Funcionario buscaFuncionario(string id)
{
Funcionario funcionario = new Funcionario();
sql = "SELECT CHAPA, NOME, IMAGEM FROM TABLE WHERE CHAPA='"+id+"'";
try
{
// Cria Conexão Driver especifico
DbCommand cmd = DAOConexaoFactory.getFactory().CreateCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
// Cria set de dados
DbDataReader dados = cmd.ExecuteReader();
// Converte Decimal para Double para IBM DB2 e MSSQL
if (dados.HasRows)
{
while (dados.Read())
{
funcionario.Mat = dados.GetString(0);
funcionario.Nome = dados.GetString(1);
funcionario.ImageFunc = (byte[]) dados["IMAGEM"];
}
}
}
catch (Exception ex)
{
// Retorna erro
erro = ex;
}
return funcionario;
}
public bool insereFuncionario(Funcionario funcionario)
{
throw new NotImplementedException();
}
public string getErro()
{
return erro.ToString();
}
}
}
namespace WpfApplication1
{
class Funcionario
{
private string mat;
private string nome;
private byte[] imagemFunc;
public Funcionario()
{
}
public Funcionario(string mat)
{
this.mat = mat;
}
public Funcionario(string mat, string nome, byte[] imagemFunc)
{
this.mat = mat;
this.nome = nome;
this.imagemFunc = imagemFunc;
}
public string Mat { get; set; }
public string Nome { get; set; }
public byte[] ImageFunc { get; set; }
}
}
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1(object sender, RoutedEventArgs e)
{
textBox.Text = textBox.Text + "1";
}
private void button_2_Click(object sender, RoutedEventArgs e)
{
textBox.Text = textBox.Text + "2";
}
private void button_3_Click(object sender, RoutedEventArgs e)
{
textBox.Text = textBox.Text + "3";
}
private void button_4_Click(object sender, RoutedEventArgs e)
{
textBox.Text = textBox.Text + "4";
}
private void button_5_Click(object sender, RoutedEventArgs e)
{
textBox.Text = textBox.Text + "5";
}
private void button_6_Click(object sender, RoutedEventArgs e)
{
textBox.Text = textBox.Text + "6";
}
private void button_7_Click(object sender, RoutedEventArgs e)
{
textBox.Text = textBox.Text + "7";
}
private void button_8_Click(object sender, RoutedEventArgs e)
{
textBox.Text = textBox.Text + "8";
}
private void button_9_Click(object sender, RoutedEventArgs e)
{
textBox.Text = textBox.Text + "9";
}
private void button_0_Click(object sender, RoutedEventArgs e)
{
textBox.Text = textBox.Text + "0";
}
private void button_erase_Click(object sender, RoutedEventArgs e)
{
textBox.Text = "";
}
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(" "));
}
}
}
namespace WpfApplication1
{
class BinaryImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
byte[] ByteArray = value as byte[];
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = new MemoryStream(ByteArray);
bmp.EndInit();
return bmp;
}
}
}
幾件事我不知道你需要調用cmd.ExecuteNonQuery(),因爲你直接調用cmd.ExecuteReader()之後。你的SQL不是更新,插入,刪除。不需要NonQuery。對於SQL命令,也使用SQL命令參數而不是String concat。查看SQL注入的原因。對於圖像轉換,你有沒有試過返回Explicit BitmapImage類型vs對象?你還需要在你的XAML中正確設置datacontext,以便你的Image控件知道ImageFunc的獲取位置。這是一個MVVM項目嗎? – Bearcat9425
你是對的,沒有必要的ExecuteNonQuery():d 這裏是我的背景下 <地方:BinaryImageConverter X:鍵= 「BinaryImageConverter」/> –
關於返回明確BitmapImage類型..我可以說...這是我第二天使用C#,我有點失落,但是,它似乎數據庫爲我返回一個「圖像」數據,我應該使用什麼樣的變量來獲取它? –