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 7808041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:02:12+00:00 2026-06-02T03:02:12+00:00

I am creating a Data Access Layer in C# for an SQL Server database

  • 0

I am creating a Data Access Layer in C# for an SQL Server database table. The data access layer contains a property for each column in the table, as well as methods to read and write the data from the database. It seems to make sense to have the read methods be instance based. The question I have is regarding handling the database generated primary key property getter/setter and the write method. As far as I know I have three options…

Option 1: Using a static method while only allowing a getter on the primary key would allow me to enforce writing all of the correct values into the database, but is unwieldy as a developer.

Option 2: Using and instance based write method would be more maintainable, but I am not sure how I would handle the get/set on the primary key and it I would probably have to implement some kind of validation of the instance prior to writing to the database.

Option 3: Something else, but I am wary of LINQ and drag/drop stuff, they have burned me before.

Is there a standard practice here? Maybe I just need a link to a solid tutorial?

  • 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-06-02T03:02:13+00:00Added an answer on June 2, 2026 at 3:02 am

    You might want to read up on active record patterns and some examples of them, and then implement your own class/classes.

    Here’s a rough sketch of a simple class that contains some basic concepts (below).

    Following this approach you can expand on the pattern to meet your needs. You might be OK with retrieving a record from the DB as an object, altering its values, then updating the record (Option2). Or if that is too much overhead, using a static method that directly updates the record in the database (Option1). For an insert, the database (SP/query) should validate the natural/unique key on the table if you need to, and probably return a specific value/code indicating a unique constraint error). For updates, the same check would need to be performed if allowing natural key fields to be updated.

    A lot of this depends on what functionality your application will allow for the specific table.

    I tend to prefer retrieving an object from the DB then altering values and saving, over static methods. For me, it’s easier to use from calling code and can handle arcane business logic inside the class easier.

    public class MyEntityClass
    {
        private int _isNew;
        private int _isDirty;
        private int _pkValue;
        private string _colValue;
    
        public MyEntityClass()
        {
            _isNew = true;
        }
    
        public int PKValue
        {
            get {return _pkValue;}
        }
    
        public string ColValue
        {
            get {return _colValue;}
    
            set
            {
                if (value != _colValue)
                {
                    _colValue = value;
                    _isDirty = true;
                }
            }
        }
    
        public void Load(int pkValue)
        {
            _pkValue = pkValue;
    
            //TODO: query database and set member vars based on results (_colVal)
    
            // if data found
            _isNew = false;
            _isDirty = false;
        }
    
        public void Save()
        {
            if (_isNew)
            {
                //TODO: insert record into DB
                //TODO: return DB generated PK ID value from SP/query, and set to _pkValue
            }
            else if (_isDirty)
            {
                //TODO: update record in DB
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating reusable Data Access Layer for SQL Server for some projects. I
I'm creating a Data Access Layer (DAL) to read/modify some tables stored in an
Creating My Windows Form Application and using ADO.Net as Data Access layer and SQL
I am creating a website and using Linq to SQl as a data access
I am creating a data access layer where I want to handle exceptions that
What's the recommended way of creating a Data Access layer in real-life ASP.NET MVC
I used Entity Framework 4.0 for creating my Data Access Layer. Then I found
I just started creating my data-access layer using LinqToSql. Everybody is talking about the
For an XML file I am creating I have data that contains a bullet
I am having a JSP page, where i am dynamically creating a Table data.

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.