IT Community - Software Programming, Web Development and Technical Support

sql server

This is a discussion on sql server within the ASP and ASP.NET Programming forums, part of the Web Development category; how to store the picture , audio ,video,flash in sql server... i am new in this site,,, can any body ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 10-05-2007, 06:46 AM
seetha seetha is offline
D-Web Trainee
 
Join Date: Oct 2007
Posts: 7
seetha is on a distinguished road
Default sql server

how to store the picture , audio ,video,flash in sql server...

i am new in this site,,,
can any body tell me relevant data type for each with ?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-02-2008, 02:28 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default Re: sql server

we can store the Image as Binary Data...
__________________
The KINGMAKER
Makes Every Thing Possible

Stuffs (My Blog)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-21-2008, 09:56 PM
poornima poornima is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 189
poornima is on a distinguished road
Smile Re: sql server

Hi,
We can store the image by using its id and path in sql server...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-25-2008, 11:51 PM
Kirubhananth Kirubhananth is offline
D-Web Programmer
 
Join Date: Feb 2008
Location: My native is Madurai but now in Chennai
Posts: 61
Kirubhananth is on a distinguished road
Send a message via AIM to Kirubhananth Send a message via Skype™ to Kirubhananth
Smile Re: sql server

Hi,

You can do it in two ways.
1. Save the file in a specific folder and save only the path of the file where it was saved in the Database.

2. Extract the name of the file, file type and the content in it and save them seperately in the db. Therefore each row consists of the name of the file,the file type and the content in three different field of a single row.

The following code is to save the files in the above said second rule.

Step 0:Create a new table in SQL named files with fields fid,fname,fdata,ftype

Step 1: Open a new Visual Stdio ASP.NET file.

Step 2: Place a fileupload tool from the toolbox.

Step 3: Place a textbox near the fileupload tool to enter the unique file ID.

Step 4:Place two labelboxs to display the status.

Step 5:Place two commandbuttons one near the upload tool and enter its text property as "Upload". Place the other commandbutton just away and enter its text property as "Download.

Step 6: Enter the connection string as
string constring = "Data Source=SYSTEM-3\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True";
inside the class .

Step 7: Enter the following code for the button1(upload button)'s click event
as

protected void Button1_Click(object sender, EventArgs e)
{
Stream fstream = FileUpload1.PostedFile.InputStream;
int flen = FileUpload1.PostedFile.ContentLength;
string ftype = FileUpload1.PostedFile.ContentType;
string fname = FileUpload1.FileName;
byte[] binarydata = new byte[flen];
int n = fstream.Read(binarydata, 0, flen);
string id = TextBox1.Text;
int no = savetoDB(id,fname, binarydata, ftype);
if (no > 0)
Response.Write("The file was uploaded successfully");
else
Response.Redirect("The file was not uploded successfully");
}



Step 8: Add a new function as follows.

private int savetoDB(string id, string fName, byte[] fData, string fType)
{
SqlConnection con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("insert into files(fid,fname,fdata,ftype) values (@fid,@fname,@fdata,@ftype)",con);
cmd.Parameters.Add("@fid", SqlDbType.VarChar).Value=id;
cmd.Parameters.Add("@fname", SqlDbType.VarChar).Value = fName;
cmd.Parameters.Add("@fdata", SqlDbType.VarBinary).Value = fData;
cmd.Parameters.Add("@ftype", SqlDbType.VarChar).Value = fType;
con.Open();
int no = cmd.ExecuteNonQuery();
con.Close();
return no;

}



Step 9:Enter the code in the button2(download button_'s click event

protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(constring);
con.Open();
SqlCommand cmd1 = new SqlCommand("Select fdata from files where fid=1", con);
byte[] file = (byte[])cmd1.ExecuteScalar();

SqlCommand cmd = new SqlCommand("Select fname,ftype from files where fid=1", con);
SqlDataReader rdr = null;
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Label1.Text = rdr.GetValue(0).ToString();
Label2.Text = rdr.GetValue(1).ToString();
}
string location = "C:\\Documents and Settings\\Admin\\Desktop\\Downloaded\\" + Label1.Text;
FileStream fs = new FileStream(location, FileMode.Append, FileAccess.Write);
fs.Write(file, 0, file.Length);
fs.Close();
con.Close();
}


Step 10: Run the program. Enter a number in the textbox and select the file to be uploaded in the uploadfile tool. Press the upload button to save it in DB.

Step 11: Press the download button to download the last uploaded file.


Note : you can upload anykind of file through it(BMP,JPG,PDF,DOC etc)

Reply me whether can you upload it or not.

Last edited by Kirubhananth : 03-25-2008 at 11:54 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
The server rejected one or more recipient addresses. The server response was: 550 5.7 poornima ASP and ASP.NET Programming 7 02-29-2008 01:07 AM
Copy SQL data structures from one SQL server to another SQL server? arjkhanna Server Management 5 11-05-2007 03:57 AM
What is a Linked Server in sql server 2005? Archer Database Support 2 08-14-2007 04:24 AM
How to create and use server side cursors in SQL Server? oxygen Database Support 2 08-01-2007 08:54 AM
Where are SQL server users names and passwords are stored in sql server? H2o Database Support 2 07-19-2007 01:15 AM


All times are GMT -7. The time now is 07:19 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0