Solved

Storing Images in a filesystem and the location in the SQL database.

Posted on 2008-10-17
13
212 Views
Last Modified: 2012-05-05
I was just wondering if there was a good code example out there in ASP.NET for me to get started on this. I googled it and I couldn't find a good one. I just found a bunch of articles stating that you SHOULD do it, but not HOW to do it.
0
Comment
Question by:GravitaZ24
  • 7
  • 5
13 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22747626
>Storing Images in a filesystem and the location in the SQL database.
actually, it would depend on the size of the files... large files -> file system. small files -> db is fine.
sql server 2008 -> has a "best of breed": FILESTREAM data type:
http://blogs.msdn.com/manisblog/archive/2007/10/21/filestream-data-type-sql-server-2008.aspx
or this one (free registration):
http://www.sqlservercentral.com/articles/SQL+Server+2008/64088/

and here some c# code with explanations:
http://blogs.msdn.com/zowens/archive/2008/03/03/sql-server-2008-filestream-and-wpf-mediaelement-part-2-writing-filestream-data.aspx

hope this helps
0
 

Author Comment

by:GravitaZ24
ID: 22750328
Most of the images i will be dealing with would be fairly large scans. Plus I'm not sure if the IT people are ready to upgrade to SQL server 2008 yet.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22752638
You would just need a VARCHAR column to store the full path to the file, and then process normal stored procedures for the CRUD stuff.
0
 

Author Comment

by:GravitaZ24
ID: 22757551
Thanks the learned one, is there any way you can point me in the direction of some example code, that is exactly what I want to do
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22757859
Are you looking for a way, using ADO.NET, to achieve this, or using an SqlDataSource?  Choosing the direction that you would want to go in will help me find example code.
0
 

Author Comment

by:GravitaZ24
ID: 22757899
oh sorry, SQL Datasource, I have an area with where I want the user to upload forms that they will scan
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 22758082
For an SqlDataSource, I would think that something like this might help:

Using Parameters with the SqlDataSource Control
http://msdn.microsoft.com/en-us/library/z72eefad.aspx
0
 

Author Comment

by:GravitaZ24
ID: 22758236
I'm sorry, i'm kind of new to this, Maybe I am using ADO.NET and not realizing it. That is using the code behind right? Do you have an example using ADO.NET. I'm don't think that that previous link you showed would help me much.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22766132
That previous link is HTML that you use to configure an SqlDataSource.  If you are not using one of those, can you show me what you are using, so I can stop guessing?
0
 

Author Comment

by:GravitaZ24
ID: 22766161
Hi, Thanks for the response.
I got this so far, is there a way you can edit this to handle multiple images?

using System;

using System.IO;

using System.Collections.Generic;

using System.Configuration;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Web.Services.Protocols;
 

public partial class _Default : System.Web.UI.Page

{

        

protected void Button1_Click(object sender, EventArgs e)

{

   int intResult = 0;

   FileUpload file1 = (FileUpload)FormView1.FindControl("FileUpload1");

   string strPath = ("c:\\Images\\" + Path.GetFileName(file1.PostedFile.FileName));

   SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=Prop_Change;Integrated Security=SSPI");

   SqlCommand com = new SqlCommand("Insert into PropChanges_AQMain(EN_OrigDocumentIMG) values(@EN_OrigDocumentIMG)", con);

   com.Parameters.Add("@EN_OrigDocumentIMG", strPath);

   con.Open();

   intResult = Convert.ToInt32(com.ExecuteNonQuery());

   if (intResult != 0) {

       file1.PostedFile.SaveAs(strPath);

       Response.Write("Record Inserted.");
 

}
 

    }

}

Open in new window

0
 

Author Comment

by:GravitaZ24
ID: 22766166
Or should I start a new question?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 22766736
Ok, so you are using ADO.NET with an SqlCommand and an SqlConnection.   You can use those same classes to get the data from the SQL Server table.   All you would need to do is change the INSERT statement to a SELECT statement, and use SqlCommand.ExecuteDataReader, and use the SqlDataReader to load a DataTable.

Example:

Convert a DataReader to DataTable in ASP.NET
http://www.dotnetcurry.com/ShowArticle.aspx?ID=143&AspxAutoDetectCookieSupport=1
0
 

Author Closing Comment

by:GravitaZ24
ID: 31507294
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now