ASP NET Web API Connecting to MySQL Database

0 votes

I'm new to C# and.Net programmes, but I'm attempting to create a Web API that gets data from a MySql database and displays it in JSON format when you hit that endpoint.

I'm working with MySql.Data.MySqlClient for this and use with Newtonsoft.JSON to serialize the result format in JSON format

My code

    [Route("api/[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        MySqlConnection conn = new MySqlConnection(@"server=localhost;port=3306;database=mysql;username=root;password=[PASSWORD];");

        [HttpGet]
        public string Get()
        {
            MySqlCommand query = conn.CreateCommand();
            query.CommandText = "SELECT * FROM engine_cost LIMIT 100";

            try
            {
                conn.Open();
            }
            catch (MySqlException ex)
            {
                throw ex;
            }

            MySqlDataReader fetch_query = query.ExecuteReader();

            while (fetch_query.Read())
            {
                return fetch_query.ToString(); // Result: "MySql.Data.MySqlClient.MySqlDataReader"
            }

            return "yay"; // Return JSON with Database values
        }
    }

The code is not the best but how to manage this?

Jun 11 in C# by pranav
• 2,580 points
27 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C#

0 votes
1 answer

How to send email in ASP.NET C#

You can try this using Hotmail like ...READ MORE

answered Jun 14 in C# by krishna
• 2,540 points
21 views
0 votes
0 answers

How to create dependency injection for ASP.NET MVC 5

It's simple to create Dependency Injection using ...READ MORE

Jun 11 in C# by pranav
• 2,580 points
20 views
0 votes
1 answer

What is the best C# to VB.net converter

Telerik has a solid SharpDevelop-based converter that ...READ MORE

answered May 30 in C# by rajiv
• 1,620 points
65 views
0 votes
1 answer

Questions every good .NET developer should be able to answer?

If I were interviewing someone who knew ...READ MORE

answered May 30 in C# by rajiv
• 1,620 points
40 views
0 votes
1 answer

Logging DML operations in ASP.NET MVC project

There are several options. You might register an ...READ MORE

answered Jun 14 in C# by jyoti
• 1,240 points
21 views
0 votes
1 answer

Awesome ASP.NET and C# tutorials for beginners

Go here https://dotnet.microsoft.com/en-us/learn/aspnet They have some nice tutorials ...READ MORE

answered Jun 17 in C# by jyoti
• 1,240 points
28 views
0 votes
1 answer

Authenticate on an ASP.Net Forms Authorization website from a console app

Essentially, we need to record a regular ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
263 views
0 votes
1 answer

Embedding Power Bi Report Promise is not defined powerbi.js

IE does not yet support Promise. You ...READ MORE

answered Oct 30, 2018 in Power BI by Shubham
• 13,490 points
1,199 views
0 votes
1 answer

Validate Amazon SessionToken / Cognito?

You can use it as an OAuth token ...READ MORE

answered Nov 14, 2018 in AWS by Theodor
• 740 points
592 views
0 votes
1 answer

Writing SEO friendly url gets infinite loop asp.net

you should know that regex avoid cases ...READ MORE

answered Feb 11 in Others by narikkadan
• 11,280 points
201 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP