I have used VS 2010 Beta and SQL Server Express Edition 2008.
Create a Table In SQL Server 2008.
sql Code:
USE [TestIdentity] GO /****** Object: Table [dbo].[Photos] Script Date: 12/18/2009 10:57:17 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Photos]( [PhotoId] [int] IDENTITY(1,1) NOT NULL, [PhotoBlob] [image] NULL, [PhotoTitle] [nvarchar](50) NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO /* Insert an image into this table. This is a short cut to insert images in a database. You can also use TEXTCOPY, but that's not available in the express edition of SQL Server. */ INSERT INTO Photos(PhotoTitle,PhotoBlob) SELECT 'Bino', BulkColumn from Openrowset( Bulk 'D:\user\Monkey_Bin.jpg', Single_Blob) As PhotoBlob GO /*Check your table */ SELECT * from Photos Go
This is my WPF XAML form.
This is the code I used to connect to the database and display the image. This code stores the image to a file location on the folder. For the purposes of demonstration, I have used the Current Directory.Code:<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" Name="MainForm"> <Grid> <Image Height="200" HorizontalAlignment="Left" Margin="24,16,0,0" Name="imgDBExtract" Stretch="Fill" VerticalAlignment="Top" Width="242" ToolTip="Extracted Image" Source="/ExtractImageFromDB;component/Images/Ben%20Franklin%20II.jpg" UseLayoutRounding="True" /> <Button Content="Click Me" Height="23" HorizontalAlignment="Left" Margin="291,16,0,0" Name="btnShow" VerticalAlignment="Top" Width="75" /> </Grid> </Window>
vb.net Code:
Imports System Imports System.Collections Imports System.ComponentModel Imports System.IO Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Class MainWindow Dim myImage As Image Dim myConn As SqlConnection Public Function ExtractImage() Try Dim cmdSelect As New SqlCommand("select PhotoBlob from Photos where PhotoID=4", myConn) Dim barrImg As Byte() = DirectCast(cmdSelect.ExecuteScalar(), Byte()) Dim strfn As String = Convert.ToString(DateTime.Now.ToFileTime()) Dim fs As New FileStream(strfn, FileMode.CreateNew, FileAccess.Write) fs.Write(barrImg, 0, barrImg.Length) fs.Flush() fs.Close() Dim myImg As New BitmapImage myImg.BeginInit() myImg.UriSource = New Uri(System.Environment.CurrentDirectory & "\" & strfn) myImg.EndInit() imgDBExtract.Source = myImg Catch ex As Exception MsgBox(ex.ToString, MsgBoxStyle.Critical, "eXTRACT IMAGE") End Try End Function Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnShow.Click Try myConn = New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=TestIdentity;Integrated Security=True;User Id=latetothegame") myConn.Open() If myConn.State = 1 Then ExtractImage() End If Catch ex As Exception MsgBox(ex.ToString, MsgBoxStyle.Critical, "cONNECT CLICK") End Try End Sub End Class
If you want to save images to a database, you can read this.


Reply With Quote
