0

テーブルからランダムな映画を選択してスターを表示するコードを作成しましたが、何らかの理由で、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()が、私が尋ねた人もわかりません

4

1 に答える 1

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();
         // Lav en reader som reader alt ud, billede title og rating
        // og så knyt det til dine forskellige ellementer som Lables og Image
        // Det bliver selvfølgelig et problem hvis du skal have flere end en, for så skal du bruger en repeater
        // og jeg ved ikke hvordan man kan kode bagved til at checke stjerne antallet

        if (reader3.Read())
        {
            int stardisplay = (int)Math.Round(Convert.ToDecimal(reader3["film_rating"]), 0);

            // display rating
            rating_panel_display.Visible = true;

            forside_img.ImageUrl = "images/plakater/"+ reader3["bill_sti"] +"";
            forside_h2.Text = "" + reader3["film_navn"] + "";
            forside_p.Text = "" + reader3["film_beskr"] + "";
            film_rating.Text = "" + reader3["film_rating"] + "";


            // 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();
于 2015-03-13T10:32:22.803 に答える