簡體   English   中英

在SQL Server中將C#記錄插入具有多個外鍵的表中

[英]Inserting a C# record into a table with multiple foreign keys in SQL Server

我有一個Players表,一個Teams表和一個TeamPlayers表。 我想將不在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表中的代碼。 我不太確定該怎么做。我是一名學生,試圖在全班學習,但不確定我是否使用C#來做到這一點。 任何人都可以幫忙。 以下是下拉列表的代碼

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"));
    }

目前尚不清楚,如果您要查找執行此操作的確切sql或只是如何執行此操作的說明。

在這些類型的情況下,當您需要分配的列表/未分配的列表時,我將有2個sql方法將采用TeamID並返回這些人員。

我假設您有3張牌桌...玩家,球隊,玩家團隊(很多)。 PlayerTeam只會有一個玩家ID和一個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)

如果您需要更多有關如何更好地執行SQ​​L調用的代碼,可以在這里輕松找到它,那么我就不用再贅述了。

要選擇不在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 

當您處理Submit事件時,您將獲得intPlayerID和intTeamID並插入值

暫無
暫無

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

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