search
HomeBackend DevelopmentC#.Net TutorialIntroduction to practical examples of ADO.NET

Introduction to practical examples of ADO.NET

Jul 03, 2017 pm 04:59 PM
introducepractical

In order to give full play to the advantages of ADO.NET, it is not only necessary to have a comprehensive and in-depth understanding of the ADO.NET programming model, but it is also very important to summarize experience and skills in a timely manner. ADO has many years of practical experience, and ADO.NET provides richer and more powerful tools based on this. However, the design goal of ADO.NET is not to provide a plug-and-play tool after all, and it will not integrate all The programming work is simplified to the extent that it can be completed with just mouse clicks.
ADO.NET contains a large number of objects representing various logical entities in the data access model, among which the two objects of connection and transaction are the most important. The function of a connection is to establish a communication channel with the back-end database. Creating a connection object must be based on a specific .NET data provider. Transaction objects can be created on an existing connection object or by explicitly executing a BEGIN TRAN SQL statement. Although the theory is simple, in fact, there are many uncertain factors surrounding connections and transactions, and they have a crucial impact on the overall stability and efficiency of the application.

 How to save the connection string and protect sensitive information (such as passwords) that may be contained in the connection string? How to design a complete data access policy that takes into account security (i.e. authentication, authorization) without having too much impact on performance and scalability? If transactions are needed, how to implement and control transactions efficiently? Use automatic transactions or manual transactions? When using ADO.NET, these issues must be carefully considered.

1. Connection string, connection pool

Database connection is an important, limited, and expensive resource, so making good use of connection objects is the most basic requirement for any application. The key points of using database connections can be summarized as follows:

Pay attention to safety when saving connection strings.
The connection should be opened late and the connection should be closed early.
The connection string is the key to access the database. In addition to describing the data to be accessed, the connection string also contains proof of identity of why the user can access that data. User authentication is the most important factor in determining data access rights when performing database operations.

 1.1 Saving connection strings

Currently, hard-coded connection strings have the best performance because they are compiled directly into the application code. However, hard-coded strings affect the flexibility of the program, and the application must be recompiled once the connection string changes.

Saving the connection string externally increases flexibility, at the expense of additional overhead for accessing the external string. But in the vast majority of cases, the resulting performance overhead is negligible, and what you really need to worry about is security. For example, an attacker could modify or steal the connection string. Common ways to save the connection string to the external environment are: Configuration file, UDL file, Windows registry.

.NET framework configuration files are deployed in the form of plain text files and are easy to access. If the connection string contains a password, the text format will be the biggest drawback, because the password will be saved in clear text. You can consider introducing a dedicated encryption/decryption engine, but this part of the work needs to be done by the developers themselves.

UDL files are text files used by OLE DB providers, that is, SQL Server hosting providers do not support UDL files. UDL files also have the same security issues as the previous configuration files, and overall there are not many advantages.

Finally, the Windows registry can be used as a naturally secure storage place. The registry is a system knowledge base that stores key information. If combined with encryption technology, higher security can be achieved. The main disadvantages of using a registry are the hassle of deployment, the requirement to create registry keys (and possibly perform encryption) and to read data from the registry. Although the .NET Framework provides a set of encapsulated classes that call the underlying Win32 API, none of these classes provide encryption functions. The aspnet_setreg.exe tool can be used to create a registration key under HKEY_LOCAL_MACHINE to save the user name and password, for example: aspnet_setreg.exe -k "Software\MyData" -u:userID -p:password. This command will encrypt the specified user ID and password.

 1.2 Connection Pool Principle

The connection pool allows us to reuse existing connection objects through a buffer pool, avoiding the need to create a new object every time a connection object is used. After using the connection pool, only a small number of connection objects can meet the needs of a large number of clients.

Each connection pool is associated with an independent connection string and its transaction context. Each time a new connection is opened, the data provider attempts to match the specified connection string with the connection pool's string. If the match fails, the data provider creates a new connection and adds it to the connection pool. After the connection pool is created, it will not be dismantled unless the process ends. Some people think that this processing method will affect performance. In fact, it does not cost much to maintain an inactive or empty connection pool.

After the connection pool is created, the system will create some connection objects and add them to the connection pool until the rated minimum number of connection objects is reached. In the future, the system will create and add connection objects as needed until the maximum number of connection objects is reached. If there is no free connection object available when the program requests a connection object, and the number of objects in the connection pool has reached the upper limit, the request is placed in the queue, and once a connection is released back to the buffer pool, it is immediately taken out for use.

Avoid constructing connection strings programmatically. If the connection string is constructed by merging multiple input data, it is easy for injection attacks to take advantage of it. If user-entered data must be used, strict validation must be performed.

 1.3 Closing the connection

When closing a connection, the connection object is returned to the connection pool for reuse, but the actual database connection is not dismantled at this time. If connection pooling is disabled, the actual database connection is also closed. One point that must be emphasized here is that the connection object should be explicitly closed and returned to the connection pool after use. Do not rely on the garbage collector to release the connection. In fact, when the reference of the connection object goes out of the valid range, the connection is not necessarily closed - the function of the garbage collector is to tear down the .NET encapsulated object representing the physical connection, but this does not mean that the underlying The connection is also closed.

Calling the Close or Dispose method can release the connection back to the connection pool. The connection object will be deleted from the connection pool only when the lifetime ends or a serious error occurs.

 1.4 Connection Pooling and Security

If all data access operations of an application use the same connection string, the advantages of the connection pool will be maximized. However, this is an idealized situation and may conflict with other requirements of the application. For example, it would be difficult to enforce security controls at the database level if only a single connection string was used.

On the other hand, if each user is allowed to use his own connection string (that is, a separate database account is set for each user), there will inevitably be a large number of small connection pools, and many connections will not be reused at all. . Conventionally, the best solution to this type of problem is to find an appropriate compromise between the two extremes. We can set up a representative set of public accounts and modify the stored procedure to accept a parameter representing the user ID. The stored procedure performs different operations based on the incoming user ID.

2. Transaction Mode

Distributed enterprise applications are inseparable from transactions. There are two main ways to add transaction management functions to data access code: manual and automatic.

In the manual method, the programmer is responsible for writing all code that configures and uses the transaction mechanism. Automatic (or COM+) transactions add declarative attributes to .NET classes to specify the transactional characteristics of runtime objects. The automated approach facilitates configuring multiple components to run within the same transaction. Both transaction methods support local or distributed transactions, but the automatic transaction method greatly simplifies distributed transaction processing.

It must be noted that transactions are a very expensive operation, so you must think twice before deciding to use transactions. If you really need to use transactions, you should try to reduce the granularity of the transactions and reduce the locking time and locking scope of the database. For example, for SQL Server, a single SQL statement does not need to explicitly declare a transaction, SQL Server will automatically run each statement as an independent transaction. Manual local transactions are always much faster than other transactions because it does not need to involve DTC (Distributed Transaction Coordinator).

Manual transactions and automatic transactions should be regarded as two different and mutually exclusive technologies. If you want to perform transactional operations on a single database, give priority to manual transactions. When a single transaction spans multiple remote databases, or a single transaction involves multiple resource managers (for example, one database and one MSMQ resource manager), automatic transactions are given priority. Regardless, mixing the two transaction modes should be avoided as much as possible. If performance is not particularly important, consider using automatic transactions even for only one database operation, making the code cleaner (but slightly slower).

In short, to improve the quality of database access code, you must have an in-depth understanding of the ADO.NET object model and flexibly use various techniques according to the actual situation. ADO.NET is a public API. Various applications, whether it is Windows Forms applications, ASP pages or Web services, can access the database through ADO.NET; however, ADO.NET does not accept input and spit out results at the same time. A black box, but a toolbox made up of many tools.

The above is the detailed content of Introduction to practical examples of ADO.NET. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# .NET and the Future: Adapting to New TechnologiesC# .NET and the Future: Adapting to New TechnologiesApr 14, 2025 am 12:06 AM

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

Is C# .NET Right for You? Evaluating its ApplicabilityIs C# .NET Right for You? Evaluating its ApplicabilityApr 13, 2025 am 12:03 AM

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

C# Code within .NET: Exploring the Programming ProcessC# Code within .NET: Exploring the Programming ProcessApr 12, 2025 am 12:02 AM

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

C# .NET: Exploring Core Concepts and Programming FundamentalsC# .NET: Exploring Core Concepts and Programming FundamentalsApr 10, 2025 am 09:32 AM

C# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.

Testing C# .NET Applications: Unit, Integration, and End-to-End TestingTesting C# .NET Applications: Unit, Integration, and End-to-End TestingApr 09, 2025 am 12:04 AM

Testing strategies for C#.NET applications include unit testing, integration testing, and end-to-end testing. 1. Unit testing ensures that the minimum unit of the code works independently, using the MSTest, NUnit or xUnit framework. 2. Integrated tests verify the functions of multiple units combined, commonly used simulated data and external services. 3. End-to-end testing simulates the user's complete operation process, and Selenium is usually used for automated testing.

Advanced C# .NET Tutorial: Ace Your Next Senior Developer InterviewAdvanced C# .NET Tutorial: Ace Your Next Senior Developer InterviewApr 08, 2025 am 12:06 AM

Interview with C# senior developer requires mastering core knowledge such as asynchronous programming, LINQ, and internal working principles of .NET frameworks. 1. Asynchronous programming simplifies operations through async and await to improve application responsiveness. 2.LINQ operates data in SQL style and pay attention to performance. 3. The CLR of the NET framework manages memory, and garbage collection needs to be used with caution.

C# .NET Interview Questions & Answers: Level Up Your ExpertiseC# .NET Interview Questions & Answers: Level Up Your ExpertiseApr 07, 2025 am 12:01 AM

C#.NET interview questions and answers include basic knowledge, core concepts, and advanced usage. 1) Basic knowledge: C# is an object-oriented language developed by Microsoft and is mainly used in the .NET framework. 2) Core concepts: Delegation and events allow dynamic binding methods, and LINQ provides powerful query functions. 3) Advanced usage: Asynchronous programming improves responsiveness, and expression trees are used for dynamic code construction.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool