using System.Data.OleDb; OleDbCommand cmd = new OleDbCommand("SELECT COUNT(*) FROM Customers", connection); int count = (int)cmd.ExecuteScalar(); Console.WriteLine("Total customers: " + count);
using System.Data.OleDb; OleDbCommand cmd = new OleDbCommand("SELECT MAX(Price) FROM Products", connection); decimal maxPrice = (decimal)cmd.ExecuteScalar(); Console.WriteLine("Maximum price: " + maxPrice);
using System.Data.OleDb; OleDbCommand cmd = new OleDbCommand("SELECT ProductName FROM Products WHERE ProductID = @ID", connection); cmd.Parameters.AddWithValue("@ID", 5); string productName = (string)cmd.ExecuteScalar(); Console.WriteLine("Product name: " + productName);This code retrieves the name of a product with a specific ID using a parameterized query. The parameter is passed using the AddWithValue method. In summary, System.Data.OleDb is a package library in C# used for accessing databases using the OleDb driver. The OleDbCommand.ExecuteScalar method is a useful tool for retrieving a single value from the database and is often used in conjunction with parameterized queries.