Sobes.tech
Junior — Middle

Máte skúsenosti s prácou s databázami pomocou SQL dotazov?

sobes.tech AI

Odpoveď od AI

Áno, interakcia s databázami prostredníctvom SQL dopytov je štandardnou praxou pri vývoji v C#. Zvyčajne sa používa ADO.NET alebo ORM, napríklad Entity Framework.

Príklad jednoduchého SQL dopytu s použitím ADO.NET:

using System.Data.SqlClient;

string connectionString = "Server=myServer;Database=myDB;User Id=myUser;Password=myPass;";
string query = "SELECT Id, Name FROM Users WHERE Age > @age";

using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlCommand command = new SqlCommand(query, connection);
    command.Parameters.AddWithValue("@age", 18);

    connection.Open();
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            int id = reader.GetInt32(0);
            string name = reader.GetString(1);
            Console.WriteLine($"User: {id}, {name}");
        }
    }
}

Tento prístup umožňuje vykonávať akékoľvek SQL dopyty, získavať údaje a s nimi pracovať v aplikácii.