This is a discussion on how do I save images to SQL DB? within the ASP and ASP.NET Programming forums, part of the Web Development category; Hi All how do I save images to SQL DB?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi... Here I gave coding for how to uploading image to DB. Image SQL 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 UploadImage.aspx <%@ Page language="c#" Src="UploadImage.aspx.cs" Inherits="DBImages.UploadImage" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> </HEAD> <body bgcolor=#ffffff> <form enctype="multipart/form-data" runat=server id=form1 name=form1> <h3>The ASPFree Friendly Image Uploader</h3> Enter A Friendly 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> </body> </HTML> UploadImage.aspx.cs (codebehind 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; namespace DBImages { public class UploadImage : System.Web.UI.Page { protected System.Web.UI.WebControls.Button UploadBtn; protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1; protected System.Web.UI.HtmlControls.HtmlInputText txtImgName; protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile; public UploadImage() { } private void Page_Load(object sender, System.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("<BR>The Image was saved"); } else { Response.Write("<BR>An error occurred uploading the image"); } } } private int SaveToDB(string imgName, byte[] imgbin, string imgcontenttype) { //use the web.config to store the connection string SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["DSN"]); 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; } } } Web.Config <configuration> <appSettings> <add key="DSN" value="server=localhost;uid=sa;pwd=;Database=aspfr ee"/> </appSettings> <system.web> <customErrors mode="Off" /> </system.web> </configuration> Hope this will helpful for u...... ![]()
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| <%@ Page language="c#" Src="UploadImage.aspx.cs" Inherits="DBImages.UploadImage" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <body bgcolor=#ffffff> <form enctype="multipart/form-data" runat=server id=form1 name=form1> <h3>Upload your Image</h3> Enter A Friendly 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> </body> </HTML> using System; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Web; using System.IO; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace DBImages { public class UploadImage : System.Web.UI.Page { protected System.Web.UI.WebControls.Button UploadBtn; protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1; protected System.Web.UI.HtmlControls.HtmlInputText txtImgName; protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile; public UploadImage() { } private void Page_Load(object sender, System.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); String idis = Request.QueryString["id"]; int busid = System.Convert.ToInt32(idis); int RowsAffected = SaveToDB( imgName, imgBinaryData,imgContentType, busid); if ( RowsAffected>0 ) { Response.Write("<BR>The Image was saved"); } else { Response.Write("<BR>An error occurred uploading the image"); } } } private int SaveToDB(string imgName, byte[] imgbin, string imgcontenttype, int busid) { //use the web.config to store the connection string SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["DSN"]); SqlCommand command = new SqlCommand( "INSERT INTO Image (img_name,img_data,img_contenttype,business_id) VALUES ( @img_name, @img_data,@img_contenttype,@img_busid)", 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 ); SqlParameter param3 = new SqlParameter( "@img_busid", SqlDbType.Int,4 ); param3.Value = busid; command.Parameters.Add( param3 ); connection.Open(); int numRowsAffected = command.ExecuteNonQuery(); connection.Close(); return numRowsAffected; } } } |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Image Compression. Save images with any quality.. | Mramesh | PHP Programming | 2 | 03-11-2008 01:16 AM |
| Image Compression. Save images with any quality.. | Mramesh | C# Programming | 2 | 02-25-2008 07:33 AM |
| Image Compression. Save images with any quality.. | Mramesh | VB.NET Programming | 0 | 02-22-2008 02:43 AM |
| Five Budget Tips to Help You Save | montyauto | eCommerce | 1 | 03-28-2007 05:53 AM |