保存照片并通过wpf从sql重新检索 [英] Save photo and retrieve again from sql by wpf

查看:67
本文介绍了保存照片并通过wpf从sql重新检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我想通过wpf将照片添加到sql,并且每次我选择一个员工姓名时,它都会从sql中检索照片并在图像控件上看到它.我可以将照片加载到图像控件中.我需要一个代码来将照片以及名称和家庭一起保存到sql数据库中,
并选择名称"或家庭"时,从sql中检索照片.

Hi everyone,
I want to add photo to sql by wpf and every time I select an employee name it''s photo retrieve from sql and see it on image control. I can load photo to image control. I need a code to save photo to sql database along with Name and Family,
and when Name or Family was selected, photo retrieve from sql.

<Datagrid>
	<DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=No}" Header="No" Width="Auto"></DataGridTextColumn>
        <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="Auto"></DataGridTextColumn>
        <DataGridTextColumn Binding="{Binding Path=Family}" Header="Family" Width="Auto"></DataGridTextColumn>
        <DataGridTextColumn Binding="{Binding Path=Photo}" Header="Photo" Width="Auto"></DataGridTextColumn>
	</DataGrid.Columns>
</DataGrid>
			
<TextBox Grid.Column="1" Grid.Row="0" Height="25" Name="txtNo" VerticalAlignment="Center" Text="{Binding Path=No}" Margin="3" removed="#FFC8EAF0" FontWeight="Bold" DataContext="{Binding Path=SelectedItem, ElementName=grdPersonnel1}" FontFamily="Arial" FontSize="11" Padding="0" VerticalContentAlignment="Center"></TextBox>
			
<TextBox Grid.Column="1" Grid.Row="1" Height="25" Name="txtName" VerticalAlignment="Center" Text="{Binding Path=Name}" Margin="3" removed="#FFC8EAF0" FontWeight="Bold" DataContext="{Binding Path=SelectedItem, ElementName=grdPersonnel1}" FontFamily="Arial" FontSize="11" Padding="0" VerticalContentAlignment="Center"></TextBox>
				
<TextBox Grid.Column="1" Grid.Row="2" Height="25" Name="txtFamily" VerticalAlignment="Center" Text="{Binding Path=Family}" Margin="3" removed="#FFC8EAF0" FontWeight="Bold" DataContext="{Binding Path=SelectedItem, ElementName=grdPersonnel1}" FontFamily="Arial" FontSize="11" Padding="0" VerticalContentAlignment="Center"></TextBox>
				
<Image Grid.Column="6" Margin="49,6,44,0" Grid.RowSpan="7" Name="Photo" DataContext="{Binding Path=SelectedItem, ElementName=grdPersonnel1}"></Image>
<Button Grid.Column="6" Margin="88,4,80,26" Grid.Row="7" Grid.RowSpan="2" Name="btnAddphoto" Click="btnAddphoto_Click">Add photo</Button>





private void btnAddphoto_Click(object sender, RoutedEventArgs e)
       {

           Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
           dlg.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.* ";
           if (dlg.ShowDialog() == true)
           {
               System.IO.Stream stream = System.IO.File.Open(dlg.FileName, System.IO.FileMode.Open);

               BitmapImage imgsrc = new BitmapImage();
               imgsrc.BeginInit();
               imgsrc.StreamSource = stream;
               imgsrc.EndInit();
               this.Photo.Source = imgsrc;

       }





([No] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](40) NULL,
	[Family] [nvarchar](50) NULL,
	[Photo] [varbinary] (max) NULL)

推荐答案

此处是一个链接,向您展示如何从Microsoft SQL保存和检索图像:

http://www .redmondpie.com/how-to-save-and-retrieve-images-in-c-wpf-application-from-sql-server-database/ [
Here is a link that shows you how to save and retrieve images from Microsoft SQL:

http://www.redmondpie.com/how-to-save-and-retrieve-images-in-c-wpf-application-from-sql-server-database/[^]

Following this article should solve your issues. You might also want to consider saving your images to a central location and then just saving the image path to the database. That will reduce how large your database gets and yet it will still work well. Just a thought.


这篇关于保存照片并通过wpf从sql重新检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆