c# form, connect to database selecting random website
I am trying to connect to a remote mysql database in a form based C# application. I want it so a user can click a button and it will randomly select a website url from a online database, I then want the website to be displayed within a broswer within my application. I have got it so I can hard code a website URL and it will display the website in the browser control but want to take it a step further and pull a website URL from a database.
Here is my current code
webBrowser1.Url = new Uri("http://www.mintuz.co.uk");
And this is what I want to achieve.
Say I have 3 website URL's in a database
ID | Website URL
-----------------
1 | http://www.mintuz.co.uk
2 | http://www.google.com
3 | http://www.hello.com开发者_StackOverflow中文版
I want one of those URL's to be selected randomly then shown on the browser.
Thanks
This could help you out
http://forums.mysql.com/read.php?132,185266,194715
SELECT * FROM Table T JOIN (SELECT FLOOR(MAX(ID)*RAND()) AS ID FROM Table) AS x ON T.ID >= x.ID LIMIT 1;
Or
If you know that ID is sequential and you know the maximum ID, you can use Random class in .NET to select a random number and query the table using that ID.
http://msdn.microsoft.com/en-us/library/system.random.aspx
精彩评论