mercredi 8 avril 2015

Select a single column from multiple different tables and join into one final table

I'm working on a C# scoring application for one of my project classes and we have a database set up to handle multiple different inputs for competitor, tournaments, events, etc.


Relationships:


TournamentID

PK Base: Tournaments

PK Column: TournamentID

FK Base: Results

FK Column: TournamentID


EventID

PK Base: EventsTable

PK Column: EventID

FK Base: Results

FK Column: EventID


UserID

PK Base: Competitor

PK Column: UserID

FK Base: Results

FK Column: UserID


Placement

PK Base: Points

PK Column: Placement

FK Base: Results

FK Column: Placement


What I'm looking to do (as I'm told by our professor) is to SELECT a single column of data from the PK Base tables(Competitor, Tournament, Events, Points) and JOIN into one final table (Results). The columns to be selected would be Competitor.UserID, Tournament.TournamentID, EventsTable.EventID, Points.Placement.


We have an input screen that has the Tournament.Title in a combobox, EventsTable.Title in a combobox, Competitor.LastName, Competitor.FirstName in a listbox, Points.PlaceScore in a listbox and a textbox for Points.PlaceScore.


I'm looking for assistance in selecting the three (3) IDs from Competitor, Tournament and Events and the single (1) Placement from Points and insert them into their predefined columns in the Results table on the Submit button click event using the input screen .selecteditem and .indices for the corresponding listboxes and comboboxes.


From the button click, I've attempted to process one single input of data rather than all four at once and our results table does not receive the input of data.



{
string sql = null;
string connectionString = "Data Source= ";
using (SqlConnection cnn = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter();

sql = @"INSERT INTO Results (TournamentID)
SELECT TournamentID
FROM Tournaments
WHERE Title = '" + comboScoringTournamentSelect.SelectedItem + "'";

using (SqlCommand command = cnn.CreateCommand())
{
cnn.Close();
command.CommandText = sql;
cnn.Open();
MessageBox.Show("Record Inserted");

}
}
}

Aucun commentaire:

Enregistrer un commentaire