1

我是使用 ASP.NET 的新手,想知道如何从 sql 数据库中选择随机行,然后在单独的页面上显示 html 表中的字段。用户可以按下按钮,该按钮将从数据库中检索随机电影,然后在新页面的 html 表中显示电影详细信息。我不知道该怎么做,并且一直在尝试使用标签来显示数据。这是到目前为止的代码示例:

    private SqlConnection conn;
    protected void Page_Load(object sender, EventArgs e)
    {
            ConnectionStringSettings connString = ConfigurationManager.ConnectionStrings ["MovieAppConnectionString1"];
            conn = new SqlConnection(connString.ConnectionString);
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            conn.Open();
            string queryString = "SELECT TOP 1 * FROM Movie ORDER BY NEWID()";
            SqlCommand cmd = new SqlCommand(queryString, conn);
            {
                SqlDataReader reader = cmd.ExecuteReader();
                StringBuilder MyStringBuilder = new StringBuilder();
                while (reader.Read())
                {
                    Image2.Text = reader[2].ToString();
                    Label1.Text = reader[1].ToString();
                    Desc.Text = reader[3].ToString();
                    Direc.Text = reader[5].ToString();
                    Strs.Text = reader[7].ToString();
                    Rtime.Text = reader[4].ToString();
                    ImdbRt.Text = reader[8].ToString();
                }
            }
       }
        finally
        {
            conn.Close();
        }
        Server.Transfer("MovieSelected.aspx");
4

2 回答 2

1

更改您的 sql server 查询:

SELECT TOP 1 * FROM Movie ORDER BY NEWID()

SELECT TOP 1 * FROM Movie ORDER BY RAND()
于 2013-01-30T23:08:29.800 回答
0

在您的 aspx.cs 文件中:

int iLength = 0; 
int index = 0;

DataTable dt = new DataTable();
dt = SqlComm.SqlDataTable("SELECT * FROM Movie");

object obj = new object();
obj = SqlComm.SqlReturn("SELECT COUNT (yourTargetColumn) FROM yourTable");

if (obj != null)                    
     iLength = Convert.ToInt32(obj); 

string[] stringArray = new string[iLength];
for (index = 0; index < iLength; index++) 
{
     stringArray[index] = (string)dt.Rows[index]["yourTargetColumn"];                   
}
foreach (string strArray in stringArray)
{    
     Label yourLabel = new Label();
     PH.Controls.Add(yourLabel);
}

在您的 .aspx 文件中:

<asp:PlaceHolder ID="PH" runat="server"></asp:PlaceHolder>          

将一个类添加到名为“SqlComm.cs”的 App_Code 文件夹中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

public class SqlComm
{
   static string DatabaseConnectionString = "your connection string";

   public static object SqlReturn(string sql)
    {
        using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);                  
            object result = (object)cmd.ExecuteScalar();
            return result;           

        }
    }

   public static DataTable SqlDataTable(string sql)
   {
       using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
       {
           SqlCommand cmd = new SqlCommand(sql, conn);
           cmd.Connection.Open();
           DataTable TempTable = new DataTable();
           TempTable.Load(cmd.ExecuteReader());
           return TempTable;
       }
}

注意:不要忘记将 添加using System.Data.SqlClient到您的代码中。此外,您只需自定义SELECT命令即可获得所需的数据。

于 2013-02-01T16:07:44.843 回答