Damien Sullivan

Damien Sullivan

  • NA
  • 26
  • 8.9k

INSERTING into SQL Server Table using Joins

Feb 2 2017 12:48 PM
Hi,
 
Here is what I am trying to do:
 
I want to create a Photo Gallery for Students.
 
Each student can have many Photo Collections.
Each Photo Collection contains a number of images.
 
So each Photo Collection will need to be linked to a Student, and each Image will need to be linked to a Photo Collection.
 
I am able to display Photo Collections successfully, based on each student.
 
I am also able to INSERT into Students, Images and Photo_Collection, but I aml unable to successfully INSERT into the Photo_Collection_Images table.
 
Can anyone help me how to insert into this table? Thanks a lot. 
 
Here is my C#:
  1.   public void InsertStudentImages(string filename, string description, string collectionName, string studentName)  
  2.     {  
  3.         string cmdText = @"DECLARE @Student_Id INT;  
  4. DECLARE @Photo_Collection_Id INT;  
  5. DECLARE @Student_Image_Id INT;  
  6.   
  7. INSERT INTO dbo.Students(Name)  
  8. VALUES (@StudentName)  
  9.   
  10. --Get the last identity  
  11. SET @Student_Id = SCOPE_IDENTITY()  
  12.   
  13. INSERT INTO dbo.Student_Images (Student_Id, Filename, Description)   
  14. VALUES (@Student_Id, @Filename, @Description);  
  15.   
  16. SET @Student_Image_Id = SCOPE_IDENTITY()   
  17.   
  18. INSERT INTO dbo.Photo_Collection (Student_Id, Name)   
  19. VALUES (@Student_Id, @CollectionName);  
  20.   
  21. SET @Photo_Collection_Id = SCOPE_IDENTITY()  
  22.   
  23. INSERT INTO dbo.Photo_Collection_Images (Photo_Collection_Id, Student_Image_Id)  
  24. VALUES (@Photo_Collection_Id, @Student_Image_Id);";  
  25.   
  26.         SqlCommand cmd = new SqlCommand(cmdText, con);  
  27.         cmd.Parameters.AddWithValue("@Filename", filename);  
  28.         cmd.Parameters.AddWithValue("@Description", description);  
  29.         cmd.Parameters.AddWithValue("@CollectionName", collectionName);  
  30.         cmd.Parameters.AddWithValue("@StudentName", studentName);  
  31.         if (con.State == ConnectionState.Closed)  
  32.         {  
  33.             con.Open();  
  34.         }  
  35.         cmd.ExecuteNonQuery();  
  36.         con.Close();  
  37.     }  
Here are my SQL Server tables:
  1. CREATE TABLE [dbo].[Students] (  
  2.     [Id]   INT           IDENTITY (1, 1) NOT NULL,  
  3.     [Name] NVARCHAR (50) NULL,  
  4.     CONSTRAINT [Students.Id.PrimaryKey] PRIMARY KEY CLUSTERED ([Id] ASC)  
  5. );  
  6.   
  7. CREATE TABLE [dbo].[Student_Images] (  
  8.     [Id]          INT            IDENTITY (1, 1) NOT NULL,  
  9.     [Student_Id]  INT            NOT NULL,  
  10.     [Filename]    NVARCHAR (250) NULL,  
  11.     [Description] NVARCHAR (250) NULL,  
  12.     CONSTRAINT [Student_Images.Id.PrimaryKey] PRIMARY KEY CLUSTERED ([Id] ASC),  
  13.     CONSTRAINT [Student_Images.to.Student] FOREIGN KEY ([Student_Id]) REFERENCES [dbo].[Students] ([Id])  
  14. );  
  15.   
  16. CREATE TABLE [dbo].[Photo_Collection] (  
  17.     [Id]         INT            IDENTITY (1, 1) NOT NULL,  
  18.     [Student_Id] INT            NOT NULL,  
  19.     [Name]       NVARCHAR (250) NULL,  
  20.     CONSTRAINT [Photo_Collection.Id.PrimaryKey] PRIMARY KEY CLUSTERED ([Id] ASC),  
  21.     CONSTRAINT [Photo_Collection.to.Student] FOREIGN KEY ([Student_Id]) REFERENCES [dbo].[Students] ([Id])  
  22. );  
  23.   
  24. CREATE TABLE [dbo].[Photo_Collection_Images] (  
  25.     [Photo_Collection_Id] INT NOT NULL,  
  26.     [Student_Image_Id]    INT NOT NULL,  
  27.     CONSTRAINT [Photo_Collection_Images.to.Student_Images] FOREIGN KEY ([Student_Image_Id]) REFERENCES [dbo].[Student_Images] ([Id]),  
  28.     CONSTRAINT [Photo_Collection_Images.to.Photo_Collection] FOREIGN KEY ([Photo_Collection_Id]) REFERENCES [dbo].[Photo_Collection] ([Id])  
  29. );  

Answers (3)