簡體   English   中英

需要幫助找到良好的設計和架構

[英]Need help finding a good design and schema

我想要一個全部查詢,甚至是從3個表中提取的兩步查詢。

表#1:游戲-此表包含一種游戲,並包含游戲說明等。

表格2:GameProfiles-包含表“ Games”中的ID,因此它們都有一個GameId列。 該表包含贏得的游戲,丟失的游戲等

表#3:Games_WhateverGame-這不是一個特定的表,有多個表,例如,如果我有一個游戲BasketBall,將有一個單獨的表稱為Games_BasketBall,並且根據游戲具有自定義列。 例如,籃球會有一個籃板。 該表將具有其自己的主鍵ID。

我可以使用公共“ GameId”列上的內部聯接輕松地將Games和GameProfiles拉到一起,但是如何做到這一點,所以我也可以在同一查詢中根據GameId動態地拉出“ Games_BasketBall”。 我可能是在構造這種錯誤,所以我願意提出建議。 我只是想不出一種真正流暢的方法來正確完成這項工作,因為每個游戲無論如何都將具有不同的配置文件實體,因此我想簡化與每個表的關系,以便可以在一個查詢中提取所有內容。

這段代碼具有對Games_Basketball的查詢而沒有該關系,我希望能夠將其全部放入一個閱讀器中,以便它具有正確的信息。

 using (SqlConnection myConnection = new SqlConnection(myConnectionString))
    {

        myConnection.Open();
        String selectSql = "SELECT * FROM aspnet_GameProfiles INNER JOIN aspnet_Games ON aspnet_GameProfiles.GameId = aspnet_Games.GameId WHERE UserId = @UserId";

        SqlCommand myCommand = new SqlCommand(selectSql, myConnection);
        myCommand.Parameters.AddWithValue("@GameProfile", UserId);


        reader = myCommand.ExecuteReader();
        gameTable.DataSource = reader;
        gameTable.DataBind();
        myConnection.Close();
    }

對於您當前的shema,我會選擇:

public enum GameType { Basketball, Snooker, ... }

void BindGameData(GameType gameType)
{

[ sql connection code ]

StringBuilder sb = new StringBuilder();
// those constant parts should be stored outside in config file
sb.append("SELECT * FROM aspnet_GameProfiles INNER JOIN aspnet_Games ON aspnet_GameProfiles.GameId = aspnet_Games.GameId "); 
sb.append("INNER JOIN ");
sb.append("aspnet_" + gameType.toString()); // adopt to yours naming pattern
sb.append("ON aspnet_Games.GameId = ");
sb.append("aspnet_" + gameType.toString() + ".GameId");
sb.append("WHERE UserId = @UserId");

String selectSql = sb.toString();

[rest of yours sql connection code]

}

您還可以使用MARS(多種活動結果集)來實現您的目標。 在MARS中,您同時使用兩個sqldatareader。

這是一個小樣本:

  SqlDataReader rdrone = null;
  SqlDataReader rdrtwo = null;
  string connectionstring = "server=sugandha;initial catalog = Employeedetail;uid = sa;pwd= sa";MultipleActiveResultSets = true;
  SqlConnection con = new SqlConnection(connectionstring);
  SqlCommand cmdone = new SqlCommand("select * from Employee", con);
  SqlCommand cmdtwo = new SqlCommand("select * from Employeedept", con);
  con.Open();
  rdrone = cmdone.ExecuteReader();
  DataTable dtone = new DataTable();
  dtone.Load(rdrone);
  dataGridView1.DataSource = dtone;
  rdrtwo = cmdtwo.ExecuteReader();
  DataTable dttwo = new DataTable();
  dttwo.Load(rdrtwo);
  dataGridView2.DataSource = dttwo;
  con.Close();

如果您想一次獲取所有游戲的所有信息,那么在一個查詢中這樣做是不現實的。 而且,您知道,用戶不想等待您執行135個LEFT JOIN操作來從所有“任何游戲”表中獲取信息。

因此,即使有可能,但這也不實用,您可能真的不想這樣做。 用戶不太可能想要通讀所有數據,對嗎?

僅在用戶需要時顯示用戶需要。 如果用戶單擊“籃球”, 那么您可以查詢已經加入了兩個或三個表的視圖,以顯示有關“籃球”游戲的信息。

暫無
暫無

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

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