Executing query with parameters

前端 未结 5 802
-上瘾入骨i
-上瘾入骨i 2020-11-28 14:46

I want to execute a .sql script from C#. Basically the script inserts a row into few different tables.

The point is I have values in C# code that I need

5条回答
  •  感动是毒
    2020-11-28 15:15

    Frankly, ADO.NET makes it hard to do things like this correctly. Tools like Dapper exist to make that easier:

    dbConn.Execute(
         @"insert into [DB].[dbo].[User] ( [Id], [AccountId], [FirstName], [LastName],
                                           [JobTitle], [PhoneNumber] )
           values ( @id, @accountId, @firstName, @lastName, @jobTitle, @phoneNumber )",
           new { id, accountId, firstName, lastName, jobTitle, phoneNumber });
    

    This will deal with all the parameterization for you, efficiently, effectively, and safely.

    There are similar APIs for executing queries and populating objects.

提交回复
热议问题