I am new to web api and I have some problem with my web api. I have created an web api with sql server, but i did not used entity framwork, it is kind of old way to connect sql server database to web api.
The problem for now is my sql server database have more than ten thousand records and when i run my web api in google chrome and it showed out of memory in google chrome. I do not know why it will out of memory, those records are less than 200mb, it should not out of memory. I have though about my codes are wrong in somewhere, does anyone know how to solve it?
web api codes with data table:
public SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
[HttpGet]
public IHttpActionResult GetAll()
{
List<webapi> web = new List<webapi>();
SqlCommand cmd = new SqlCommand("SELECT UserID, Name, Address, Mobile, Birthday From tbluser", connection);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
sda.Fill(table);
foreach(DataRow read in table.Rows)
{
web.Add(new webapi
{
UserId = Convert.ToString(read[0]),
Name = Convert.ToString(read[1]),
Address = Convert.ToString(read[2]),
Mobile = (read[3] != DBNull.Value) ? Convert.ToInt32(read[3]) : 0,
Birthday = (read[4] != DBNull.Value) ? Convert.ToDateTime(read[4]) : (DateTime?)null
});
}
return Ok(web);
}
web api codes with data reader:
public SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
[HttpGet]
public IHttpActionResult GetAll()
{
List<webapi> web = new List<webapi>();
SqlCommand cmd = new SqlCommand("SELECT UserID, Name, Address, Mobile, Birthday From tbluser", connection);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
web.Add(new webapi()
{
UserId = sdr.GetString(0),
Name = sdr.GetString(1),
Address = sdr.GetString(2),
Mobile = (sdr.GetValue(3) != DBNull.Value) ? Convert.ToInt32(sdr.GetValue(3)) : 0,
birthday = (sdr.GetValue(4) != DBNull.Value) ? Convert.ToDateTime(sdr.GetValue(4)) : (DateTime?)null
});
}
return Ok(web);
}
I also made two version of get method which used data table and data reader to get data.
Class:
public class webapi
{
public string UserId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Mobile { get; set; }
public DateTime? Birthday { get; set; }
}