本文介绍了sql查询以检索特定主键的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public partial class EnterResults : Form
{
SqlConnection dbConnection;
SqlCommand dbCommand;
DataSet dsResults;
SqlDataAdapter dbAdapter;
public EnterResults()
{
InitializeComponent();
}
private void EnterResults_Load(object sender, EventArgs e)
{
dbConnection = new SqlConnection("Initial Catalog = Rugby_World_Cup; Data Source = localhost; Integrated Security= True");
dbCommand = new SqlCommand("SELECT * FROM Game", dbConnection);
dbAdapter = new SqlDataAdapter(dbCommand);
dsResults = new DataSet();
dbAdapter.Fill(dsResults, "Result");
cboGameID.DataSource = dsResults.Tables["Result"];
cboGameID.DisplayMember = "GameId";
cboGameID.SelectedIndex = -1;
}
private void btnEnter_Click(object sender, EventArgs e)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
dbCommand = new SqlCommand("EnterGame", dbConnection);
dbCommand.CommandType = CommandType.StoredProcedure;
dbCommand.Parameters.AddWithValue("@GameID", cboGameID.SelectedValue.ToString());
dbCommand.Parameters.AddWithValue("@CountryID", cboCountryID.SelectedValue.ToString());
dbCommand.Parameters.AddWithValue("@Tries", txtTries.Text);
dbCommand.Parameters.AddWithValue("@Penalty", txtPenalties.Text);
dbCommand.Parameters.AddWithValue("@DropG", txtDropGoals.Text);
dbCommand.Parameters.AddWithValue("@Points", txtPoints.Text);
if (dbCommand.ExecuteNonQuery() > 0)
{
MessageBox.Show("Results where Entered");
}
cboGameID.SelectedIndex = -1;
cboCountryID.SelectedIndex = -1;
txtDropGoals.Clear();
txtPenalties.Clear();
txtPoints.Clear();
txtTries.Clear();
cboGameID.Focus();
}
private void cboGameID_SelectedIndexChanged(object sender, EventArgs e)
{
dbCommand = new SqlCommand("Select * from game", dbConnection);
dbCommand.Parameters.AddWithValue("@ID",cboGameID.SelectedValue.ToString());
dbAdapter = new SqlDataAdapter(dbCommand);
dsResults = new DataSet();
dbAdapter.Fill(dsResults, "enter");
cboCountryID.DataSource = dsResults.Tables["enter"];
cboCountryID.DisplayMember = "CountryId";
cboCountryID.SelectedIndex = -1;
}
}
}
推荐答案
var GamenName = from game in dbContext.Games
where game.CountryId == cboCountryID.SelectedIndex.ToString()
select game.name;
这篇关于sql查询以检索特定主键的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!