C# module 2
C# module 2
The Command object in ADO.NET is used to execute SQL queries and stored procedures
against a database. It facilitates communication between an application and the database.
2.Interpret how a DataAdapter acts as a bridge between a DataSet and the database.
In ADO.NET, the DataAdapter serves as an intermediary between a DataSet and a
database, enabling smooth data retrieval and updates without requiring a continuous
connection to the database.
Roles of DataAdapter
1. Fetching Data from the Database
o Uses SQL queries (SELECT) to fill a DataSet.
o Works with DataReader internally for efficient data transfer.
2. Updating the Database
o Syncs changes made in the DataSet back to the database using INSERT,
UPDATE, and DELETE commands.
3. Managing Disconnected Data Access
• Allows applications to modify data locally without requiring a constant database
connection.
• Improves performance and scalability.
class AdoNetDemo
{
static void Main()
{
string connectionString = "your_connection_string_here";
class DatabaseConnectionDemo
{
static void Main()
{
// Define connection string
string connectionString =
"Server=your_server_name;Database=your_database_name;User
Id=your_username;Password=your_password;";
Database Layer Represents the actual database (SQL Server, MySQL, Oracle)
Entity Data Model (EDM) Defines the schema of entities mapped to tables
Migration & Change Tracking Helps version control and automatic schema updates
✅ Definition: A class inherits from another class, which itself inherits from a third class.
✅ Purpose: Establishes a hierarchical relationship, allowing deeper extension of
functionality.
13.Evaluate the steps involved in establishing connections to SQL Server, OLEDB, and
ODBC databases using ADO.NET.
1. Connecting to SQL Server Database Using SqlConnection
✅ Provider: System.Data.SqlClient
✅ Best Used For: Microsoft SQL Server
Steps
1. Import the System.Data.SqlClient namespace.
2. Define the connection string.
3. Create an SqlConnection object.
4. Open the connection.
5. Execute commands (INSERT, SELECT, etc.).
6. Close the connection.
2. Connecting to an OLEDB Database Using OleDbConnection
✅ Provider: System.Data.OleDb
✅ Best Used For: Access, Excel, and other OLE DB-compatible databases
Steps
1. Import the System.Data.OleDb namespace.
2. Define the connection string.
3. Create an OleDbConnection object.
4. Open the connection.
5. Execute commands (INSERT, SELECT, etc.).
6. Close the connection.
3. Connecting to an ODBC Database Using OdbcConnection
✅ Provider: System.Data.Odbc
✅ Best Used For: Generic databases that support ODBC drivers
Steps
1. Import the System.Data.Odbc namespace.
2. Define the connection string.
3. Create an OdbcConnection object.
4. Open the connection.
5. Execute commands (INSERT, SELECT, etc.).
6. Close the connection.
conn.Close(); // Closes the connection manually (if not using 'using') ✔ Alternative: The
using statement automatically closes the connection.
15.Compare connected and disconnect architecture in ADO.NET.
1. Connected Architecture
✅ Best For:
✔ Fetching real-time data from large databases.
✔ Fast execution with minimal memory usage.
✔ Ideal for reporting and quick queries.
❌ Limitations:
⛔ Requires a constant connection, which can overload the database.
⛔ Can only read forward, making repeated access difficult
2. Disconnected Architecture
✅ Definition: Retrieves data from the database, stores it in memory (DataSet), and
disconnects from the database, allowing offline data manipulation.
✅ Component: Uses SqlDataAdapter and DataSet for batch processing.
✅ Best For:
✔ Offline data processing without needing a constant connection.
✔ Batch updates (modifying multiple records at once).
✔ Storing multiple tables in memory, allowing complex operations.
❌ Limitations:
⛔ Requires more memory since it holds multiple records in RAM.
⛔ Slower performance compared to DataReader when handling huge datasets.
class Program
{
static void Main()
{
string connectionString = "Server=your_server_name;Database=CompanyDB;User
Id=your_username;Password=your_password;";
class UserRegistration
{
static void Main()
{
string connectionString =
"Server=your_server_name;Database=your_database_name;User
Id=your_username;Password=your_password;";
Console.WriteLine("Enter Username:");
string username = Console.ReadLine();
Console.WriteLine("Enter Password:");
string password = Console.ReadLine();
Console.WriteLine("Confirm Password:");
string confirmPassword = Console.ReadLine();
if (password != confirmPassword)
{
Console.WriteLine("Passwords do not match! Please try again.");
return;
}
Console.WriteLine("Enter Email:");
string email = Console.ReadLine();
18.How do you establish a connection to a SQL Server database using ADO.NET in C#?
ADO.NET provides the SqlConnection class to create and manage connections to a SQL
Server database.
Steps to Establish a Connection
1 Import the ADO.NET namespace (System.Data.SqlClient).
2 Define the connection string with server and authentication details.
3 Create an instance of SqlConnection and pass the connection string.
4 Open the connection using conn.Open().
5 Perform database operations (e.g., querying, inserting data).
6 Close the connection using conn.Close() or using statement
21.What are the differences between connecting to a database using OLEDB and ODBC?
Provide sample connection strings for each.
Provider
Supports COM-based data access Uses driver-based architecture
Type
🔹 Used when only one value needs to be retrieved (e.g., count, sum, max, min).
🔹 Returns an object, so casting is required.
✅ Best For: Queries that return a single result (e.g., total employees, highest salary).
⛔ Limitation: Cannot handle multiple rows.
23.What is the ADO.NET Entity Framework? How does it simplify database access
compared to raw ADO.NET? Explain.
ADO.NET Entity Framework
ADO.NET Entity Framework provides an abstraction over database operations.
✅ Developers interact with objects and classes instead of writing complex SQL queries.
✅ The framework automatically translates C# objects into relational database
operations.
✅ Supports multiple databases such as SQL Server, MySQL, PostgreSQL, and SQLite.
ADO.NET Entity Framework Simplifies Database Access