Solved

Linq to SQL Query for multiple rows

Posted on 2008-11-01
6
2,414 Views
Last Modified: 2013-12-17
Hello,

We have this data entry page where user enters data for the following fields

BookName
ex. asp.net programming

ISBN
ex. 12345

Authors (enter comma seperated)
author1, author2, author3


When the page is submitted, we want the above data to be stored in two tables

table1 - bookmaster
columns - bookid (identity), bookname, isbn
ex.  234, asp.net programming, 12345

table2 - bookauthors
columns - id (identity), bookid, authorname
ex.
1, 234, author1
2, 234, author2
3, 234, author3


Please let me know how to write the query for the above in Linq to SQL (C#).
Note. User enters authors name comma seperated.

Thanks in advance.

0
Comment
Question by:sunseshasai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 22859110
Hi sunseshasai;

What is the structure of the tables in the database?

Fernando
0
 

Author Comment

by:sunseshasai
ID: 22859522
Hello FernandoSoto:
Here it is,

CREATE TABLE [dbo].[BookMaster](
      [BookID] [int] NOT NULL,
      [BookName] [varchar](100) NOT NULL,
      [ISBN] [varchar](50) NULL,
 CONSTRAINT [PK_BookMaster] PRIMARY KEY CLUSTERED
(
      [BookID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


CREATE TABLE [dbo].[BookAuthors](
      [ID] [int] IDENTITY(1,1) NOT NULL,
      [BookID] [int] NOT NULL,
      [AuthorName] [varchar](100) NOT NULL,
 CONSTRAINT [PK_BookAuthors] PRIMARY KEY CLUSTERED
(
      [ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


ALTER TABLE [dbo].[BookAuthors]  WITH CHECK ADD  CONSTRAINT [FK_BookAuthors_BookMaster] FOREIGN KEY([BookID])
REFERENCES [dbo].[BookMaster] ([BookID])
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 22860413
Hi sunseshasai;

This will do what you need.

Fernando

using System.Data.Linq;
 
 
// Instantiate the DataContext
BooksDataContext bookDB = new BooksDataContext();
// Get the BookMaster table which has a link to Authors
Table<BookMaster> books = bookDB.GetTable<BookMaster>();
// Separate the authors on the comma
String[] authors = txtAuthors.Text.Split(',');
// Instantiate a new book to be inserted
BookMaster newBook = new BookMaster();
// Fill in the fields of the BookMaster table using text boxes, can be any string variable
newBook.BookName = txtBookName.Text.Trim();
newBook.ISBN = txtISBN.Text.Trim();
// Create a Author record for each author and add it to the Entity Set of the newBook
foreach (String author in authors)
{               
    BookAuthors bAuthor = new BookAuthors();
    bAuthor.AuthorName = author.Trim();
    newBook.BookAuthors.Add(bAuthor);
}
// Insert book and authors into the table and submit on changes
books.InsertOnSubmit(newBook);
// Submit changes to the database
bookDB.SubmitChanges();

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 22868089
Did this answer your question?
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question