8.3. Creating the HTTP Endpoint and Exposing a Web Method ExampleThe example in this section creates a stored procedure and defines an HTTP SOAP endpoint with a single web method that accesses the stored procedure. A .NET client is created to call the stored procedure via a web service call and to display the results. Follow these steps to create the endpoint and expose a web method:
Creating and managing HTTP endpoints is described in detail in the "Creating an HTTP Endpoint" section later in this chapter. You can confirm that the endpoint has been created by querying the catalog view sys.http_endpoints: SELECT * FROM sys.http_endpoints You can confirm the existence of the web method by querying the catalog view sys.endpoint_webmethods: SELECT * FROM sys.endpoint_webmethods Catalog views that contain information about the HTTP endpoints defined in the SQL Server instance are described in the "Endpoint Metadata" section later in this chapter. This permission controls whether a principal can see metadata for a specific endpoint: { GRANT | DENY | REVOKE | } ON ENDPOINT::endPointName TO server_principal This permission does not give the principal access to the endpoint. The next step is to consume this web method. The following example uses a C# .NET client. Follow these steps in Visual Studio 2005:
Figure 8-3. Results for web service example![]() 8.3.1. SQL BatchesWeb-service endpoints can be configured to support ad hoc queries. The BATCHES language-specific argument for SOAP in the CREATE ENDPOINT and ALTER ENDPOINT configures this. You execute an ad hoc query by calling the sqlbatch( ) method of the HTTP endpoint, passing in the queries (multiple queries must be separated with semicolons) and any parameters. In a SOAP SQL batch request, the SOAP <body> element contains a single, <sqlbatch>. This element has two child elements:
The following example demonstrates ad hoc query support by altering the preceding client. This new version returns the purchase orders for an employee using an ad hoc query instead of the GetPurchaseOrder( ) web method. Replace the code for the goButton_Click event handler with the following code: int employeeID = -1; try { employeeID = int.Parse(employeeIDTextBox.Text); } catch (Exception) { } // execute the query only if a valid employee ID is entered if (employeeID != -1) { GetPurchaseOrderWS.GetPurchaseOrderEndpoint proxy = new GetPurchaseOrderWS.GetPurchaseOrderEndpoint( ); proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; // SQL parameterized command that returns purchase orders for an employee string commandText = "SELECT * FROM Purchasing.PurchaseOrderHeader " + "WHERE EmployeeID = @employeeID FOR XML AUTO"; // create the employee ID parameter GetPurchaseOrderWS.SqlParameter[] parm = new GetPurchaseOrderWS.SqlParameter[1]; parm[0] = new GetPurchaseOrderWS.SqlParameter( ); parm[0].name = "employeeID"; parm[0].Value = employeeID; parm[0].sqlDbType = GetPurchaseOrderWS.sqlDbTypeEnum.Int; parm[0].direction = GetPurchaseOrderWS.ParameterDirection.Input; // call the web service sqlbatch method to execute the ad hoc query object[] results = proxy.sqlbatch(commandText, ref parm); // iterate over the array of objects returned from the web service and // handle each of them foreach (object o in results) { if (o.GetType( ).IsPrimitive) MessageBox.Show("Return Code = " + o); else switch (o.GetType().ToString( )) { case "System.Xml.XmlElement": // retrieve the XmlElement and convert to a DataSet System.Xml.XmlElement xmlResult = (System.Xml.XmlElement)o; System.Xml.XmlNodeReader xnr = new System.Xml.XmlNodeReader(xmlResult); DataSet ds = new DataSet( ); ds.ReadXml(xnr); PODataGridView.DataSource = ds.Tables[0]; break; case "GetPurchaseOrderSoapApp.GetPurchaseOrderWS.SqlRowCount": MessageBox.Show("RowCount = " + ((GetPurchaseOrderSoapApp.GetPurchaseOrderWS.SqlRowCount) o).Count); break; } } } The results are the same as in the previous example, except that you must specify an employee ID. |