Hi,
Try this example:
public static string aConnectionString = ConfigurationManager.AppSettings["AGMDat"].ToString();
public static Stream GetPhoto(int photoid, PhotoSize size)
{
object result;
SqlConnection aConnection = new SqlConnection(aConnectionString);
using (SqlCommand command = new SqlCommand("GetPhoto", aConnection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@PhotoID", photoid));
command.Parameters.Add(new SqlParameter("@Size", (int)size));
try
{
aConnection.Open();
result = command.ExecuteScalar();
}
catch (Exception)
{
return null;
}
finally
{
command.Dispose();
aConnection.Close();
}
return new MemoryStream((byte[])result);
}
}
In your Store procedure (Example):
USE [AGMDAT]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetPhoto]
@PhotoID int,
@Size int
AS
IF @Size = 1
SELECT TOP 1 [BytesThumb] FROM [Photos] LEFT JOIN [Category] ON [Category].[CategoryID] = [Photos].[CategoryID] WHERE [PhotoID] = @PhotoID AND ([Category].[IsPublic] = @IsPublic OR [Category].[IsPublic] = 1)
ELSE IF @Size = 2
SELECT TOP 1 [BytesPoster] FROM [Photos] LEFT JOIN [Category] ON [Category].[CategoryID] = [Photos].[CategoryID] WHERE [PhotoID] = @PhotoID AND ([Category].[IsPublic] = @IsPublic OR [Category].[IsPublic] = 1)
ELSE IF @Size = 3
SELECT TOP 1 [BytesFull] FROM [Photos] LEFT JOIN [Category] ON [Category].[CategoryID] = [Photos].[CategoryID] WHERE [PhotoID] = @PhotoID AND ([Category].[IsPublic] = @IsPublic OR [Category].[IsPublic] = 1)
ELSE IF @Size = 4
SELECT TOP 1 [BytesOriginal] FROM [Photos] LEFT JOIN [Category] ON [Category].[CategoryID] = [Photos].[CategoryID] WHERE [PhotoID] = @PhotoID AND ([Category].[IsPublic] = @IsPublic OR [Category].[IsPublic] = 1)
ELSE
SELECT TOP 1 [BytesPoster] FROM [Photos] LEFT JOIN [Category] ON [Category].[CategoryID] = [Photos].[CategoryID] WHERE [PhotoID] = @PhotoID AND ([Category].[IsPublic] = @IsPublic OR [Category].[IsPublic] = 1)
RETURN
Hope this could help...
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Regards,
Algem