How to handle data that's coming from from database in c#


How to handle data that's coming from from database in c#



I think my connection has no problem. But when I run the program and opened browser with this link "http://localhost:1290/bolum/hello" I reached just



Output: DKLSAJDKLJALSJDLs Q



. How can I use my database data? My model,view,controller files are on the bottom. I removed "using .." lines when writing here.



CONTROLLER


namespace WebApplication4.Controllers
{
public class BolumController : Controller
{
public ActionResult Hello()
{
var bolum = new Hello() { Ad = "Q" };
Console.WriteLine("success");
MySqlConnection con = new MySqlConnection("server=localhost;database=proje;uid=root;password=123456;");
MySqlDataAdapter da = new MySqlDataAdapter("Select * From hasta_bilgileri", con);
DataSet ds = new DataSet();
try{
con.Open();
}
catch (MySqlException e){
Console.WriteLine(e.Message);
}
finally {
da.Fill(ds, "hasta_bilgileri");
con.Close();
}
return View(bolum);
}
}
}



MODEL


namespace WebApplication4.Models
{
public class Hello
{
public string Ad { get; set; }

}
}



VIEW


@model WebApplication4.Models.Hello
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Hello</title>
</head>
<body>


DKLSAJDKLJALSJDLs
@Model.Ad

</body>
</html>





Instead of a DataSet - use a DataReader and read the data into new instances of your model (and create a List<Hello>` if there are multiple records). But that code belongs in a separate service, not your controller
– Stephen Muecke
Jun 29 at 10:01


DataSet


DataReader





I am trying but taking errors like "Connection must be valid and open". Can you write a code sample? @StephenMuecke
– Hüseyin Argun
Jun 29 at 10:46





You will find tens of thousands of examples on the web (including this one)
– Stephen Muecke
Jun 29 at 10:55





Sounds like you tried to read data from the database after you'd closed the connection to it. Maybe just need to move your code around a bit. It's not clear, since you didn't show us what you tried. Anyway yeah basically you need to read the data from the database into your model object. If you want to return a list of things, make the model be a list of those objects
– ADyson
Jun 29 at 10:55




1 Answer
1



Since the question is not clear & we don't understand what is the scenario, I suppose that you want to fetch some data from a table & you want to show it in a line using Console.WriteLine()! This is going to be a sample, then you can improve it yourself in your own script.


Console.WriteLine()



We suppose you have a table calledTable including 2 fields: ID and Username. We want to read the Username field of a row where ID is 1.


Table


ID


Username


Username


1



We will use MySqlDataReader which allows accessing the data.


MySqlDataReader



Then, I update your code like this:


MySqlConnection con = new MySqlConnection("server=localhost;database=proje;uid=root;password=123456;");
con.Open();
MySqlCommand CMD = new MySqlCommand("select * from table where ID = 1", con);
MySqlDataReader dr = CMD.ExcecuteReader();
while(dr.Read())
{
Console.WriteLine(dr.GetString(2));
}



Because Username filed is the second field in Table we use 2 in Console.WriteLine(dr.GetString(2))!


Username


Table


2


Console.WriteLine(dr.GetString(2))



This is one way of accessing data from a database. the way that you are doing is something else! I recommend you watching some tutorials on Youtube which will be helpful for understanding....






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift