Created
September 21, 2016 06:36
-
-
Save Kevin-Bronsdijk/4536f680dec7f9c089b598b7acf2baea to your computer and use it in GitHub Desktop.
SQL Server and JSON and c#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static async Task<IDataResponse<TResponse>> BuildResponse<TResponse>(string message) | |
{ | |
var response = new DataResponse<TResponse>(); | |
if (!string.IsNullOrEmpty(message)) | |
response.Body = await JsonConvert.DeserializeObjectAsync<TResponse>(message, SerializerSettings()).ConfigureAwait(false); | |
return response; | |
} | |
internal static JsonSerializerSettings SerializerSettings() | |
{ | |
return new JsonSerializerSettings | |
{ | |
Converters = new List<JsonConverter> { new StringEnumConverter { CamelCaseText = true } }, | |
NullValueHandling = NullValueHandling.Ignore | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var command = new SqlCommand(dataRequest.CmdText, SqlConnection) | |
{ | |
CommandType = dataRequest.CommandType | |
}) | |
{ | |
command.Parameters.AddRange(dataRequest.Parameters.ToArray()); | |
var reader = await command.ExecuteReaderAsync().ConfigureAwait(false); ; | |
if (reader.HasRows) | |
{ | |
while (reader.Read()) | |
{ | |
jsonResult.Append(reader.GetValue(0).ToString()); | |
} | |
} | |
reader.Close(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
Id, | |
UserName, | |
FROM dbo.Accounts | |
FOR JSON PATH |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
Id, | |
UserName, | |
FROM dbo.Accounts | |
WHERE Id = @AccountId | |
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment