簡體   English   中英

QueryString檢索數據庫信息

[英]QueryString to retrieve database information

我正在一個項目中,我有一個用於主機游戲的游戲庫。 我在Visual Studio中使用ASP.NET。

現在我有一個表的數據庫gamesconsolescategories (類型), categories_games (包含gameidcategory id指定游戲類型是什么)和consoles_games (同樣的事情,但對於控制台)。

我正在使用DataList顯示游戲(名稱和圖像),如下所示:

<asp:DataList ID="DataList1" runat="server" BackColor="White"
   BorderColor="White" BorderStyle="None" BorderWidth="0px"
   CellPadding="10" CellSpacing="2" Font-Names="Verdana"
   Font-Size="Small" GridLines="Both" RepeatColumns="5"
   RepeatDirection="Horizontal" Width="850px">
      <FooterStyle BackColor="White" ForeColor="#8C4510" />
      <HeaderStyle BackColor="White" Font-Bold="true" Font-Size="Large"
         ForeColor="White" HorizontalAlign="Center"
         VerticalAlign="Middle" />
            <ItemStyle BackColor="White" ForeColor="Black"
               BorderWidth="0px" />
  <ItemTemplate>
    <asp:Image ID="imgGame" runat="server" Width="128px"
    Height="159px" ImageUrl='<%# Bind("uimg", "{0}") %>' />
    <br />
    <asp:Label ID="lblTitle" runat="server"
    Text='<%# Bind("uname") %>'></asp:Label>
    <br />
    <br />
  </ItemTemplate>
</asp:DataList>

后面的代碼:

protected void BindData()
{
    DataSet ds = new DataSet();
    con.Open();
    SqlCommand cmd = new SqlCommand(cmdstr, con);
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    adp.Fill(ds);
    DataList1.DataSource = ds.Tables[0];
    DataList1.DataBind();
}

例如,獲取所有PS4游戲的SQL語句如下所示:

SELECT 
    games.uname, games.uimg 
FROM 
    games, consoles, consoles_games 
WHERE 
    consoles.consoleid = consoles_games.consoleid 
    AND games.uid = consoles_games.uid 
    AND consoles.consoleid = 2 
ORDER BY 
    games.uname

但是,我想使用查詢字符串來顯示游戲,所以最終得到這樣的結果:

mywebsite.com/PS4.aspx?consoleid=2

如果有人知道我該怎么做,並且仍然像現在一樣在DataList中顯示游戲,那就太好了。 我一直在尋找任何好的教程,但似乎找不到任何可以幫助我解決我的情況的東西。

首先,當用戶導航到ps4游戲視圖(他在鏈接上之前在某處單擊)時,必須創建querystring。 然后在導航頁面上顯示ps 4的游戲列表,您應該捕獲該查詢字符串並根據傳遞的ID或其他參數進行搜索。

看一下: 如何在C#中為URL建立查詢字符串?

了解如何為網址構建查詢字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM