Prepare for your Next Interview
This is a discussion on How to store a picture in sql server 2000? within the SQL Server forums, part of the Databases category; How to store a picture in sql server 2000? I am using Asp.net please help me...
|
|||
|
There are 2 ways to do so.
1. Store the actual image on the file system or on the web server and just point that url in sql server. This is the simple and best way especially if you want to show those images in browser 2. Convert those images into binary streams and then store them in sqlserver. Here is the code: using System.Data.SqlClient; using System.IO protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1; protected System.Web.UI.WebControls.Button UploadBtn; protected System.Web.UI.HtmlControls.HtmlInputText txtImgName; protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile; private void UploadMe_Click(object sender, System.EventArgs e) { if (Page.IsValid) { 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 = Save( 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 Save(string imgName, byte[] imgbin, string imgcontenttype) { SqlCommand command = new SqlCommand( "INSERT INTO tbl_Image (img_name,img_data,img_contenttype) VALUES ( @img_name, @img_data,@img_contenttype)", con ); 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 ); con.Open(); int numRowsAffected = command.ExecuteNonQuery(); con.Close(); return numRowsAffected; } |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to store a File in Databes? | chowsys | VB.NET | 2 | 12-26-2007 05:42 AM |
| SQLServer 2005 restore to 2000 | sonzls | SQL Server | 2 | 04-22-2007 08:36 AM |
| Guidelines for ISO 90001:2000 | JobHelper | Testing Issues | 1 | 12-23-2006 08:19 AM |
| Uploading picture | bharathi_ark | Testing Issues | 2 | 12-14-2006 06:00 AM |
| DNS server in Windows server 2000 | FantaGuy | Web Servers | 2 | 12-13-2006 03:21 PM |