**************************************************************************
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Uploading downloading pictures to from a SQL Serve in Asp.Net c#</title>
<style type="text/css">
.imgStyle
{
width:90px;
height:100px;
border:1px solid #d1d1e8;
padding:2px;
margin:2px;
background-color:#d1d1e8;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Uploading downloading pictures to from a SQL Serve</h2>
<p id="result" runat="server"></p>
<p>Image tag:</p>
<p><asp:TextBox ID="txtTag" runat="server"></asp:TextBox></p>
<p>Browse image from your computer:</p>
<p><asp:FileUpload ID="FileUpload1" runat="server" /></p>
<p><asp:Button ID="butSave" runat="server" Text="Upload" onclick="butSave_Click" /></p>
</div>
<hr />
<div>
<asp:GridView ID="GridView1"
runat="server"
ShowHeader="false"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="Image1"
CssClass="imgStyle"
ToolTip='<%# Eval("Tag") %>'
runat="server"
ImageUrl='<%# "Handler.ashx/ReadImagegalleryForImgdataById?id=" + Eval("Id") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
public partial class _Default : System.Web.UI.Page
{
private Imagegallery imageGallery = new Imagegallery();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void butSave_Click(object sender, EventArgs e)
{
try
{
imageGallery = new Imagegallery();
imageGallery.Tag = txtTag.Text;
imageGallery.Imgdata = FileUpload1.FileBytes;
imageGallery.Create(imageGallery);
}
catch (Exception ex)
{
result.InnerHtml = ex.Message;
}
}
private void BindGrid()
{
imageGallery = new Imagegallery();
GridView1.DataSource = imageGallery.ReadAll();
GridView1.DataBind();
}
}
using System;
using System.Web.Configuration;
public class Connection
{
public Connection(){}
/// <summary>
/// Get Connection string.
/// <summary>
public static String Cs
{
get
{
return WebConfigurationManager.ConnectionStrings["cs"].ConnectionString;
}
}
}
**************************************************************************
#region Namespaces
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
#endregion
/// <summary>
/// class Imagegallery
/// </summary>
public class Imagegallery
{
#region Properties
private Int32 _Id;
/// <summary>
///Gets or Sets the Int32 value of Id
/// </summary>
public Int32 Id { get { return _Id;} set { _Id = value;} }
private String _Tag;
/// <summary>
///Gets or Sets the String value of Tag
/// </summary>
public String Tag { get { return _Tag;} set { _Tag = value;} }
private Byte[] _Imgdata;
/// <summary>
///Gets or Sets the Byte[] value of Imgdata
/// </summary>
public Byte[] Imgdata { get { return _Imgdata;} set { _Imgdata = value;} }
#endregion
#region Private Meambers
private SqlCommand cmd;
private SqlConnection con;
private SqlDataReader table;
private String ConnectionString = String.Empty;
#endregion
#region Constructor
/// <summary>
///Default constructor
/// </summary>
public Imagegallery()
{
ConnectionString=Connection.Cs;
}
/// <summary>
///Parameterised constructor
/// </summary>
public Imagegallery(String connectionString)
{
ConnectionString=connectionString;
}
#endregion
#region Public Methods
/// <summary>
/// This meathod is for inserting record in Imagegallery table.
/// </summary>
/// <param name="object of Imagegallery"></param>
/// <returns>Number of row affected by query.</returns>
public Int32 Create(Imagegallery obj)
{
using(con = new SqlConnection(ConnectionString))
{
try
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd = new SqlCommand("Insert Into [dbo].[Imagegallery]([Tag],[Imgdata]) Values(@Tag,@Imgdata)",con);
cmd.Parameters.AddWithValue("@Tag",obj._Tag);
cmd.Parameters.AddWithValue("@Imgdata",obj._Imgdata);
return cmd.ExecuteNonQuery();
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
cmd.Parameters.Clear();
}
}
}
/// <summary>
/// This meathod returns all records of Imagegallery table.
/// </summary>
/// <returns> All rows of Imagegallery table.</returns>
public List<Imagegallery> ReadAll()
{
using(con = new SqlConnection(ConnectionString))
{
try
{
List<Imagegallery> list = new List<Imagegallery>();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd = new SqlCommand("Select Top(1000)* From [dbo].[Imagegallery]",con);
using(table = cmd.ExecuteReader())
{
while (table.Read())
{
Imagegallery obj = new Imagegallery();
obj._Id = table.GetInt32(0);
obj._Tag = table.GetString(1);
list.Add(obj);
}
//return (list.Count > 0) ? list : GetEmptyList(list);
return list;
}
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}
/// <summary>
/// This meathod return rows of type image from Imagegallery table by column Id.
/// </summary>
/// <param name="Id"></param>
/// <returns>Rows of type image from Imagegallery table.</returns>
public System.IO.Stream ReadImagegalleryForImgdataById(String id)
{
try
{
con = new SqlConnection(ConnectionString);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd = new SqlCommand("Select [Imgdata] From [dbo].[Imagegallery] Where [Id]=@Id",con);
cmd.Parameters.AddWithValue("@Id",id);
return new System.IO.MemoryStream((Byte[])cmd.ExecuteScalar());
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
cmd.Parameters.Clear();
}
}
}
#endregion
}
<connectionStrings>
<add name="cs" connectionString="Data Source=Your Data Source;Initial Catalog=softwarekaffee;Persist Security Info=True;User ID=Your User Id;Password=Your Password"/>
</connectionStrings>
**************************************************************************
/*Handler.ashx code*/
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.IO;
public class Handler : IHttpHandler{
public void ProcessRequest (HttpContext context) {
String ID;
if (context.Request.QueryString["id"] != null)
ID = context.Request.QueryString["id"].ToString();
else
throw new ArgumentException("No parameter specified");
context.Response.ContentType = "image/jpeg";
Stream strm = new Imagegallery().ReadImagegalleryForImgdataById(ID);
Byte[] buffer = new byte[2048];
Int32 byteSeq = strm.Read(buffer, 0, 2048);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 2048);
}
}
public bool IsReusable {
get {
return false;
}
}
}
**************************************************************************
Go
Create database softwarekaffee
Go
use softwarekaffee
Go
create table ImageGallery
(
Id int identity primary key,
Tag nvarchar(30),
ImgData image
)
No comments:
Post a Comment