【问题标题】:Inserting a C# record into a table with multiple foreign keys in SQL Server在 SQL Server 中将 C# 记录插入具有多个外键的表中
【发布时间】:2018-03-22 20:27:41
【问题描述】:

我有一个Players 表、一个Teams 表和一个TeamPlayers 表。我想将不在TeamPlayers 表中的玩家分配给已经存在的团队。我有一个显示TeamPlayers 表中的球队的下拉列表和一个显示不在TeamPlayers 表中的球员的下拉列表。

这是TeamPlayers 表的结构:

CREATE TABLE TTeamPlayers
(
     intTeamPlayerID INTEGER NOT NULL,
     intTeamID       INTEGER NOT NULL,
     intPlayerID     INTEGER NOT NULL,

     CONSTRAINT TTeamPlayers_PK PRIMARY KEY (intTeamPlayerID)
)

在我的提交按钮中,我将有代码将未分配的Player 添加到TeamPlayers 表中。我不太确定如何做到这一点任何人都可以帮忙。下面是下拉列表的代码

private void PopulateDropDowns()
{
        SqlConnection con = new SqlConnection("Data Source=itd2");

        // Select Query.
        string cmdText = "SELECT * FROM vPlayersThatsUnassigned";

        // Providing information to SQL command object about which query to 
        // execute and from where to get database connection information.
        SqlCommand cmd = new SqlCommand(cmdText, con);

        //To check current state of the connection object. If it is closed open the connection
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }

        //ddlAgeGroup.DataSource = cmd.ExecuteReader();
        ddlPlayers.DataTextField = "Players";
        ddlPlayers.DataValueField = "intPlayerID";
        ddlPlayers.DataBind();

        con.Close();

        ddlPlayers.Items.Insert(0, new ListItem("--Select an A Player--", "0"))
}

private void PopulateTeams()
{
        SqlConnection con = new SqlConnection("Data Source=itd2");

        // Select Query.
        string cmdText = "SELECT * FROM vTeamsAssigned";

        // Providing information to SQL command object about which query to 
        // execute and from where to get database connection information.
        SqlCommand cmd = new SqlCommand(cmdText, con);

        //To check current state of the connection object. If it is closed open the connection
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }

        ddlTeams.DataTextField = "strTeam";
        ddlTeams.DataValueField = "intTeamID";
        ddlTeams.DataBind();

        con.Close();

        ddlTeams.Items.Insert(0, new ListItem("--Select A Team --", "0"));
    }

【问题讨论】:

标签: c# asp.net sql-server database


【解决方案1】:

不清楚您是在寻找确切的 sql 来执行此操作,还是只是对如何执行此操作的说明。

在这些类型的情况下,当您需要一个已分配/未分配的列表时,我将有 2 个 sql 方法来获取 TeamID 并返回这些人。

我假设您有 3 张桌子,类似这样的东西……玩家、团队、玩家团队(多对多)。 PlayerTeams 将只有一个 playerid 和一个 teamid。您需要通过 @teamid 将 teamid 作为参数传递给每个调用。

所以,对于那些被分配的人来说,典型的 sql 看起来像这样......

Select * from Players p inner join PlayerTeams pt on pt.playerid = p.playerid where pt.teamid = @teamid

现在,要获得不在团队中的球员有点多。

Select * from Players where playerid not in (select playerid from playerteams where teamid = @teamid)

然后要添加某人,您只需将其插入到 playerteam 表中

Insert into playerteams (playerid, teamid) values (@playerid, @teamid)

如果您需要更多关于如何很好地进行 SQL 调用的代码,这很容易在此处找到,因此我将不再赘述。

【讨论】:

    【解决方案2】:

    要选择不在 PlayerTeams 中的玩家:

    select p.*
    from Players p
    left join TeamPlayers tp on p.intPlayerID = tp.intPlayerID 
    Where tp.intPlayerID  is null /*there is no record in TeamPlayers */
    

    要选择 TeamPlayers 表中的团队:

    select t.*
    from Teams t
    left join TeamPlayers tp on p.intTeamID = tp.intTeamID 
    

    当你处理提交事件时,你会得到 intPlayerID 和 intTeamID 并插入值

    【讨论】:

      猜你喜欢
      • 2013-05-26
      • 2014-10-09
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      相关资源
      最近更新 更多