What is the best way to communicate with the database through Datagrid in C# WPF between DataSet and Entity?

Mojtaba_Hakim 321 Reputation points
2023-01-07T20:19:39.27+00:00

I use C# WPF, I am programming an accounting program with a SQL Server database, in this accounting program, data is inserted, updated, and deleted by the user directly in a data grid, which contains a textbox and Combobox elements.

277138-ginsr.png

In an issue that I was very confused about, which method of connecting to the database and storing data through the data grid is better?

ADO.NET with DataSet and TableAdapter like this :

 private void LoadData()  
    {  
        nOTELBOOKDBDataSet = ((SIMPLE_MVVM.NOTELBOOKDBDataSet)(this.FindResource("nOTELBOOKDBDataSet")));  
        // Load data into the table PERSONEL. You can modify this code as needed.  
        nOTELBOOKDBDataSetPERSONELTableAdapter = new SIMPLE_MVVM.NOTELBOOKDBDataSetTableAdapters.PERSONELTableAdapter();  
        nOTELBOOKDBDataSetPERSONELTableAdapter.Fill(nOTELBOOKDBDataSet.PERSONEL);  
        System.Windows.Data.CollectionViewSource pERSONELViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("pERSONELViewSource")));  
        pERSONELViewSource.View.MoveCurrentToFirst();  
    }  

Entity Framework with SqlQuery

 public void LoadData()  
    {  
        using (DataModeling.Entities dbms = new DataModeling.Entities())  
        {  
            var RES = dbms.Database.SqlQuery<HUMANS_TBMODEL>("SELECT ID,NAME,TEL,REMARKS FROM PERSONEL").ToList();  
            foreach (var item in RES)  
            {  
                MY_ALL_HUMANS.Add(item);  
            }  
        }  
    }  

Considering ease of implementation and coding
Please guide me which is the best and easiest way?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,006 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
814 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 48,571 Reputation points Microsoft Vendor
    2023-01-09T09:42:40.733+00:00

    Hi, @Mojtaba_Hakim . Welcome Microsoft Q&A.

    1. ADO.NET entity is an ORM (object relational mapping) which creates a higher abstract object model over ADO.NET components.

    2.Entity Framework is a wrapper for ADO.NET(Entity framework also uses ADO.NETin "the basement".). So there is little difference in performance between the two.(maybe Entity Framework is a bit slower.)

    3.The main benefit of Entity framework is it auto-generates code for the Model (middle layer), Data Access Layer and mapping code, thus reducing a lot of development time.
    Entity framework helps you to map from database to objects. With ADO.NET you have to do that yourself.

    What you use is entirely up to your preference. It depends on how you program it how fast it is.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Lex Li (Microsoft) 5,657 Reputation points Microsoft Employee
    2023-01-07T21:10:08.907+00:00

    Even though you said "ease of implementation and coding" the answer still depends on yourself because you didn't show enough context on how big your application is going to be.

    Simple apps (school homework, apps for small companies) can live very well with just ADO.NET (even enterprises in early 2000 did that). Certain people do like seeing SQL statements so that they know what happens under the hood.

    While entity framework (EF) based code might look better for larger scale applications, you do need to spend more time learning the internals of EF. Otherwise, you don't know what happens behind the scenes and you cannot troubleshoot issues efficiently.

    Just my 5 cents.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.