using System.Data.SqlClient; var connectionString = ""; var connection = new SqlConnection(connectionString); try { connection.Open(); Console.WriteLine("Connection Successful!"); } catch (Exception ex) { Console.WriteLine("Connection Failed: " + ex.Message); } finally { connection.Close(); }
using System.Data; using MySql.Data.MySqlClient; var connectionString = "In both examples, we use the package library C# DataAccess Add to connect to the specified database and execute SQL queries. The specific package library used to implement these examples is not specified in the code snippets, but it could be ADO.NET, Dapper, Entity Framework, etc."; var connection = new MySqlConnection(connectionString); try { connection.Open(); var query = "INSERT INTO Customers (Name, Address, City) VALUES (@Name, @Address, @City)"; var command = new MySqlCommand(query, connection); command.Parameters.AddWithValue("@Name", "John Doe"); command.Parameters.AddWithValue("@Address", "123 Main St"); command.Parameters.AddWithValue("@City", "New York"); var result = command.ExecuteNonQuery(); Console.WriteLine(result + " row(s) inserted."); } catch (Exception ex) { Console.WriteLine("Insert Failed: " + ex.Message); } finally { connection.Close(); }