Geeks Talk

Prepare for your Next Interview




How to store a picture in sql server 2000?

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...


Go Back   Geeks Talk > Databases > SQL Server

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-11-2007
Junior Member
 
Join Date: Feb 2007
Location: Chennai
Posts: 0
Thanks: 0
Thanked 0 Times in 0 Posts
nmurugantech is on a distinguished road
How to store a picture in sql server 2000?

How to store a picture in sql server 2000?
I am using Asp.net please help me
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-19-2007
Junior Member
 
Join Date: Feb 2007
Location: Bangalore
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
shrinidhi_bhat is on a distinguished road
Thumbs up Re: How to store a picture in sql server 2000?

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;
}
Reply With Quote
Reply

  Geeks Talk > Databases > SQL Server


Thread Tools
Display Modes


Similar Threads

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


All times are GMT -4. The time now is 02:57 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved