【问题标题】:Order by newid() - reader not reading properly按 newid() 排序 - 阅读器未正确阅读
【发布时间】:2015-05-15 15:01:33
【问题描述】:

我编写的代码从我的表中选择了一部随机电影并显示了星星,但由于某种原因,当我使用 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() 有关,但我不知道我问过的任何人都没有

【问题讨论】:

  • 如果您从 SSMS 执行 proc,或者在 VS 中调试并检查阅读器包含的内容,该怎么办。 proc 是否返回您期望的结果?
  • 如果您在 SSMS 中尝试SELECT top(10) * FROM [film] INNER JOIN billeder ON film.fk_bil_id = billeder.bill_id ORDER BY newid(),返回的数据是否正确?
  • return 0 没有意义。如果您没有要返回的内容,请不要返回 :) film_rating 可以为空吗?在这种情况下,如果值为NULLConvert.ToInt32(reader3["film_rating"]) 将抛出异常。我希望您的“电影 2”设置正确,但大多数电影都没有。
  • 它返回正确的数据,我做了一个

    ,它在我的转发器中获取评级的值,它只是读者,它可能是因为我的 Tabel colum film_rating 是十进制并且我没有正确转换它吗?

  • 我已经做了一个显示星星的地方,其中电影 = 我的查询字符串,就在我使用 newid() 时,它在阅读器中感到困惑

标签: c# sql sql-server reader


【解决方案1】:

一个朋友告诉我删除我的中继器并在阅读器中分配值并为帮助而工作 ty 这里是解决方案

 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多