Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Service built in C# to retrieve data from mySQL database

I am trying to build a web service in .NET, which will retrieve data from a mySQL database. This web service will later on be combined with a windows form where this data will be displayed.

Till now, I have the database ready, the connection between the database and the web service has been made and the form is ready as well.

However, I am unable to retrieve particular bits of information from the table itself. Can anyone help me to figure out what my next steps should be? I have googled a lot on this issue but I was still unable to find a good tutorial regarding this issue...if you have any in mind then can you please post the link as well? Thanks in advance!

EXTRA INFORMATION: Suppose a sample table called "testdata" which has three columns in it ("id", "name", "age"). How can I extract the name and the age and display them on the form?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace WebService2
{

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {



        private void connectoToMySql()
        {

            string connString = "SERVER=localhost" + ";" +
                "DATABASE=testdatabase;" +
                "UID=root;" +
                "PASSWORD=password;";

            MySqlConnection cnMySQL = new MySqlConnection(connString);

            MySqlCommand cmdMySQL = cnMySQL.CreateCommand();

            MySqlDataReader reader;

            cmdMySQL.CommandText = "select * from testdata";

            cnMySQL.Open();

            reader = cmdMySQL.ExecuteReader();


           //-----------------------------------------------------------
           // This is the part where I should be able to retrieve the data from the database
           //-----------------------------------------------------------               


            cnMySQL.Close();
        }


    }
}
like image 998
BurninatorDor Avatar asked Dec 22 '22 05:12

BurninatorDor


1 Answers

Create a method that is public, marked with the WebMethodAttribute, and returns a DataTable (if you consumer is a .net client). Have the consumer call that method and do whatever you want with the DataTable.

[System.Web.Services.WebMethod]
public DataTable connectoToMySql()
{
    string connString = "SERVER=localhost" + ";" +
        "DATABASE=testdatabase;" +
        "UID=root;" +
        "PASSWORD=password;";

    MySqlConnection cnMySQL = new MySqlConnection(connString);

    MySqlCommand cmdMySQL = cnMySQL.CreateCommand();

    MySqlDataReader reader;

    cmdMySQL.CommandText = "select * from testdata";

    cnMySQL.Open();

    reader = cmdMySQL.ExecuteReader();

    DataTable dt = new DataTable();
    dt.Load(reader);


    cnMySQL.Close();

    return dt;
} 
like image 54
Dave Avatar answered Jan 05 '23 11:01

Dave