【发布时间】: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"));
}
【问题讨论】:
-
如果您能与我们分享minimal reproducible example,那就太好了。
标签: c# asp.net sql-server database