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, 2022 in C# by pranav
• 2,590 points
794 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, 2022 in C# by krishna
• 2,820 points
662 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, 2022 in C# by pranav
• 2,590 points
366 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, 2022 in C# by rajiv
• 1,620 points
1,367 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, 2022 in C# by rajiv
• 1,620 points
485 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, 2022 in C# by jyoti
• 1,240 points
971 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, 2022 in C# by jyoti
• 1,240 points
668 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
636 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,840 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
1,006 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, 2022 in Others by narikkadan
• 63,700 points
661 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP