Answered by:
can anyone explain a simple example of getting data from database using webservice

Question
-
User-1934105638 posted
can anyone explain a simple example of getting data from database using webservice with code
It is urgent. Please reply as soon as possible.
Thanks in Advance
Tuesday, September 24, 2013 7:32 AM
Answers
-
User839260933 posted
Hi
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; }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, September 24, 2013 8:26 AM -
User260886948 posted
Hi,
The reply from the @Sekhar can be a solution to get data from the database using web service.
Also, we can implement it by using the WCF data service.
Here are some examples for you to refer to:
#Tutorial for Creating WCF Data Services:
http://www.codeproject.com/Articles/572417/AplusBeginner-27splusTutorialplusforplusCreatingpl .#Walkthrough: Creating and Accessing a WCF Data Service:
http://msdn.microsoft.com/en-us/library/vstudio/cc668184.aspx .Best Regards.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 25, 2013 1:36 AM
All replies
-
User839260933 posted
Hi
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; }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, September 24, 2013 8:26 AM -
User-1934105638 posted
what code I have to write in operation Contract and what code in datacontract
I have declared a fun in interface defined in inherited class
but my function is not showing in my website.
Wednesday, September 25, 2013 1:29 AM -
User260886948 posted
Hi,
The reply from the @Sekhar can be a solution to get data from the database using web service.
Also, we can implement it by using the WCF data service.
Here are some examples for you to refer to:
#Tutorial for Creating WCF Data Services:
http://www.codeproject.com/Articles/572417/AplusBeginner-27splusTutorialplusforplusCreatingpl .#Walkthrough: Creating and Accessing a WCF Data Service:
http://msdn.microsoft.com/en-us/library/vstudio/cc668184.aspx .Best Regards.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 25, 2013 1:36 AM -
User503812343 posted
Below article will give you starting point for WCF with reading and writing data for database
http://dotnetmentors.com/how-to-start-with-creating-wcf-services-and-test-using-wcftestclient.aspx
Wednesday, September 25, 2013 4:56 PM