I am creating an API which returns some results from a MySql database. I have a table including 2 rows and some fields. The main factor for query is a field called Title
. The application should return results based on the "title" field. The titles
of these two records are Sweet Street and Sweet Street 2 .
for returning the results as JSON, I have created a class:
using System.Collections.Generic;
namespace IMES_Backend.Controllers
{
internal class Movie
{
public class BaseResponse
{
public List<Item> search { get; set; } = new List<Item>();
public bool response { get; set; }
}
public class Item
{
public string title { get; set; }
public string year { get; set; }
public string released { get; set; }
public string runTime { get; set; }
public string genre { get; set; }
public string director { get; set; }
public string writer { get; set; }
public string actors { get; set; }
public string language { get; set; }
public string country { get; set; }
public string awards { get; set; }
public string poster { get; set; }
public string imdbScore { get; set; }
public string production { get; set; }
public string dl480p { get; set; }
public string dl720p { get; set; }
public string dl1080p { get; set; }
public string subtitleLink { get; set; }
public string dubLink { get; set; }
public string description { get; set; }
public string state { get; set; }
}
}
}
Then I select that 2 rows as previously mentioned and return the results in JSON:
[Route("IMES/api/GET/search/t={movieTitle}")]
[HttpGet]
public IActionResult MovieSearch(string movieTitle)
{
string searchKeyword = movieTitle.Replace("%20", " ");
//Try to connect to the database
try
{
DB.dbConnection.Open();
MySqlCommand cmd = new MySqlCommand("select * from Movies where Title LIKE '%" + searchKeyword + "%'",DB.dbConnection);
DB.dataReader = cmd.ExecuteReader();
while (DB.dataReader.Read())
{
baseResponse.response = true;
//Create a list for storing the movie's data
baseResponse.search.Add(new Movie.Item
{
title = DB.dataReader.GetString(1),
year = DB.dataReader.GetString(2),
released = DB.dataReader.GetString(3),
runTime = DB.dataReader.GetString(4),
genre = DB.dataReader.GetString(5),
director = DB.dataReader.GetString(6),
writer = DB.dataReader.GetString(7),
actors = DB.dataReader.GetString(8),
language = DB.dataReader.GetString(9),
country = DB.dataReader.GetString(10),
awards = DB.dataReader.GetString(11),
poster = DB.dataReader.GetString(12),
imdbScore = DB.dataReader.GetString(13),
production = DB.dataReader.GetString(14),
dl480p = DB.dataReader.GetString(15),
dl720p = DB.dataReader.GetString(16),
dl1080p = DB.dataReader.GetString(17),
subtitleLink = DB.dataReader.GetString(18),
dubLink = DB.dataReader.GetString(19),
description = DB.dataReader.GetString(20),
state = DB.dataReader.GetString(21),
});
string response = JsonConvert.SerializeObject(baseResponse);
return Ok(response);
}
}
catch(MySqlException ex) //MySql connection problem
{
return Ok(ex.Message);
}
finally
{
DB.dbConnection.Close();
DB.dataReader.Close();
}
baseResponse.response = false;
string error = JsonConvert.SerializeObject(baseResponse.response);
return Ok(error);
}
When I browse the .../IMES/api/GET/search/t=sweet
, I just get the first rows data, there is no second list for the second row which includes the keyword sweet
.
I want to get the both rows' data in separated lists in JSON. Can anyone help?
Note: I have tried the query in SQL Studio and I have received two records. So I'm sure about that 2 rows!
You're returning from the while loop
while(){
retun Ok(response);
}
, so it is returned after the first item is added. Move it outside of the while loop
while(){
//do stuff
}
string response = JsonConvert.SerializeObject(baseResponse);
return Ok(response);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.