Thursday, February 5, 2009

Uploading-Images-to-a-Database-C#

SQl Table Creation

CREATE TABLE [dbo].[image] (
[img_pk] [int] IDENTITY (1, 1) NOT NULL ,
[img_name] [varchar] (50) NULL ,
[img_data] [image] NULL ,
[img_contenttype] [varchar] (50) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[image] WITH NOCHECK ADD
CONSTRAINT [PK_image] PRIMARY KEY NONCLUSTERED
(
[img_pk]
) ON [PRIMARY] GO

in aspx page:

form id="form1" name="form1" enctype="multipart/form-data" runat="server"
Image Uploader
Enter A Name: input type="text" id="txtImgName" runat="server">
asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required"
ControlToValidate="txtImgName"> /asp:RequiredFieldValidator>
br>
Select File To Upload:
input id="UploadFile" type="file" runat="server"
asp:Button ID="UploadBtn" Text="Upload Me!" OnClick="UploadBtn_Click" runat="server"
/asp:Button
/form

In .cs File

using System;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.IO;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


public partial class UploadImage : System.Web.UI.Page
{
public UploadImage() { }
protected void Page_Load(object sender, EventArgs e)
{

}
public void UploadBtn_Click(object sender, System.EventArgs e)
{
if (Page.IsValid) //save the image
{
Stream imgStream = UploadFile.PostedFile.InputStream;
int imgLen = UploadFile.PostedFile.ContentLength;
string imgContentType = UploadFile.PostedFile.ContentType;
string imgName = txtImgName.Value;
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryData, 0, imgLen);

int RowsAffected = SaveToDB(imgName, imgBinaryData, imgContentType);
if (RowsAffected > 0)
{
Response.Write("
The Image was saved");
}
else
{
Response.Write("
An error occurred uploading the image");
}

}
}
private int SaveToDB(string imgName, byte[] imgbin, string imgcontenttype)
{
//use the web.config to store the connection string
string connectionString = @"Data Source =DEIVAMURUGAN\SQLEXPRESS ;Initial Catalog=master;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);

//SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["conString"]);
SqlCommand command = new SqlCommand( "INSERT INTO Image (img_name,img_data,img_contenttype) VALUES ( @img_name, @img_data,@img_contenttype )", connection );

SqlParameter param0 = new SqlParameter( "@img_name", SqlDbType.VarChar,50 );
param0.Value = imgName;
command.Parameters.Add( param0 );

SqlParameter param1 = new SqlParameter( "@img_data", SqlDbType.Image );
param1.Value = imgbin;
command.Parameters.Add( param1 );

SqlParameter param2 = new SqlParameter( "@img_contenttype", SqlDbType.VarChar,50 );
param2.Value = imgcontenttype;
command.Parameters.Add( param2 );

connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();

return numRowsAffected;
}
}

No comments: