Back To Samples


CREATE TABLE SQL SCRIPT

CREATE TABLE [dbo].[ImageTable]( [ticketId] [varchar](50) NOT NULL, [imageData] [image] NULL, CONSTRAINT [PK_ImageTable] PRIMARY KEY CLUSTERED ( [ticketId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

=================================================================================================================

INSERT SIGNATURE TO DB

Bitmap bmp = ctlSignature.SaveSignature("");
byte[] imageData;
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
    bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);     stream.Position = 0;
    imageData = new byte[stream.Length];
    stream.Read(imageData, 0, imageData.Length);
    stream.Close();
}

SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=YOUR_SERVER;User ID=sa;Password=sa;Initial Catalog=YOUR_DATABASE"; //provide connection string of your server.
con.Open(); //open connection to server.
string query = "insert into ImageTable values (@ticketId, @imageData)"; //create a query variable.
SqlCommand cmd = new SqlCommand(query, con); //create a sqlcommand object.
cmd.Parameters.Add(new SqlParameter("@ticketId", Request.QueryString["ticketId"])); //add first parameter ticket id.
cmd.Parameters.Add(new SqlParameter("@imageData", imageData)); //add second parameter image bytes.


int rows = cmd.ExecuteNonQuery(); //execute query.

if (rows > 0)
{
    }
else
{
   }

con.Close();
=================================================================================================================

LOAD BACK FROM DB

if (null != Request.QueryString["ticketId"])
{
    string ticketId = Convert.ToString(Request.QueryString["ticketId"]);

    SqlConnection con = new SqlConnection();
    con.ConnectionString = @"Data Source=YOUR_SERVER;User ID=sa;Password=sa;Initial Catalog=YOUR_DATABASE"; //provide connection string of your server.
    con.Open(); //open connection to server.
    SqlCommand cmdSelect = new SqlCommand("select imageData from ImageTable where ticketId=@ticketId", con);
    cmdSelect.Parameters.Add("@ticketId",SqlDbType.VarChar,50);
    cmdSelect.Parameters["@ticketId"].Value = ticketId;
    byte[] imageData = (byte[])cmdSelect.ExecuteScalar();
    MemoryStream ms = new MemoryStream(imageData);


    Response.ContentType = "image/png";
    ms.WriteTo(Response.OutputStream);
    Response.End();
}