Skip to main content

Past Blast

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Sapphire Soltuions

Remote Data Access using the Winsock control

Written by Pete Vickers  [author's bio]  [read 86936 times]
Edited by Derek

Download the code

Page 1  Page 2  Page 3 

Retrieving the data

We just pass on a SQL string thus:-

Private Sub cmdretrieve_Click()
lstData.Clear
Sbuff = "READSelect * from employees~"
tcpClient.SendData Sbuff
End Sub

That is the easy bit. We now have to deal with the data, probably the most complex piece of code, so we will break it down into bite-size chunks.

Private Sub Process_Records()
Dim WkErr

lRecs = CLng(Mid(Rbuff, 7, 6))
iFields = CInt(Mid(Rbuff, 14, 4))

WkBuff = Mid(Rbuff, 19, Len(Rbuff) - 18)

As we outlined earlier, the first 6 characters are the function return (READOK), followed by 6 characters for the number of records, 4 characters for the number of fields, and then the data. Accordingly, we split out the buffer as above.

Data_Records = Split(WkBuff, "|")
Dim WkEmpno
Dim WkEmpFore
Dim WkEmpLast
Dim iFldNo As Integer
lstData.Visible = False

We separated our data with '|' prior to sending it, so we again use the split function to separate the data. 'Data_Records' is now an array, containing all our data fields. The arrayupper bound will equal our number of records multiplied by our number of fields.

Our table contained employee number, forename and surname, so our first record will be contained in Data_Records(0), Data_Records(1) and Data_Records(2).

When processing array data it is always a challenge to find an algorithm to deal with it easily. In this case, we can always say our loop starts at 0, and ends at (number of fields - 1).

Our step value is the number of fields. I have named the data items to (hopefully) make it a bit clearer. This also makes it close to the way of addressing fields in recordsets (rs.field(fldno).value). So we just loop through the Data_Records array, assigning our employee number, forneame and surname as we go. (We could just as easily use the array values directly, but I thought this way made it a bit clearer. I am probably wrong, but what the hell!!). There may be better ways of handling this - all suggestions welcome.

For iFldNo = 0 To (lRecs * iFields) - 1 _
  Step iFields
  WkEmpno = Data_Records(iFldNo)
  WkEmpFore = Data_Records(iFldNo + 1)
  WkEmpLast = Data_Records(iFldNo + 2)
  lstData.AddItem WkEmpno & ":" & _
  WkEmpFore & " " & WkEmpLast
Next
Erase Data_Records
lstData.Visible = True
End Sub

Fire up the server and the client, connect to the server, open the database, and hopefully you should see on the server.

and on the client

Even taking into account the vagaries of the winsock control, the results are fairly impressive. This is just a simple example. Try it against your own databases, with more data.

A full blown data server would cater for multiple clients, allow SQL insert update and delete statements, and probably run as a service, but this is just a primer to fire the imagination (some hopes). This has been tested 'wired' and wireless from an iPaq with no problems whatsoever. The sample eVB project contains the code for testing against the standard SQL Server test database 'pubs', but the code is commented out. Just comment out the Access statements and un-comment out the SQL Server statements to test it (how would you like me to teach you to suck eggs, Grandma?).

The test database and source code for both projects are available for download. In case you are wondering, the names from number 10 onwards, are from the squad of Bolton Wanderers (www.bwfc.co.uk).

Previous Page