C# and Databases

Database Integration with C# (e.g., ADO.NET, Entity Framework)

C# provides robust database integration capabilities, allowing developers to connect, interact, and manipulate data from various database systems. Two popular approaches to database integration in C# are ADO.NET and Entity Framework.

Example: ADO.NET

using System;
using System.Data.SqlClient;

public class DataAccess
{
private const string ConnectionString = “Data Source=myServer;Initial Catalog=myDB;Integrated Security=True;”;

public void ReadData()
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
string query = “SELECT * FROM Customers”;
SqlCommand command = new SqlCommand(query, connection);

connection.Open();

using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine($”ID: {reader[“CustomerID”]}, Name: {reader[“ContactName”]}”);
}
}
}
}
}

In this example, we use ADO.NET to read data from the “Customers” table in a SQL Server database.

Example: Entity Framework

using System;
using System.Linq;

public class Customer
{
public int CustomerID { get; set; }
public string ContactName { get; set; }
}

public class DataAccess
{
public void ReadData()
{
using (var dbContext = new MyDbContext())
{
var customers = dbContext.Customers.ToList();

foreach (var customer in customers)
{
Console.WriteLine($”ID: {customer.CustomerID}, Name: {customer.ContactName}”);
}
}
}
}

In this example, we use Entity Framework to read data from the “Customers” table using a DbContext.

Performing CRUD Operations with Databases

C# enables developers to perform essential CRUD (Create, Read, Update, Delete) operations on databases with ease, making data manipulation a seamless process.

Example: Creating Data

using System;
using System.Data.SqlClient;

public class DataAccess
{
private const string ConnectionString = “Data Source=myServer;Initial Catalog=myDB;Integrated Security=True;”;

public void InsertData()
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
string query = “INSERT INTO Customers (ContactName, City) VALUES (@ContactName, @City)”;
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue(“@ContactName”, “John Doe”);
command.Parameters.AddWithValue(“@City”, “New York”);

connection.Open();
command.ExecuteNonQuery();
}
}
}

In this example, we use ADO.NET to insert data into the “Customers” table.

Example: Updating Data

using System;
using System.Linq;

public class DataAccess
{
public void UpdateData()
{
using (var dbContext = new MyDbContext())
{
var customer = dbContext.Customers.FirstOrDefault(c => c.CustomerID == 1);
if (customer != null)
{
customer.ContactName = “Jane Doe”;
dbContext.SaveChanges();
}
}
}
}

In this example, we use Entity Framework to update data in the “Customers” table.

Conclusion:

C# and databases form a powerful combination, enabling developers to seamlessly integrate and interact with databases. With tools like ADO.NET and Entity Framework, performing CRUD operations becomes a breeze, allowing for efficient data manipulation.

By leveraging C# database integration, developers can build robust, data-driven applications that excel in efficiency and scalability. So, embrace the capabilities of C# and databases, and unlock the true potential of your applications!

Leave a Comment