Import Web API data into SQL Server database - c#

I am new to Web API and I have a questions: how to import Web API data into a SQL Server database.
My Web API is created with data reader and I have searched in the internet, it seems like it could be import data with data reader. I want to test importing data with Web API, so I can know is my Web API working or not.
Code:
[HttpGet]
public IHttpActionResult GetAll()
{
List<webapi> web = new List<webapi>();
using (SqlCommand cmd = new SqlCommand("SELECT U.UserID, U.Name, U.Mobile, U.Age, U.Birthday, A.Address, A.Country From tbluser U inner join tblAddress A where A.Country=U.Country", connection))
{
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
web.Add(new webapi()
{
UserId = Convert.ToString(sdr.GetValue(0)),
Name = Convert.ToString(sdr.GetValue(01)),
Mobile = Convert.ToString(sdr.GetValue(2)),
Age = (sdr.GetValue(3) != DBNull.Value) ? Convert.ToInt32(sdr.GetValue(3)) : 0,
Birthday = (sdr.GetValue(4) != DBNull.Value) ? Convert.ToDateTime(sdr.GetValue(4)) : (DateTime?)null
Address = Convert.ToString(sdr.GetValue(5)),
Country = Convert.ToString(sdr.GetValue(6))
});
}
return Ok(web);
}
}
[HttpGet]
public IHttpActionResult GetCountry(string country)
{
List<webapi> web = new List<webapi>();
using (SqlCommand cmd = new SqlCommand("SELECT U.UserID, U.Name, U.Mobile, U.Age, U.Birthday, A.Address, A.Country From tbluser U inner join tblAddress A where charindex(#country, A.Country)=1 order by U.UserId", connection))
{
cmd.Parameters.Add("#country", SqlDbType.VarChar);
cmd.Parameters["#country"].Value = country.ToString();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
web.Add(new webapi()
{
UserId = Convert.ToString(sdr.GetValue(0)),
Name = Convert.ToString(sdr.GetValue(01)),
Mobile = Convert.ToString(sdr.GetValue(2)),
Age = (sdr.GetValue(3) != DBNull.Value) ? Convert.ToInt32(sdr.GetValue(3)) : 0,
Birthday = (sdr.GetValue(4) != DBNull.Value) ? Convert.ToDateTime(sdr.GetValue(4)) : (DateTime?)null
Address = Convert.ToString(sdr.GetValue(5)),
Country = Convert.ToString(sdr.GetValue(6))
});
}
}
return Ok(web);
}
}
}
Class:
public class webapi
{
public string UserId { get; set; }
public string Name { get; set; }
public string Mobile { get; set; }
public int Age { get; set; }
public DateTime? Birthday { get; set; }
public string Address { get; set; }
public string Country { get; set; }
}

Related

i got out of memory when i am reading large amount of data with datareader in web api C#

There is large data in sql server database and i have made an api to read record. i used data reader to read all the data, when i test the data with google chrome, it said out of memory in te chrome and i want to use data set to control these data. how can i add data set into data reader or i need change to data table to use data set in my code? or are there any other solution to control large amount data.
my code:
public IHttpActionResult Get()
{
List<TestClass> draft = new List<TestClass>();
string mainconn = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(mainconn);
string sqlquery = "SELECT UserID, Name, Mobile, Age, Date From tbluser";
sqlconn.Open();
SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
SqlDataReader sdr = sqlcomm.ExecuteReader();
while (sdr.Read())
{
draft.Add(new TestClass()
{
UserId = sdr.GetString(0),
Name = sdr.IsDBNull(1) ? string.Empty : sdr.GetString(1),
Mobile = sdr.IsDBNull(2) ? string.Empty : sdr.GetString(2),
Age = (sdr.GetValue(3) !=DBNull.Value)? Convert.ToInt32(sdr.GetValue(3)) : 0,
Date = (sdr.GetValue(4) !=DBNull.Value)? Convert.ToDateTime(sdr.GetValue(4)): (DateTime?)null
});
}
return Ok(draft);
}
my class:
public class TestClass
{
public string UserId { get; set; }
public string Name { get; set; }
public string Mobile { get; set; }
public int Age { get; set; }
public DateTime? Date { get; set; }
}
When any process gets triggered, a separate virtual space is assigned to that process. Any program deals with virtual space not with physical memory, Garbage Collector deals with the same virtual memory to allocate and de-allocate memory.
out of memory happens due to virtual space is full
I would try in this way:
int limit = 50 // limit for data to get from DB.
public IHttpActionResult Get(int offset)
{
List<TestClass> draft = new List<TestClass>();
string mainconn = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
using(SqlConnection sqlconn = new SqlConnection(mainconn))
{
string sqlquery = $"SELECT UserID, Name, Mobile, Age, Date From tbluser order by UserID offset {offset} rows fetch next {limit} rows only";
sqlconn.Open();
using(SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn))
{
using(SqlDataReader sdr = sqlcomm.ExecuteReader())
{
while (sdr.Read())
{
draft.Add(new TestClass()
{
UserId = sdr.GetString(0),
Name = sdr.IsDBNull(1) ? string.Empty : sdr.GetString(1),
Mobile = sdr.IsDBNull(2) ? string.Empty : sdr.GetString(2),
Age = (sdr.GetValue(3) != DBNull.Value)? Convert.ToInt32(sdr.GetValue(3)) : 0,
Date = (sdr.GetValue(4) != DBNull.Value)? Convert.ToDateTime(sdr.GetValue(4)): (DateTime?)null
});
}
}
}
}
return Ok(draft);
}

How to show all data from c#

I'm kind a new on c#. I have a problem with to store the className to list since i need to display all the class that teacher taught. On result, it turns out just the last class teacher taught. I did use join table between teacher and classes.
Model
public class Teacher
{
public int teacherId { get; set; }
public string teacherfName { get; set; }
public string teacherlName { get; set; }
public string className { get; set; }
public int classId { get; set; }
}
Controller
public Teacher FindTeacher(int id)
{
Teacher newTeacher = new Teacher();
MySqlConnection Conn = school.AccessDatabase();
Conn.Open();
MySqlCommand cmd = Conn.CreateCommand();
//SQL QUERY
cmd.CommandText = "Select * from teachers left join classes on teachers.teacherid=classes.teacherid where teachers.teacherid = " + id;
//Gather Result Set of Query into a variable
MySqlDataReader ResultSet = cmd.ExecuteReader();
while (ResultSet.Read())
{
int teacherId = (int)ResultSet["teacherId"];
string teacherfName=ResultSet["teacherfname"].ToString();
string teacherlName=ResultSet["teacherlname"].ToString();
newTeacher.teacherId = teacherId;
newTeacher.teacherFName = teacherFName;
newTeacher.teacherLName = teacherLName;
newTeacher.className = className;
newTeacher.classId = (int)ResultSet["classid"];
}
return newTeacher;
}
Your only returning one teacher if you want all the teachers your code should be:
public IEnumerable<Teacher> FindTeacher(int id)
{
//Lise here
List<Teacher> teachers = new List<Teacher>();
//note the using
using MySqlConnection Conn = school.AccessDatabase();
Conn.Open();
//note the using
using MySqlCommand cmd = Conn.CreateCommand();
//SQL QUERY
cmd.CommandText = "Select * from teachers left join classes on teachers.teacherid=classes.teacherid where teachers.teacherid = " + id;
//Gather Result Set of Query into a variable
MySqlDataReader ResultSet = cmd.ExecuteReader();
while (ResultSet.Read())
{
//new teacher in the loop
Teacher newTeacher = new Teacher();
int teacherId = (int)ResultSet["teacherId"];
string teacherfName=ResultSet["teacherfname"].ToString();
string teacherlName=ResultSet["teacherlname"].ToString();
newTeacher.teacherId = teacherId;
newTeacher.teacherFName = teacherFName;
newTeacher.teacherLName = teacherLName;
newTeacher.className = className;
newTeacher.classId = (int)ResultSet["classid"];
//add to the collection
teachers.Add(newTeacher);
}
//return the collection
return teachers;
}
If also added using statements. These are important to prevent memory leaks
Modify Teacher Class to be able to carry List of TeacherClass that correspond to one teacher:
Define New Class TeacherClass to Carry a TeacherClass Data
public class TeacherClass
{
public string Name { get; set; }
public int Id { get; set; }
}
Modify Teacher Class To have a List Of TeacherClass
public class Teacher
{
public int teacherId { get; set; }
public string teacherfName { get; set; }
public string teacherlName { get; set; }
public List<TeacherClass> classes { get; set; } = new List<TeacherClass>();
}
Then get your function to set this TeacherClass List in a loop:
public Teacher FindTeacher(int id)
{
Teacher newTeacher = new Teacher();
//note the using
using MySqlConnection Conn = school.AccessDatabase();
Conn.Open();
//note the using
using MySqlCommand cmd = Conn.CreateCommand();
//SQL QUERY
cmd.CommandText = "Select * from teachers left join classes on teachers.teacherid=classes.teacherid where teachers.teacherid = " + id;
//Gather Result Set of Query into a variable
MySqlDataReader ResultSet = cmd.ExecuteReader();
// Check if any rows retrieved
if (reader.HasRows)
{
// Iterate Over Rows
while (ResultSet.Read())
{
// Set Teacher Data Just Once
if(newTeacher.teacherId == 0){
newTeacher.teacherId = (int)ResultSet["teacherId"];;
newTeacher.teacherFName = ResultSet["teacherfname"].ToString();
newTeacher.teacherLName = ResultSet["teacherlname"].ToString();
}
// Add new TeacherClass data for this teacher
newTeacher.classes.Add(
new TeacherClass(){
Name = className, // className Check this variable as it is not declared
Id = (int)ResultSet["classid"]
});
}
}
return newTeacher;
}

Using web service to retrieve more than one database column

I am trying to use a web service to retrieve data for more than one column. The code below works fine :
public string GetCustomerNameWithIDNumber(string id_number)
{
string fullname ="";
string id_type = "";
string id_number = "";
string dob = "";
using (SqlConnection cn = new SqlConnection(constring))
{
cn.Open();
string q = "select fullname, id_type, id_number, date_ofbirth from account_info where id_number = #id_number";
using (SqlCommand cmd = new SqlCommand(q, cn))
{
cmd.Parameters.AddWithValue("#id_number", id_number);
using (SqlDataReader rd = cmd.ExecuteReader())
{
while (rd.Read())
{
fullname = rd["fullname"].ToString();
id_type = rd["id_type"].ToString();
id_number = rd["id_number"].ToString();
dob = rd["date_ofbirth"].ToString();
bvn2 = rd["bvn"].ToString();
}
}
return fullname;
}
}
}
Which just gets the Full name alone, now suppose i want to get for more than just one database column, how do I go about it?
You will want to return a class. Here is a simple class I wrote to hold this data:
class Customer {
public string FullName { get; set; }
public string IdType { get; set; }
public string IdNumber { get; set; }
public string DateOfBirth { get; set; }
public string Bvn { get; set; }
}
Then this method returns that customer object:
public Customer GetCustomerNameWithIDNumber(string id_number) {
using (SqlConnection cn = new SqlConnection(constring)) {
cn.Open();
string q = "select fullname,id_type,id_number,date_ofbirth from account_info where id_number =#id_number";
using (SqlCommand cmd = new SqlCommand(q, cn)) {
cmd.Parameters.AddWithValue("#id_number", id_number);
using (SqlDataReader rd = cmd.ExecuteReader()) {
while (rd.Read()) {
return new Customer {
FullName = rd["fullname"].ToString(),
IdType= rd["id_type"].ToString(),
IdNumber = rd["id_number"].ToString(),
DateOfBirth = rd["date_ofbirth"].ToString(),
Bvn = rd["bvn"].ToString()
};
}
}
}
}
}
This code will return the first customer in the database matching the id_number

display more than one database query in .net MVC

I am creating my first project using asp.net MVC - I have successfully connected to the database and displayed the information on the index page. My question is how do I get more than one query result on the one index page
for e.g
SELECT student ID,first name,surname FROM STUDENT Notes WHERE student ID = 7
Do I need to create new controllers/models for each query or need to add to the current and if I add to the current how would I do it? Below is the code I currently have in my controller.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
//int sNumber = 1;
List<CustomerModel> customers = new List<CustomerModel>();
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
string query = "SELECT title, `first name`, surname FROM `STUDENT REGISTER`";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(new CustomerModel
{
// CustomerId = Convert.ToInt32(sdr["Student Number"]),
Title = sdr["title"].ToString(),
Name = sdr["first name"].ToString(),
Surname = sdr["surname"].ToString()
});
}
}
con.Close();
}
}
return View(customers);
}
Create a view model with two result set for e.g. Student and Marks
public class Result
{
public Student Student { get; set; }
public Marks Marks { get; set; }
}
Load/Construct this Result view model in controller /service with appropriate data and pass this view model to view.
I hope this helps!
You have to create a new class with all the properties you want to display in your View.
Example:
public class StudentModel {
public int Id { get; set; }
public string Title { get; set; }
public string Name { get; set; }
public strign Surname { get; set; }
}
public class MarkModel {
public int Id { get; set; }
public int StudentId { get; set; }
public int SubjectId { get; set; }
public int Mark { get; set; }
}
public class ResultModel
{
public StudentModel Student { get; set; }
public List<MarkModel> Marks { get; set; }
}
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
//int sNumber = 1;
var model= new ResultModel{
Student = new StudentModel(),
Marks = new List<MarkModel>();
}
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
string queryStudent = "SELECT id, title, `first name`, surname FROM `STUDENT` WHERE Id=1";
using (MySqlCommand cmd = new MySqlCommand(queryStudent))
{
cmd.Connection = con;
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
model.student.Id = Convert.ToInt32(sdr["id"]),
model.student.Title = sdr["title"].ToString(),
model.student.Name = sdr["first name"].ToString(),
model.student.Surname = sdr["surname"].ToString()
}
}
}
string queryMarks = "SELECT Id, `StudentId`, StudentId,Mark FROM `MARK` WHERE StudentId=1";
using (MySqlCommand cmd = new MySqlCommand(queryMarks))
{
cmd.Connection = con;
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
model.Marks.Add(new MarkModel
{
Id = Convert.ToInt32(sdr["Id"]),
StudentId = Convert.ToInt32(sdr["StudentId"]),
StudentId = Convert.ToInt32(sdr["StudentId"]),
Mark = Convert.ToInt32(sdr["Mark"]),
});
}
}
}
}
return View(model);
}
You should create a new ViewModel class with all the properties you want to display in your View. Then you model your View after it.
From the properties you provided so far, the class should look like this:
public class StudentViewModel {
public int Id { get; set; }
public string Title { get; set; }
public string Name { get; set; }
public strign Surname { get; set; }
}
Then you do the value x property assignment
string query = "SELECT title, `first name`, surname FROM `STUDENT REGISTER`";
List<StudentViewModel> model = new List<StudentViewModel>();
using (MySqlCommand cmd = new MySqlCommand(query)) {
cmd.Connection = con;
con.Open();
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
model.Add(new StudentViewModel
{
Id = Convert.ToInt32(sdr["StudentNumber"]),
Title = Convert.ToString(sdr["title"]),
Name = Convert.ToString(sdr["first name"]),
Surname = Convert.ToString(sdr["surname"])
});
}
}
con.Close();
}
return View(model);

MVC DataReader Timeout Error

I have this problem with error timeout, always when it suppose to get data from database it bounce error. I am trying to retrieve video from database with this code:
Repository.cs
public List<videoTble> Video()
{
var model = new List<videoTble>();
string ConStr = "Data Source="";Connect Timeout=60";
using (SqlConnection con = new SqlConnection(ConStr))
{
string str = "SELECT * FROM videoTbles";
con.Open();
SqlCommand cmd = new SqlCommand(str, con);
cmd.CommandTimeout = 60;
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
var v = new videoTble();
v.Name = rd["Name"].ToString();
v.Data = (byte[])rd["Data"];
v.ContentType = rd["ContentType"].ToString();
v.ArtistName = rd["ArtistName"].ToString();
v.Expirydate = (DateTime)rd["Expirydate"];
model.Add(v);
}
con.Close();
}
return model;
}
Controller.cs
public ActionResult Download()
{
Repository res = new Repository();
ViewBag.Video = res.Video();
return View();
}
Model.cs
public partial class videoTble
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string ContentType { get; set; }
public byte[] Data { get; set; }
public string ArtistName { get; set; }
public DateTime Expirydate { get; set; }
}
The timeout Error I got, when i run application
It looks like you are storing your video as raw data in your database. I highly advise against this. Store the video files on your file system, and store the paths to the video files in your database.

Categories