IDbHelper dbHelper = new DbHelper(); IDbConnection conn = dbHelper.CreateConnection("connectionString");
IDbHelper dbHelper = new DbHelper(); IDbConnection conn = dbHelper.CreateConnection("connectionString"); IDbCommand cmd = dbHelper.CreateCommand(conn, "INSERT INTO customers (name, email) VALUES (@name, @email)"); cmd.Parameters.Add(dbHelper.CreateParameter("name", "John")); cmd.Parameters.Add(dbHelper.CreateParameter("email", "[email protected]")); cmd.ExecuteNonQuery();
IDbHelper dbHelper = new DbHelper(); IDbConnection conn = dbHelper.CreateConnection("connectionString"); IDbCommand cmd = dbHelper.CreateCommand(conn, "SELECT * FROM customers WHERE id = @id"); cmd.Parameters.Add(dbHelper.CreateParameter("id", 1)); IDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Console.WriteLine("ID: {0}, Name: {1}, Email: {2}", reader["id"], reader["name"], reader["email"]); }This code creates a new database connection and executes an SQL SELECT statement to retrieve customer records from the "customers" table where the ID is 1. It loops through the result set and prints the ID, name, and email of each record to the console. Overall, IDbHelper provides a convenient and consistent way to interact with a database in C#. It allows developers to write simpler and more maintainable database code by abstracting away low-level details of database connection, command, and result set handling.