0% found this document useful (0 votes)
56 views2 pages

1st Code

This document defines a web service class called BookServices that provides methods for querying a book database and returning results in JSON format. The GetBooksByTitle method searches the database for books matching part of a title and returns the results. The GetBooksByAuthor method searches for books matching part of an author name and also returns results. Both methods connect to the database, execute a query, populate a dataset, convert the results to a JSON string using a JavaScript serializer, and return the JSON string.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views2 pages

1st Code

This document defines a web service class called BookServices that provides methods for querying a book database and returning results in JSON format. The GetBooksByTitle method searches the database for books matching part of a title and returns the results. The GetBooksByAuthor method searches for books matching part of an author name and also returns results. Both methods connect to the database, execute a query, populate a dataset, convert the results to a JSON string using a JavaScript serializer, and return the JSON string.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1: using System; 2: using System.Web; 3: using System.Collections; 4: using System.Web.Services; 5: using System.Web.Services.Protocols; 6: using System.Data; 7: using System.Data.

Odbc; 8: using System.Web.Script.Serialization; 9: using System.Web.Script.Services; 10: 11: /// <summary> 12: /// Web services to query the book database. All methods return JSON data. 13: /// </summary> 14: [WebService(Description = "Web services to query the book database.", Namespace = "http://www.williamsportwebdeveloper.com/")] 15: [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 16: [ScriptService] 17: public class BookServices : System.Web.Services.WebService { 18: 19: public BookServices () { 20: 21: //Uncomment the following line if using designed components 22: //InitializeComponent(); 23: } 24: 25: [WebMethod(Description = "Gets the books matching part of a title.")] 26: [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 27: public string GetBooksByTitle(string strTitle) { 28: OdbcConnection objConnection = new OdbcConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Books"].ConnectionString); 29: OdbcCommand objCommand = new OdbcCommand("SELECT * FROM reading WHERE Title LIKE '%" + strTitle + "%' ORDER BY BookNum;", objConnection); 30: DataSet objDataSet = new DataSet(); 31: OdbcDataAdapter objDataAdapter = new OdbcDataAdapter(objCommand); 32: objDataAdapter.Fill(objDataSet, "reading"); 33: objConnection.Close(); 34: 35: // Create a multidimensional jagged array 36: string[][] JaggedArray = new string[objDataSet.Tables[0].Rows.Count][]; 37: int i = 0; 38: foreach (DataRow rs in objDataSet.Tables[0].Rows) 39: { 40: JaggedArray[i] = new string[] { rs["BookNum"].ToString(), rs["Title"].ToString(), rs["Author"].ToString() }; 41: i = i + 1; 42: } 43: 44: // Return JSON data 45: JavaScriptSerializer js = new JavaScriptSerializer(); 46: string strJSON = js.Serialize(JaggedArray); 47: return strJSON; 48: } 49: 50: [WebMethod(Description = "Gets the books matching part of an author's name.")]

51: [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 52: public string GetBooksByAuthor(string strAuthor) 53: { 54: OdbcConnection objConnection = new OdbcConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Books"].ConnectionString); 55: OdbcCommand objCommand = new OdbcCommand("SELECT * FROM reading WHERE Author LIKE '%" + strAuthor + "%' ORDER BY BookNum;", objConnection); 56: DataSet objDataSet = new DataSet(); 57: OdbcDataAdapter objDataAdapter = new OdbcDataAdapter(objCommand); 58: objDataAdapter.Fill(objDataSet, "reading"); 59: objConnection.Close(); 60: 61: // Create a multidimensional jagged array 62: string[][] JaggedArray = new string[objDataSet.Tables[0].Rows.Count][]; 63: int i = 0; 64: foreach (DataRow rs in objDataSet.Tables[0].Rows) 65: { 66: JaggedArray[i] = new string[] { rs["BookNum"].ToString(), rs["Title"].ToString(), rs["Author"].ToString() }; 67: i = i + 1; 68: } 69: 70: // Return JSON data 71: JavaScriptSerializer js = new JavaScriptSerializer(); 72: string strJSON = js.Serialize(JaggedArray); 73: return strJSON; 74: } 75: 76: }

You might also like