Hi

i created a small program that does
connection to db and perform a select.
that's works fine for me.

now,i want to make it a web service,
but how can i return the result of the select query from it?
please help...

this is my code...

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Text;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace WebService3
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string Main(string[] args)
        {
            string host = "localhost";
            string database = "articles";
            string user = "root";
            string password = "08031976";
            string strSQL = "SELECT * FROM users";
            return "some string";

            string strProvider = "Data Source=" + host + ";Database=" + database + ";User ID=" + user + ";Password=" + password;
            
            try
            {
                MySqlConnection mysqlCon = new MySqlConnection(strProvider);
                mysqlCon.Open();
                

                if (mysqlCon.State.ToString() == "Open")
                {
                    Console.WriteLine("Database Connection Open");
                    Console.WriteLine("------------------------");

                    MySqlCommand mysqlCmd = new MySqlCommand(strSQL, mysqlCon);
                    MySqlDataReader mysqlReader = mysqlCmd.ExecuteReader();
                   

                   Console.WriteLine("Id\tName\tLastName");
                   Console.WriteLine("------------------------");

                    while (mysqlReader.Read())
                    {
                        Console.WriteLine(mysqlReader.GetInt32(0) + "\t" + mysqlReader.GetString(1) + "\t" + mysqlReader.GetString(2));
                        
                        
                    }

                }

                mysqlCon.Close();
                

               

            }
            catch (Exception er)
            {
                Console.WriteLine("An Error Occured" + er.Message);
                
                
            }

            Console.ReadKey();
            
        }
    }
}

Recommended Answers

All 6 Replies

Can the Console class even be used in web services?

You can return any object of array of objects (except for certain objects that implement certain interfaces, like IDictionary) from a web service.

If you're using it in a windows application then the web reference you create to connect with the service, this reference generates the objects used for input parameters and return values if they don't already exist (no need to generate a string class for example).

If you're using it for an ASP.Net service then the same thing applies, but these classes are generated in javascript, all you have to do then is have a scriptmanager that correctly references your service and you're set to go.

I am using it for an ASP.Net service.

what do u mean " a scriptmanager" ?

The ASP.Net Ajax tools have a <asp:Scriptmanager> tag.
If, however, you're not actually using this webservice for AJAX purposes but to call one website's functionality from another website then I imagine you will have to create a web reference and then probably the same thing applies as with a windows application.

thank's guys :)

Mark this thread 'Solved' if your question is answered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.