简体   繁体   English

需要帮助找到良好的设计和架构

[英]Need help finding a good design and schema

I want to make an all in one query, or even a two step query that pulls from 3 tables. 我想要一个全部查询,甚至是从3个表中提取的两步查询。

Table #1: Games - this table holds a type of game and has a description of the game etc. 表#1:游戏-此表包含一种游戏,并包含游戏说明等。

Table #2: GameProfiles - this holds an id from the table 'Games', so they both have a GamesId column. 表格2:GameProfiles-包含表“ Games”中的ID,因此它们都有一个GameId列。 This table holds Games won, Games Lost, etc etc 该表包含赢得的游戏,丢失的游戏等

Table #3: Games_WhateverGame - this is not a specific table, there are multiple tables, for instance if I have a game BasketBall, there is going to be a seperate table for it called Games_BasketBall and it has custom columns depending on the game. 表#3:Games_WhateverGame-这不是一个特定的表,有多个表,例如,如果我有一个游戏BasketBall,将有一个单独的表称为Games_BasketBall,并且根据游戏具有自定义列。 For example, Basketball would have a column for rebounds. 例如,篮球会有一个篮板。 This table would have its own primary key id. 该表将具有其自己的主键ID。

I can easily pull the Games and GameProfiles together using an inner join on their common "GameId" column, but how do I make it so I can also pull 'Games_BasketBall' also in the same query, dynamically depending on the GamesId. 我可以使用公共“ GameId”列上的内部联接轻松地将Games和GameProfiles拉到一起,但是如何做到这一点,所以我也可以在同一查询中根据GameId动态地拉出“ Games_BasketBall”。 I may be structuring this wrong, so I am open to suggestions. 我可能是在构造这种错误,所以我愿意提出建议。 I just cant seem to think of a really fluid way of making this work correctly because each game will have different profile entities regardless so I want to make the relations to each table easy so I can pull everything in one query. 我只是想不出一种真正流畅的方法来正确完成这项工作,因为每个游戏无论如何都将具有不同的配置文件实体,因此我想简化与每个表的关系,以便可以在一个查询中提取所有内容。

This code has the query WITHOUT the relation on to Games_Basketball, I want to be able to pull it all into one reader so it has the information correct. 这段代码具有对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();
    }

For yours current shema I would go for something like: 对于您当前的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]

}

You could also use MARS (Mulitple Active Result Sets) to accomplish your goal. 您还可以使用MARS(多种活动结果集)来实现您的目标。 In MARS you use two sqldatareader at the same time. 在MARS中,您同时使用两个sqldatareader。

Here is a small sample: 这是一个小样本:

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

If you want to get all the information for all games at one time, it's not practical to do that in one query. 如果您想一次获取所有游戏的所有信息,那么在一个查询中这样做是不现实的。 And, you know, the user doesn't want to wait for you to do, say, 135 LEFT JOIN operations to get information from all the game "whatever" tables. 而且,您知道,用户不想等待您执行135个LEFT JOIN操作来从所有“任何游戏”表中获取信息。

So, even though it's possible, it's not practical, and you really probably don't want to do that anyway. 因此,即使有可能,但这也不实用,您可能真的不想这样做。 The user isn't likely to want to read through all that data, right? 用户不太可能想要通读所有数据,对吗?

Show just what the user needs, just when the user needs it. 仅在用户需要时显示用户需要。 If the user clicks on "basketball", then you can query a view that has already joined the two or three tables you need to display information about the "basketball" game. 如果用户单击“篮球”, 那么您可以查询已经加入了两个或三个表的视图,以显示有关“篮球”游戏的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM