using System.Data.SqlClient; public IMethodResult ExecuteQuery(string query) { try { using (var connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand(query, connection); SqlDataReader reader = command.ExecuteReader(); // process data return MethodResult.Success(); } } catch (Exception ex) { return MethodResult.Error(ex.Message); } }
using System.Data.Entity; public IMethodResult AddToDatabase(MyEntity entity) { try { using (var dbContext = new MyDbContext()) { dbContext.MyEntities.Add(entity); dbContext.SaveChanges(); return MethodResult.Success(); } } catch (Exception ex) { return MethodResult.Error(ex.Message); } }In this example, we are using the IMethodResult interface to handle exceptions that can occur when adding a new entity to the database using Entity Framework. The MethodResult.Success() method will be called if the entity is successfully added to the database, and the MethodResult.Error() method will be called if an exception occurs during this process.