Junior — Middle
Դուք ունե՞ք փորձ SQL հարցումների միջոցով տվյալների բազաներով աշխատելու։
sobes.tech AI
Պատասխան AI-ից
Այո, տվյալների բազաների հետ շփումը SQL հարցումների միջոցով սովորական պրակտիկա է C#-ում զարգացման ժամանակ: Սովորաբար օգտագործվում է ADO.NET կամ ORM, օրինակ՝ Entity Framework:
Օրինակ՝ պարզ SQL հարցում 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}");
}
}
}
Այս մոտեցումը թույլ է տալիս կատարել ցանկացած SQL հարցում, ստանալ տվյալներ և աշխատել դրանց հետ ծրագրում։