<% @ WebService Language = "vb" Class = "Sample" %>
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
<WebService(Namespace:="http://microsoft.com/webservices/")> _
Public Class Sample
Public connection As SqlConnection = New SqlConnection("Data Source=(local);Integrated Security=SSPI;Initial Catalog=Northwind")
<WebMethod( Description := "Returns Northwind Customers", EnableSession := False )> _
Public Function GetCustomers() As DataSet
Dim adapter As SqlDataAdapter = New SqlDataAdapter( _
"SELECT CustomerID, CompanyName FROM Customers", connection)
Dim custDS As DataSet = New DataSet()
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
adapter.Fill(custDS, "Customers")
Return custDS
End Function
<WebMethod( Description := "Updates Northwind Customers", EnableSession := False )> _
Public Function UpdateCustomers(custDS As DataSet) As DataSet
Dim adapter As SqlDataAdapter = New SqlDataAdapter()
adapter.InsertCommand = New SqlCommand( _
"INSERT INTO Customers (CustomerID, CompanyName) " & _
"Values(@CustomerID, @CompanyName)", connection)
adapter.InsertCommand.Parameters.Add( _
"@CustomerID", SqlDbType.NChar, 5, "CustomerID")
adapter.InsertCommand.Parameters.Add( _
"@CompanyName", SqlDbType.NChar, 15, "CompanyName")
adapter.UpdateCommand = New SqlCommand( _
"UPDATE Customers Set CustomerID = @CustomerID, " & _
"CompanyName = @CompanyName WHERE CustomerID = " & _
@OldCustomerID", connection)
adapter.UpdateCommand.Parameters.Add( _
"@CustomerID", SqlDbType.NChar, 5, "CustomerID")
adapter.UpdateCommand.Parameters.Add( _
"@CompanyName", SqlDbType.NChar, 15, "CompanyName")
Dim parameter As SqlParameter = _
adapter.UpdateCommand.Parameters.Add( _
"@OldCustomerID", SqlDbType.NChar, 5, "CustomerID")
parameter.SourceVersion = DataRowVersion.Original
adapter.DeleteCommand = New SqlCommand( _
"DELETE FROM Customers WHERE CustomerID = @CustomerID", _
connection)
parameter = adapter.DeleteCommand.Parameters.Add( _
"@CustomerID", SqlDbType.NChar, 5, "CustomerID")
parameter.SourceVersion = DataRowVersion.Original
adapter.Update(custDS, "Customers")
Return custDS
End Function
End Class