Inserting Text File into Database - asp.net

I have visual studio 2010 with sql express. Im trying to create a asp website that will display a file from a database, and need to insert a textfile or pdf into the database table, can this be done using the visual interface, all the intructions i have found is only using the sql insert command?
Thanks

No you can't do this. Basic UI interface means you can type it in, you can't type in an array of byte.
So that means code, it's not hard, but I would recommend you consider storing the a url / filename in the table and then putting the file on the file system instead of as a blob in the table.
Files in database blobs does have some benefits for local deployment, in that you don't have to worry about some muppet making them inaccessible. Server side though that is far less of a worry. As long as you back up database and file system and come up with a reasonable directory and file naming structure its much less of a cost than bloating a database with a load of stuff you can't use in a query, not to mention it tends to make a fair mess of volume (mdb) file.
Oh and there are persistant rumours that MS are going to drop blobs over 8000 bytes as well.

Related

Peforming DML on a table by taking Excel file as an input

I am writing a PLSQL procedure that takes input as an excel file through front end and using that excel input the procedure inserts , updates or deletes the records present in an existing table . Can anyone show me the approach for this?
If that "Excel" file has to be really in native XLS(X) format, a simple option - if you want to stay within Oracle boundaries - is an Apex application which offers a data loading wizard. Takes 4 pages to create it (don't worry, Apex Wizard creates almost everything for you). Once the loading is over, a (stored) procedure can do the rest of processing (you'd call it by pushing a button).
Alternatively, if you save contents of that file as a CSV file, you can load it with SQL*Loader, utility ran at the operating system command prompt. You'd have to create a control file (no wizard to do that, I'm afraid). This approach probably isn't convenient for end users (who's going to type anything at the command prompt?) so you'd have to create some kind of an application to do that.
Or, CSV again, but this time used as an external table. This approach requires the file to be located in a directory accessible by the database server (most frequently, the directory is located on that computer, and you most frequently don't want to allow access to anyone to it). Its advantage is that you can access the CSV file directly from (PL/)SQL, fetch data from it, perform various adjustments etc.
If you're capable of writing programs that aren't part of the Oracle niche (I'm not), go for it (but I can't suggest anything; someone else might).

Should we store data in database?

i'm asp.net beginner and currently working in "upload download file" project with asp.net and vb.net as code behind language (like skydrive's web).
what i'm want ask is about upload file in server, must we store path file, size, accessed or created date into database? as we know we can use directory listing in system.io.
Thanks for your help.
You definetly want to store the path of the file. You want a way to find the file ;) Maybe later you will have multiple servers, replication or other fancy things.
For the rest, it depends a bit on the type of website. If it's going to get high traffic then store it in the database, this will limit the number of IO call (very slow). Also, it'll be a lot easier to handle sorting and queries. (sort by date, pull only the read onyl files, ...).
Database will also help if you want to show history or statistique.
You can save file in some directory and can save path of that file in database. You can also store size and created date of that file in DB. But storing a file in DB is a bit difficult. Rather than save file in Directory and save path of that file in DB
you could store the file information in a database to built some extra features like "avoid storing duplicate files", because you are having a faster search in the database! if you search the filesystem always a recursive function call get started

concurrent reading and writing image files (asp.net, but applies to most web languages)

I have a .jpg file which represents the current image from a webcam. User's will be downloading this file at an interval of once a second. Because there could be dozens of users reading it, this could be dozens of times a second (which is normal for any web server).
Problem is, this image is updated by a 3rd party application also once a second which "spiders" my local networks webcam portal image. This is so we can build our webcams into our current administration panel.
The problem I am already finding is ASP.net sometimes gets an error it can not access the file because it is open for write permissions by the bot. Likewise, the bot can not access it because IIS is feeding it to the user.
The bot uses io.streamwriter to save the data to the file, and my script uses Response.WriteFile to send the file to the script. (I need to use an actual ASP.net page with a JPG content-type that feeds the file to make sure only users with a active session can view the JPG).
My question is what is the best practices for this? I know why it's happening but what is the best resolution for this? Would storing as a BLOB in a database maybe be smarter since databases are created for concurrent read/writing already? Is there an easier way of doing this with a file I have not thought of yet?
Thanks in advance,
Anthony Greco
Using a BLOB will work if the readers use SNAPSHOT isolation model (SQL Server 2005 and up). See Download and Upload images from SQL Server via ASP.Net MVC for how to stream an image from a BLOB, and see Understanding Row Versioning-Based Isolation Levels for a lecture on SNAPSHOT.
But using a BLOB may be overkill, you could get away with something much simpler. For instance, if you only have one ASP.Net process, then you could have a global volatile variable for the current file name. The writer writes the JPG into a new file, and then updates the global 'current' file name with an Interlocked.CompareExchange operation (it has to be Compare because a newer writer might actually finish faster, outrun a previous writer, and you want to preserve the latest update). There are still some issues left to solve (find out the file name at startup, clean up old files etc) but they are all fairly ease to solve.
If you have a farm of servers, or multiple ASP.Net processes serving the site, then things could get complicated. I would still do a rotating file name and do a try-and-error approach (try to respond with newest file, fall back to previous older one if conflict is detected).
You could get the bot to write the data to a different filename and then do a delete and rename to the filename being served by ASP.Net. This should reduce the file lock time down to the time for a delete and rename to occur. To clarify:
ASP.Net serving image from "webcam.jpg"
bot writes image data to "temp.jpg"
when last image byte written, bot deletes "webcam.jpg" and renames "temp.jpg" to "webcam.jpg"
ASP.Net should check "webcam.jpg" exists, if not wait 10ms (or suitable small increment) and check again.

Storing and retrieving images in sql server 2005 in asp.net via c#

How do I store and retrieve images in sql server 2005 in asp.net with c#
You may store them as binary data.
You may write a module (derived from IHttpModule), register it for RequestBegin event.
When request come you may retrieve data from database, store it on disk with requested file name and
this image will automatically be returned by built-in IIS handler (which is written in native code and works very fast).
Here is a project that does just the thing. All you need to do is move the storing and retrieving logic into your ASP .Net application.
It is not recommended.
Best practice is storing meta-data (here meta-data is you image file name) inside database and sotre them on file-system (for example c:\images). Then when you need an image you can ask database for its name and retreive it from file-system.
This helps you to have a compact database and maintenance became easy. Handling large databases backup/restore is a headache.

storing image in Db using asp.net

Im developing art gallery which needs 1000s of images to be uploaded so inserting images in DB won't be feasible. so i want to give link of folder inside db where ultimately images should get stored. plz help.
so inserting images in DB won't be
feasible
Why? Works very nicely. Especially with 2008 where you then dump them into a file system store again transparently.
I have a datas store where I store about 500gb of binary information all stored within the database. No issue at all.
Actually if you are using SQL 2008 you can use the FileStream mechanism, which actually stores the images as files on disk, but can be returned in sql results, so the data is still relational.
But answer aside, there's nothing wrong with storing 1000's of images in the database. But worst case, just store the string link if you really want.. its just that process is a little archaic.
It all depends on the DB that you're using I suppose (you didn't mentioned which you're using)
You can always store a relative path to the image file.
For example you might store images at UNC location of: \\YOUR_SERVER\myApplication\images\image1.png
-> the relative path would just be \myApplication\images\image1.png
(By storing just the relative path to the file, you're isolating the possiblity that you might change the server location in future.)
If you are using Sql Server 2008, then checkout the FileStream feature. More background info here.
One of my sites stores all user uploaded images in sql server and it works great. The only problem I can see if you are using shared hosting where you don't get a large database. Otherwise I think its a great option.

Resources