Reputation: 11
I have the following 2 tables in a database:
Movies
(id, title, year) which is the parent of table LanguageLanguage
(movie_id, language) ----movie_id references Movies.id
I wrote the following code but I still have to do the update/delete part and to add a new record in the child table
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace lab1_v2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string connectionString = "Server=DESKTOP-T33VBF8;Database=lab1SGBD_v6;Integrated Security=true";
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Open connection
connection.Open();
MessageBox.Show("State of the connection: " + connection.State.ToString());
// Create the data set
DataSet dataset = new DataSet();
// Create the two SQL Data Adapters for parent and child tables.
SqlDataAdapter parentAdapter = new SqlDataAdapter("SELECT * FROM Movies;", connection);
SqlDataAdapter childAdapter = new SqlDataAdapter("SELECT * FROM Languages;", connection);
// Create and populate the parent DataTable and the child DataTable
parentAdapter.Fill(dataset, "Movies");
childAdapter.Fill(dataset, "Languages");
// Create the two BindingSources for parent and child DataTable
BindingSource parentBS = new BindingSource();
BindingSource childBS = new BindingSource();
// Show all the records from parent DataTable in dataGridViewParent
parentBS.DataSource = dataset.Tables["Movies"];
dataGridViewParent.DataSource = parentBS;
// Create and add in the DataSet the DataRelation between parent DataTable and child DataTable
DataColumn parentPK = dataset.Tables["Movies"].Columns["id"];
DataColumn childFK = dataset.Tables["Languages"].Columns["movie_id"];
DataRelation relation = new DataRelation("fk_parent_child", parentPK, childFK);
dataset.Relations.Add(relation);
// Show in dataGridView the child records which are from the parent record selected
childBS.DataSource = parentBS;
childBS.DataMember = "fk_parent_child";
dataGridViewChild.DataSource = childBS;
}
}
catch (Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}
}
}
Upvotes: 0
Views: 551
Reputation: 74605
You're making your life so difficult and complicated. Visual Studio can write literally every line youve written there, and more, in a couple of seconds.
SELECT * FROM movies
, name it, save itYes, that's it. There is no code for you to write. VS has written it all, literally everything in your question and more, including the insert, update and delete queries and they're all wired up- just press the save icon in the top right. Because the Languages bindingsource is bound to the movies bindingsource the grids work in a related fashion; whatever is the currently selected movie row forces a filter to only the languages relevant to that movie. Adding a new languages row relates it to the movie
Later when you want your app to be more useful than just "download the whole database into the DataSet" you can go back to the DataSet and right click the tableadapter and add another query, perhaps like SELECT * FROM movies WHERE title LIKE @title
, call it FillByTitle and use it in code like moviesTableAdapter.FillByTitle(someDataSet.Movies, "Jaws%")
.
Remember that you have to fill all the related rows you want to use yourself too. For this reason I seldom make the first query in a tableadapter be a simple "select * from" - I always add a WHERE primarykeyid = @id
then I can loop through movies filling each language id, or if performance is a concern I'll add another query to Languages like SELECT * FROM languages WHERE ID IN (SELECT DISTINCT languageid FROM movies WHERE title LIKE @title)
so I can fill movies by Jaws% and then fill Languages by Jaws% too
Upvotes: 1