Guest User

web service

a guest
Aug 1st, 2011
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Services;
  8. using System.Web.Services.Protocols;
  9. using System.Xml.Linq;
  10. using System.Data.SqlClient;
  11. using System.Collections.Generic;
  12.  
  13.  
  14. namespace WebService10
  15. {
  16. /// <summary>
  17. /// Summary description for Service1
  18. /// </summary>
  19. [WebService(Namespace = "http://tempuri.org/")]
  20. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  21. [ToolboxItem(false)]
  22. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
  23. // [System.Web.Script.Services.ScriptService]
  24.  
  25. public class Service1 : System.Web.Services.WebService
  26. {
  27.  
  28. [WebMethod]
  29.  
  30. public String getData()
  31. {
  32. var names = new List<string>();
  33.  
  34. SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123");
  35. try
  36. {
  37. myConnection.Open();
  38.  
  39. SqlCommand myCommand = new SqlCommand();
  40. myCommand.Connection = myConnection;
  41. myCommand.CommandText = "select name from names";
  42.  
  43. SqlDataReader myReader = myCommand.ExecuteReader();
  44.  
  45. while (myReader.Read())
  46. {
  47. names.Add(myReader["name"].ToString());
  48. }
  49. }
  50. catch (Exception ex)
  51. {
  52. Console.WriteLine(ex.Message);
  53. }
  54. finally
  55. {
  56. myConnection.Close();
  57. }
  58.  
  59. return string.Join("#", names.ToArray());
  60. }
  61.  
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment