I want to randomly select an image from a database and view it on a aspx page
I have a database with images in it and now I want to rando开发者_运维技巧mly select an image from the database and view it on the aspx page.
I know how to read images from a database depending on the id.
I am using MS Sql and C# programming.
Any database has a way to select random records, have a look here to find your database: http://www.petefreitag.com/item/466.cfm
Select a random row with MySQL:
SELECT column FROM table
ORDER BY RAND()
LIMIT 1
Select a random row with PostgreSQL:
SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1
Select a random row with Microsoft SQL Server:
SELECT TOP 1 column FROM table
ORDER BY NEWID()
Select a random row with IBM DB2
SELECT column, RAND() as IDX
FROM table
ORDER BY IDX FETCH FIRST 1 ROWS ONLY
Select a random record with Oracle:
SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1
You just need to select a random record from your images table like so:
SELECT TOP 1 ... FROM Images
ORDER BY NEWID()
You can get max image id and after generate random number using this as maximus value like in this example:
int maxIdValue = MaxIdFromDatabase();
int randomId = new Random().Next(maxIdValue);
Image image = GetImage(randomId);
Can Help?
You're going to need a way to supply the image to the end user over HTTP. This can be any of several ways, such as creating your own http handler, to simply passing the image name as a query string parameter to an aspx page, then sending the image back (instead of html).
This Article will tell you how to serve the image, I leave the sql to select random images to the other answers.
精彩评论