我有一个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
表中。我不太确定如何做到这一点我是一名学生在课堂上学习并且不确定我是否正在使用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"));
}
答案 0 :(得分:0)
目前还不清楚你是在寻找确切的sql来做这件事还是只是解释如何做到这一点。
在这些类型的情况下,当你需要一个Assigned / Unassigned列表时,我会有2个sql方法,它们会接受TeamID并返回那些人。
我假设你有3个表格的东西......玩家,团队,玩家团队(多对多)。 PlayerTeams只有一个playerid和一个teamid。您需要通过@teamid将teamid作为参数传递给每个调用。
因此,那些Assigned的典型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调用的代码,这很容易在这里找到,所以我不会进入。
答案 1 :(得分:0)
选择不在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并插入值