我编写了从表中选择随机电影并显示星星的代码,但由于某种原因,当我使用 ORDER by newid() 时,我的星星可能无法显示,这是我的存储过程:
CREATE PROCEDURE [dbo].[select_forside]
AS
SELECT TOP(1) *
FROM [film]
INNER JOIN billeder ON film.fk_bil_id = billeder.bill_id
ORDER BY newid()
RETURN 0
和我的代码隐藏:
SqlConnection conn3 = new SqlConnection();
conn3.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString;
SqlCommand cmd3 = new SqlCommand();
cmd3.CommandType = CommandType.StoredProcedure;
cmd3.CommandText = "select_forside";
cmd3.Connection = conn3;
// cmd3.Parameters.Add("@login", SqlDbType.Decimal).Value = Session["login"];
conn3.Open();
SqlDataReader reader3 = cmd3.ExecuteReader();
if (reader3.Read())
{
int stardisplay = Convert.ToInt32(reader3["film_rating"]);
// display rating
rating_panel_display.Visible = true;
// hvis rating = 1
if (stardisplay == 1)
{
star6.Visible = true;
}
// hvis rating = 2
if (stardisplay == 2)
{
star6.Visible = true;
star7.Visible = true;
}
// hvis rating = 3
if (stardisplay == 3)
{
star6.Visible = true;
star7.Visible = true;
star8.Visible = true;
}
// hvis rating = 4
if (stardisplay == 4)
{
star6.Visible = true;
star7.Visible = true;
star8.Visible = true;
star9.Visible = true;
}
// hvis rating = 5
if (stardisplay == 5)
{
star6.Visible = true;
star7.Visible = true;
star8.Visible = true;
star9.Visible = true;
star10.Visible = true;
}
}
conn3.Close();
如果我说它WHERE film_id = 2
会显示电影 2 和正确数量的星星,它会起作用,order by newid()
但我不知道我问过的任何人也不知道