Connecting to SQL Server and Oracle

ADO.NET connections for major databases

Posted by Rodrigo Castro on December 20, 2024

ADO.NET supports many relational databases, including SQL Server and Oracle.

🗄️ SQL Server Connection

  • Namespace: System.Data.SqlClient
  • NuGet: Usually built-in
1
2
3
4
5
6
string connString = "Server=localhost;Database=TestDb;User Id=sa;Password=YourPassword;";
using (SqlConnection conn = new SqlConnection(connString))
{
    conn.Open();
    // Do stuff...
}

🛢️ Oracle Connection

1
2
3
4
5
6
string connString = "User Id=myuser;Password=mypass;Data Source=localhost:1521/ORCL;";
using (OracleConnection conn = new OracleConnection(connString))
{
    conn.Open();
    // Do stuff...
}

⚠️ Notes

  • Change your connection string to match your server/database details.
  • You may need to install client libraries or NuGet packages for Oracle.

💡 Tip

  • You can use similar ADO.NET code for other databases (e.g. MySQL, PostgreSQL) with the right NuGet provider.

Next: Executing SQL commands (SELECT, INSERT, UPDATE, DELETE)!