CREATE PROCEDURE [dbo].[GetRandomDownload] AS DECLARE @Rows int SELECT @Rows = max(DownloadID) FROM Downloads get_random: SELECT DownloadID, Title, Description, Downloads FROM Downloads WHERE DownloadID = (SELECT CAST((RAND() * @Rows) AS int) + 1) IF (@@rowcount = 0) GOTO get_random GO
SELECT CAST((RAND() * @Rows) AS int + 1
SELECT DownloadID, Title, Description, Downloads FROM Downloads WHERE DownloadID = (SELECT CAST((RAND() * @Rows) AS int) + 1)
SqlConnection conn = new SqlConnection("Data Source=localhost;Database=MyDatabase;Integrated Security=SSPI"); SqlCommand selectCommand = new SqlCommand("GetRandomDownload", conn); conn.Open(); SqlDataReader reader = selectCommand.ExecuteReader(); // do you stuff here conn.Close();
public class RandomRow : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; private void Page_Load(object sender, System.EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=localhost;Database=AcademicNet;Integrated Security=SSPI"); SqlCommand selectCommand = new SqlCommand("GetRandomDownload", conn); conn.Open(); SqlDataReader reader = selectCommand.ExecuteReader(); while (true) { if (reader.Read()) { string link = " a href='Download.aspx?DownloadID=" + reader["DownloadID"].ToString() + "'>" + reader["Title"].ToString() + ""; this.Label1.Text = link; break; } else { reader.NextResult(); } } conn.Close(); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion }
Build Your Own ASP.NET Website Using C# & VB.NET