K
K
Kiram2019-08-01 06:23:47
Database
Kiram, 2019-08-01 06:23:47

What is the correct way to connect to the database in C#?

Good afternoon everyone.
I am writing a desktop application. I connect at first a DB to the project through "data sources". And then I drag the table onto the form (previously setting up the display in DBGRID or through label and textbox), as a result, non-visual and visual components are automatically created on the form and some code is inserted. Is it right to make a connection in complex databases or choose another way? Where can you find an answer to this question?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
eRKa, 2019-08-01
@Kiram

Anything that does the job is right. Everything that allows you to maintain and scale the code later is even more correct.
In business projects, no one, of course, writes code for the database through the constructor. Most often, for mapping from the database, they use some kind of ORM ( EF , Dapper ), write models with their hands, which will then be mapped to database tables, prescribe their relationships.

V
Vladimir Korotenko, 2019-11-16
@firedragon

Usually use ConnectionString and ConnectionStringBuilder
The connection string is stored somewhere in the appSerrings or connectionStrings config file.
Further, depending on the application, somehow it is transferred to the ORM or directly to the driver.
If you need to dynamically assemble it, then output the interface for the assembly.
In ASP Net, WebConfig Transformation, net core are used, they get by with config substitution.
In addition, in an enterprise I often use a multi-tier architecture:
Pesentation layer - displaying results
BL layer - application logic
DB layer - database logic
But often BL works in the database itself. Triggers, checks, table types, geography, encryption

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question