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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| we can store the Image as Binary Data... |
| |||
| 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. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |