⚠️ Malware / Trojaner / VirenThe Real Risks Of Shadow AI In The Enterprise(08.09.2026 um 16:47 Uhr)
🔧 AI Nachrichten Stopping Data Exfiltration Through LLM Prompts And Responses(08.09.2026 um 17:51 Uhr)
🕵️ SicherheitslückenLinux Threat Hunting - PSW #942(03.09.2026 um 23:00 Uhr)
⚠️ Malware / Trojaner / VirenThe Real Risks Of Shadow AI In The Enterprise(08.09.2026 um 16:47 Uhr)
🔧 AI Nachrichten Stopping Data Exfiltration Through LLM Prompts And Responses(08.09.2026 um 17:51 Uhr)
🕵️ SicherheitslückenLinux Threat Hunting - PSW #942(03.09.2026 um 23:00 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 4 Min Lesezeit
0

Params Guide in UkrGuru.Sql

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




Guide to Using Parameters in UkrGuru.Sql



In this article, we'll walk through creating a sample console application using the UkrGuru.Sql library. This library simplifies database operations in .NET applications. We'll demonstrate how to execute various SQL queries and handle different data types.






Setting Up the Project



First, ensure you have the UkrGuru.Sql library installed. You can add it to your project via NuGet Package Manager.




CODE
dotnet add package UkrGuru.Sql









Configuring the Connection String



Set up your database connection string. For this example, we'll use a local database.




CODE
DbHelper.ConnectionString = "Server=(localdb)\\mssqllocaldb;Integrated Security=true";









Executing SQL Queries



The UkrGuru.Sql library uses @Data as the default name for a single parameter. Below are examples of executing SQL queries with different data types.






Boolean Result



To execute a query that returns a boolean result:




CODE
var boolResult = DbHelper.Exec<bool>("SELECT @Data", true);
Console.WriteLine($"Boolean Result: {boolResult}");






Result: Boolean Result: True






Integer Result



For queries that return an integer:




CODE
var intResult = DbHelper.Exec<int>("SELECT @Data", 123);
Console.WriteLine($"Integer Result: {intResult}");






Result: Integer Result: 123






String Result



To handle string results:




CODE
var stringResult = DbHelper.Exec<string>("SELECT @Data", "Hello, World!");
Console.WriteLine($"String Result: {stringResult}");






Result: String Result: Hello, World!






Field Result



When you need to work with named parameters:




CODE
var fieldResult = DbHelper.Exec<string>("SELECT @Name", new { Name = "John" });
Console.WriteLine($"Field Result: {fieldResult}");






Result: Field Result: John






DateOnly Result



For date-only results:




CODE
var dateResult = DbHelper.Exec<DateOnly>("SELECT @Data", DateTime.Today);
Console.WriteLine($"DateOnly Result: {dateResult}");






Result: DateOnly Result: 05/01/2025






TimeOnly Result



To handle time-only results:




CODE
var timeResult = DbHelper.Exec<TimeOnly>("SELECT @Data", new TimeSpan(23, 59, 0));
Console.WriteLine($"TimeOnly Result: {timeResult}");






Result: TimeOnly Result: 23:59






Decimal Result



For queries that return a decimal:




CODE
var decimalResult = DbHelper.Exec<decimal>("SELECT @Data", 123.45m);
Console.WriteLine($"Decimal Result: {decimalResult}");






Result: Decimal Result: 123.45






Guid Result



To handle GUID results:




CODE
var guidResult = DbHelper.Exec<Guid>("SELECT @Data", Guid.NewGuid());
Console.WriteLine($"Guid Result: {guidResult}");






Result: Guid Result: b69df053-cf79-4689-a6b2-93f390dd705d






Enum Result



When working with enums:




CODE
var enumResult = DbHelper.Exec<UserType>("SELECT @Data", UserType.Admin);
Console.WriteLine($"Enum Result: {enumResult}");






Result: Enum Result: Admin






Handling JSON and Custom Types



You can also handle JSON and custom types with UkrGuru.Sql.




CODE
// JSON Object Result
var jsonObject = DbHelper.Exec<NamedType>("SELECT @Id Id, @Name Name FOR JSON PATH, WITHOUT_ARRAY_WRAPPER", new { Id = 1, Name = "Test" });
Console.WriteLine($"Json Object Result: Id = {jsonObject?.Id}, Name = {jsonObject?.Name}");






Result: Json Object Result: Id = 1, Name = Test




CODE
// Named Object Result
var namedObject = DbHelper.Read<NamedType>("SELECT @Id Id, @Name Name", new { Id = 1, Name = "Test" }).FirstOrDefault();
Console.WriteLine($"Named Object Result: Id = {namedObject?.Id}, Name = {namedObject?.Name}");






Result: Named Object Result: Id = 1, Name = Test






Using SqlParameter and SqlParameter[]



In addition to the default parameter handling, you can also use SqlParameter and SqlParameter[] for more complex scenarios.






Using SqlParameter



You can create individual SqlParameter objects to pass parameters to your SQL queries.




CODE
var sqlBooleanParam = new SqlParameter("@Data", true);
var booleanResult = DbHelper.Exec<bool>("SELECT @Data", sqlBooleanParam);
Console.WriteLine($"Sql Boolean Result: {booleanResult}");






Result: Sql Boolean Result: True






Using SqlParameter[]



For queries that require multiple parameters, you can use an array of SqlParameter objects.




CODE
var sqlNamedParameters = new SqlParameter[]
{
new SqlParameter("@Id", 1),
new SqlParameter("@Name", "John")
};
var sqlNamedResult = DbHelper.Read<NamedType>("SELECT @Id Id, @Name Name", sqlNamedParameters).FirstOrDefault();
Console.WriteLine($"Sql Named Result: Id = {sqlNamedResult?.Id}, Name = {sqlNamedResult?.Name}");






Result: Sql Named Result: Id = 1, Name = John






Enum and Custom Class Definitions



Define your enums and custom classes as needed.




CODE
enum UserType
{
Guest,
User,
Manager,
Admin,
SysAdmin
}

class NamedType
{
public int Id { get; set; }
public string? Name { get; set; }
}









Conclusion



This sample console application demonstrates how to use the UkrGuru.Sql library to execute SQL queries and handle various data types in a .NET application. With this library, you can simplify your database operations and focus on building your application's core features.



Feel free to expand this example to suit your specific needs. Happy coding! 🚀



For more details, you can check the source code here.

https://github.com/UkrGuru/Sql/blob/main/demos/InOutSqlDemo/Program.cs

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
2 Quellen
News alert: Bright Security launches AI PT, AI-powered penetration testing that cuts weeks to hours
1 Quelle
Case study: ManageWP Blocks 11.9M+ Threats in 6 Months with Patchstack
1 Quelle
Unauthenticated PHP Object Injection to Remote Code Execution on GiveWP
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Params Guide in UkrGuru.Sql

Thematisch verwandte Begriffe: Params, Guide, UkrGuruSql · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...