Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 531437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:18:04+00:00 2026-05-13T09:18:04+00:00

I want to create relationships in my SQL Server database using C#. I have

  • 0

I want to create relationships in my SQL Server database using C#.

I have this code:

   foreach (ForeignKey fk in table.ForeignKeys)
                {
                    if (!table.ForeignKeys.Contains("ProductMaterial"))
                }

                table.ForeignKeys.Add(
                     new ForeignKey(
                        new Table(database, 
                                  "ProductMaterial"), 
                                  "ProductMaterial" ));
                DataRelation relation = new DataRelation(
                        "ProductMaterial", new DataColumn(), )

“table” is from a Tables collection in the database, and the database comes from a databases collection in the server instance. I have the condition reversal because I want to add a relationship obviously where one does not exist so I am looking for the absence of an FK.

However, I also have the DataRelation line. Can I just add an FK and that’ll be the relationship? Or should I add both an FK and a datarelation object?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:18:04+00:00Added an answer on May 13, 2026 at 9:18 am

    First of all, just to be clear (and hopefully you already know this), this code will not create anything in the database… All you are affecting is the structure and relationships between the tables in the ADO.Net DataSet object, which exists only in memory in the process space you are running this code in.

    But having said that, adding the Foreign Key just establishes a constraint. This means that it prevents new records from being added to that dataTable with values in the FK field that do not exist in the other table. This is not the same as adding a relationship. A relationship estanlishes the Parent-Child thingy (redundant to call it a relationship here ) which allows other .Net objects like Grids etc, to render the data in a hierarchical manner.

    So yes, if you want both you need to add both.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 320k
  • Answers 320k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer After trying a few different librairies (suds, soaplib), I finally… May 14, 2026 at 12:25 am
  • Editorial Team
    Editorial Team added an answer I am not an expert, and I do not have… May 14, 2026 at 12:25 am
  • Editorial Team
    Editorial Team added an answer The scope of the variable is limited to the "SharedManager"… May 14, 2026 at 12:25 am

Related Questions

I'm working on a multi-user internet database-driven website with SQL Server 2008 / LinqToSQL
Just dipping my toes into Linq2sql project after years of rolling my own SQL
I am using SQL Server 2005. I have three tables - Users, Groups, and
A C# program I am working on will need to export data to one
I am new to LINQ. I just dragged all my database tables onto the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.