Unit4.pptx Data Access File System Data

priyankajadhav6600 49 views 7 slides Oct 07, 2024
Slide 1
Slide 1 of 7
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7

About This Presentation

4.1 File System Data
4.2.XML
4.3 Databases and ADO.NET
4.4 Data Binding


Slide Content

DATA ACCESS Dot net

File System D ata In .NET, data access typically refers to how applications interact with databases or other data sources. . NET provides several technologies and libraries to handle data access efficiently.

1.ADO.NET (Active Data Objects for .NET) ADO.NET is a low-level, connection-oriented, and highly efficient data access framework. It provides direct interaction with databases through SQL queries. You can use ADO.NET for: Connecting to a database. Executing SQL queries or stored procedures. Handling data using DataReader , DataSet , or DataTable . Example: using ( SqlConnection connection = new SqlConnection ( connectionString )) { connection.Open (); SqlCommand command = new SqlCommand ("SELECT * FROM Users", connection); SqlDataReader reader = command.ExecuteReader (); while ( reader.Read ()) { Console.WriteLine (reader["Name"]. ToString ()); } }

2. Entity Framework (EF Core) Entity Framework Core (EF Core) is an object-relational mapper (ORM) that allows developers to interact with databases using strongly-typed objects, rather than raw SQL queries. It supports LINQ (Language Integrated Query), which makes data queries more readable. EF Core handles the generation of SQL queries, simplifying data access and reducing boilerplate code . Example : using ( var context = new ApplicationDbContext ()) { var users = context.Users.Where (u => u.IsActive ). ToList (); foreach ( var user in users) { Console.WriteLine ( user.Name ); } } LINQ Query: context.Users.Where (u => u.IsActive ). ToList (); is a LINQ query that fetches all users where IsActive is true . ToList (): Converts the result of the query into a list of User objects.

3. Dapper Dapper is a lightweight ORM for .NET. It is known for its performance and simplicity. Unlike Entity Framework, it doesn’t generate SQL for you, but it provides methods to map results of SQL queries to objects in an easy way. Example: using ( var connection = new SqlConnection ( connectionString )) { var users = connection.Query <User>("SELECT * FROM Users WHERE IsActive = 1"). ToList (); foreach ( var user in users) { Console.WriteLine ( user.Name ); } } The SQL query "SELECT * FROM Users WHERE IsActive = 1" retrieves all rows from the Users table where the IsActive column is set to 1 (i.e., true for active users ). ToList (): Converts the results from the query into a List<User>, which is a collection of User objects that can be iterated over.

Data Providers in ADO.NET: SQL Server : System.Data.SqlClient or Microsoft.Data.SqlClient for .NET Core. MySQL : MySql.Data.MySqlClient . Oracle : Oracle.ManagedDataAccess . Summary of Key Libraries and Frameworks: ADO.NET : Low-level, highly efficient data access. Entity Framework Core : Full-featured ORM with LINQ support. Dapper : Lightweight ORM with manual SQL control. LINQ to SQL : Lightweight ORM for simpler applications. Stored Procedures : Executable from EF Core or ADO.NET.

Data binding Data binding in .NET is a powerful way to connect the user interface to underlying data models or collections . Steps for Simple Data Binding in .NET : Fetch Data from a Database: Use ADO.NET to retrieve data from a database using SQL queries . Bind Data to UI Controls: Use simple data binding to link the fetched data to controls, so changes in data will reflect in the UI and vice versa . Key Components: Data Source: The database from which data is retrieved (e.g., SQL Server, Oracle, MySQL ). UI Controls: Windows Forms controls like TextBox , DataGridView , ComboBox , etc., that will display or modify data. Binding : The process of connecting a control’s property (like Text or ItemsSource ) to a data source.
Tags