Home > other >  SQL Server JSON object and C#
SQL Server JSON object and C#

Time:09-20

I have created a JSON object using SQL Server with the following query:

SELECT 
    Attribute 1 ,Attribute 2, Attribute 3,.... AS Identifier 
FROM 
    Table_1  tbl1_obj
INNER JOIN 
    Table_2 tbl2_obj ON tbl2_obj.Id = tbl1_obj.Id 
FOR JSON AUTO 

I wanted to get a JSON object in C# from SQL Server. Wanted to know how ...

CodePudding user response:

You can use Dapper, Its high-performance Micro-ORM. And your code should be similar to the one below. You can define properties and Use the proper overloaded method based on your requirement. The below code is just for reference.

public class Identifier 
{
    public int Attribute1;
    public int Attribute2;
    public int Attribute3;
}

using (var connection = new SqlConnection(_onpremisesFXecuteDBConnectionString))
{
    CommandDefinition command = new CommandDefinition(<YourQuery>, null, null, null, CommandType.Text);
    var identifier= connection.QueryFirstOrDefault<Identifier>(command);
}

Other options are to use Ado.Net code or EF as well, you can explore them as well.

  • Related