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

  • Home
  • SEARCH
  • 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 7501149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:26:29+00:00 2026-05-29T20:26:29+00:00

I have an abstract parent class(Product) and two child classes that inherit from it(Book,Software).

  • 0

I have an abstract parent class(Product) and two child classes that inherit from it(Book,Software). I need to insert the common data(the parent class attributes) into the Products table and the specific attributes of the child classes into the BookProducts and SoftwareProducts tables.

this is the implementation i have come up with, i was wondering if anyone can provid a better approach.

public static class Database
{


    public static void SaveBook(Book book)
    {
        //save the product to products table and return the ID of the new row
        int ProductID = saveProduct(book);

       SqlConnection connection = DatabaseConnection.GetConnection();


            string insertBookStatement =
            "INSERT BookProducts" +
            "(ProductID,Author)" +
            "VALUES (@ProductID,@Author)";

        SqlCommand insertBookCommand = new SqlCommand(insertBookStatement, connection);

        insertBookCommand.Parameters.AddWithValue("@ProductID",ProductID);
        insertBookCommand.Parameters.AddWithValue("@Author",book.Author );


        try
        {
            connection.Open();
            insertBookCommand.ExecuteNonQuery();

        }
        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            connection.Close();
        }



    }

    public static void SaveSoftware(Software software)

    {
        int ProductID = saveProduct(software);

        SqlConnection connection = DatabaseConnection.GetConnection();


        string insertSoftwareStatement =
        "INSERT SoftwareProducts" +
        "(ProductID,VersionNumber)" +
        "VALUES (@ProductID,@VersionNumber)";

        SqlCommand insertSoftwareCommand = new SqlCommand(insertSoftwareStatement, connection);

        insertSoftwareCommand.Parameters.AddWithValue("@ProductID", ProductID);
        insertSoftwareCommand.Parameters.AddWithValue("@VersionNumber", software.VersionNumber);


        try
        {
            connection.Open();
            insertSoftwareCommand.ExecuteNonQuery();

        }
        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            connection.Close();
        }
    }


    private static int saveProduct(Product p)
    {
        int ProductID;
        SqlConnection connection = DatabaseConnection.GetConnection();

        string insertProductStatement =
            "INSERT Products" +
            "(Code,Description,Category,Price)" +
            "VALUES (@code,@Description,@Category,@Price)";

        SqlCommand insertProductCommand = new SqlCommand(insertProductStatement, connection);
        insertProductCommand.Parameters.AddWithValue("@Code", p.Code);
        insertProductCommand.Parameters.AddWithValue("@Description", p.ShortDescription);
        insertProductCommand.Parameters.AddWithValue("@Category", p.Category);
        insertProductCommand.Parameters.AddWithValue("@Price", p.Price);

        try
        {
            connection.Open();
            insertProductCommand.ExecuteNonQuery();
            string selectStatement = "SELECT IDENT_CURRENT('Products') FROM Products";
            SqlCommand selectCommand = new SqlCommand(selectStatement, connection);
            ProductID = Convert.ToInt16(selectCommand.ExecuteScalar());



        }
        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            connection.Close();
        }


        return ProductID;
    }


}
  • 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-29T20:26:34+00:00Added an answer on May 29, 2026 at 8:26 pm

    The Data Access Object pattern will help you.

    First of all, you’ll want one BaseProduct table, one Software table and one Book table. The SoftwareProduct DAO will call base.Store() (this is pretty pseudo-cody, but it’ll get the idea across) to store all the base-class’ fields in BaseProduct, while then storing itself to the correct table with a BaseProductId as foreign key.

    Database normalization is usually good, and this particular case could probably be used as a text-book example of why, as it shows the connection between OCP/SRP in the domain layer and the normalized tables in the database 🙂

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

Sidebar

Related Questions

Suppose we have 2 classes, Child, and the class from which it inherits, Parent.
I have a abstract Parent class that has multiple children. I'd like the child
Suppose I have two classes deriving from a third abstract class: public abstract class
I have an abstract parent class Item , from which different types of items
If I have a parent-child that defines some method .foo() like this: class Parent
Let's say I have an abstract parent class called shape, and that there are
hi I have a parent class that is Abstract Employee and I have childrens
I would like to have my abstract parent class have a method that would
In my c# project, I have two abstract generic parent classes which then have
I have the following structure: abstract class Parent {} class Child : Parent {

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.