我是使用 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");