[ASP.NET] 데이터 베이스 조회, JSON리턴

using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;

public class HomeController : Controller
{
    private readonly string _connectionString;

    public HomeController(IConfiguration configuration)
    {
        _connectionString = configuration.GetConnectionString("DefaultConnection");
    }

    public IActionResult Index()
    {
        using (SqlConnection connection = new SqlConnection(_connectionString))
        {
            connection.Open();

            // 데이터베이스 조회
            string query = "SELECT idx, title, m_id FROM board_tbl";
            SqlCommand command = new SqlCommand(query, connection);
            SqlDataReader reader = command.ExecuteReader();

            // 결과 처리
            DataTable dataTable = new DataTable();
            dataTable.Load(reader);

            // DataTable을 JSON 문자열로 변환
            string json = JsonConvert.SerializeObject(dataTable);

            // JSON 문자열을 반환
            return Content(json, "application/json");
        }
    }
}

using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;

public class HomeController : Controller
{
    private readonly string _connectionString;

    public HomeController(IConfiguration configuration)
    {
        _connectionString = configuration.GetConnectionString("DefaultConnection");
    }

    public IActionResult Index()
    {
        using (SqlConnection connection = new SqlConnection(_connectionString))
        {
            connection.Open();

            // 데이터베이스 조회
            string query = "SELECT idx, title, m_id FROM board_tbl";
            SqlCommand command = new SqlCommand(query, connection);
            SqlDataReader reader = command.ExecuteReader();

            // 결과 처리
            DataTable dataTable = new DataTable();
            dataTable.Load(reader);

            // JSON 배열 생성
            var jsonArray = new JArray();
            while (reader.Read())
            {
                var jsonObject = new JObject();
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    jsonObject[reader.GetName(i)] = JToken.FromObject(reader.GetValue(i));
                }
                jsonArray.Add(jsonObject);
            }

            // JSON 배열을 문자열로 변환
            string json = jsonArray.ToString();

            // JSON 문자열을 반환
            return Content(json, "application/json");
        }
    }
}

using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;

public class HomeController : Controller
{
    private readonly string _connectionString;

    public HomeController(IConfiguration configuration)
    {
        _connectionString = configuration.GetConnectionString("DefaultConnection");
    }

    public IActionResult Index()
    {
        using (SqlConnection connection = new SqlConnection(_connectionString))
        {
            connection.Open();

            // 데이터베이스 조회
            string query = "SELECT idx, title, m_id FROM board_tbl";
            SqlCommand command = new SqlCommand(query, connection);
            SqlDataReader reader = command.ExecuteReader();

            // 결과 처리
            DataTable dataTable = new DataTable();
            dataTable.Load(reader);

            // JSON 배열 생성
            var jsonArray = new JArray();
            foreach (DataRow row in dataTable.Rows)
            {
                var jsonObject = new JObject();
                foreach (DataColumn column in dataTable.Columns)
                {
                    jsonObject[column.ColumnName] = JToken.FromObject(row[column]);
                }
                jsonArray.Add(jsonObject);
            }

            // JSON 배열을 문자열로 변환
            string json = jsonArray.ToString();

            // JSON 문자열을 반환
            return Content(json, "application/json");
        }
    }
}

결과에 원하는 값 추가하려면 이렇게 해야함.

IEnumerable<dynamic> result = dbConnection.Query(query);
List<ExpandoObject> resultList = new List<ExpandoObject>();

foreach (var row in result)
{
    dynamic dynamicRow = new ExpandoObject();
    foreach (var property in row)
    {
        ((IDictionary<string, object>)dynamicRow)[property.Key] = property.Value;
    }
    dynamicRow.test = "addtest";
    resultList.Add(dynamicRow);

}

return Ok(resultList);
guest
0 Comments
Inline Feedbacks
View all comments