getTitles(): <!--WebService Language="VB" Class="Service1"--> Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Web.Services Public Class Service1 : Inherits WebService _ Public Function getTitles( _ ByVal title As String) As DataSet ' Make the database connection. Dim conn As New SqlConnection( _ "server=localhost; uid=sa;" & _ "password=; database=Pubs") ' Create the SQL and set the parameter. Dim sql As String = "SELECT * FROM " & _ "titles WHERE title LIKE @title" Dim comm As New SqlCommand(sql, conn) comm.Parameters.Add("@title", _ "%" & title & "%") ' Create a data adapter and data set. Dim dataAdapter As New SqlDataAdapter(comm) Dim ds As New DataSet() ' Fill the data set with the query results. conn.Open() dataAdapter.Fill(ds, "titles") conn.Close() ' Return the dataset. Return ds End Function End Class |
'-----conn and ds are defined globally----- Dim conn As New SqlCeConnection( _ "Provider=Microsoft.SQLServer.OLEDB.CE.1.0;" & _ "Data Source=\My Documents\BookStores.sdf") Dim ds As DataSet '------------------------------------------ Sub createStoreDB() ' if database does not exist, create one If Not File.Exists( _ "\My Documents\BookStores.sdf") Then Dim sqlEngine As New Engine( _ "Data Source=" & _ "\My Documents\BookStores.sdf") sqlEngine.CreateDatabase() Dim cmd As New SqlCeCommand( _ "CREATE TABLE Stores(storeID int " & _ "Primary Key NOT NULL, " & _ "storeName nvarchar(20))", conn) conn.Open() cmd.ExecuteNonQuery() cmd.CommandText = _ "CREATE TABLE Orders(storeID int, " & _ "title_id nvarchar(20), qty int)" cmd.ExecuteNonQuery() cmd.CommandText = _ "INSERT INTO Stores (storeID, " & _ "storeName) VALUES (1, " & _ "'Great BookStore')" cmd.ExecuteNonQuery() cmd.CommandText = _ "INSERT INTO Stores (storeID, " & _ "storeName) VALUES (2, " & _ "'Computer BookStore')" cmd.ExecuteNonQuery() conn.Close() End If End Sub |
Sub LoadStores() conn.Open() Dim reader As SqlCeDataReader Dim cmd As New SqlCeCommand( _ "SELECT * FROM Stores", conn) reader = cmd.ExecuteReader While reader.Read cboStoreID.Items.Add( _ reader.Item("storeID")) End While conn.Close() End Sub |
Private Sub cboStoreID_SelectedIndexChanged( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cboStoreID.SelectedIndexChanged conn.Open() Dim sql As String = _ "SELECT * FROM Stores WHERE storeID=" & _ cboStoreID.Items(cboStoreID.SelectedIndex) Dim cmd As New SqlCeCommand(sql, conn) Dim reader As SqlCeDataReader = cmd.ExecuteReader reader.Read() lblStoreName.Text = reader.Item("storeName") conn.Close() End Sub |
Private Sub cmdSearch_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cmdSearch.Click Dim ws As New TitlesWS.Service1() ' get the web service ds = ws.getTitles(txtSearch.Text) cboResult.DataSource = ds.Tables(0) cboResult.DisplayMember = "title" End Sub |
Private Sub cboResult_SelectedIndexChanged( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cboResult.SelectedIndexChanged ' display the information of the ' selected book. Dim row As DataRow row = ds.Tables("titles").Rows( _ cboResult.SelectedIndex) lblTitleID.Text = row.Item("title_id") lblPrice.Text = "$" & row.Item("price") txtNotes.Text = row.Item("notes") End Sub |
Private Sub cmdAdd_Click( ByVal sender As System.Object,ByVal e As System.EventArgs) _ Handles cmdAdd.Click '---add the title to the stores ORDER table conn.Open() Dim sql As String = "INSERT INTO Orders " &_ "(storeID, title_id, Qty) VALUES (" & _ cboStoreID.Items(cboStoreID.SelectedIndex) _ & ",'" & _ lblTitleID.Text & "'," & txtQty.Text & ")" Dim cmd As New SqlCeCommand(sql, conn) cmd.ExecuteNonQuery() MsgBox("Title added for " & lblStoreName.Text, _ MsgBoxStyle.Information, "Orders") conn.Close() End Sub |
Private Sub cmdRefresh_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cmdRefresh.Click '---displays the list of stores available conn.Open() Dim sql As String = "SELECT * FROM Stores" Dim cmd As New SqlCeCommand(sql, conn) Dim reader As SqlCeDataReader = _ cmd.ExecuteReader '---clears the listbox cboStoreIDs.Items.Clear() While reader.Read() cboStoreIDs.Items.Add( _ reader.Item("storeID")) End While conn.Close() End Sub |
Private Sub cboStoreIDs_SelectedIndexChanged( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cboStoreIDs.SelectedIndexChanged '---displays the orders of store selected conn.Open() Dim sql As String = _ "SELECT * FROM Orders WHERE storeID=" & _ cboStoreIDs.Items(cboStoreIDs.SelectedIndex) Dim cmd As New SqlCeCommand(sql, conn) Dim reader As SqlCeDataReader = _ cmd.ExecuteReader() '---clears the listbox cboOrders.Items.Clear() While reader.Read() cboOrders.Items.Add( _ reader.Item("title_id") & " - " & _ reader.Item("qty") & "-copy(ies)") End While conn.Close() End Sub |