mbed to MSSQL database example

26 Jan 2010


Just put together some code to get an mbed to populate an sql database.

the mbed access the website with a querystring and website uses the vb function Request.QueryString to write to the database.

here is the vb at the webserver

Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Get the values from Query string
'TextBox1.Text = Request.QueryString("ID")
'TextBox2.Text = Request.QueryString("TIME")

Dim myConnection As SqlConnection
Dim myCommand As SqlCommand

myConnection = New SqlConnection("server=localhost\SQLEXPRESS;Trusted_Connection=True;database=accesscontrol")
'establishing connection
'opening the connection
myCommand = New SqlCommand("INSERT INTO log VALUES ('" + Request.QueryString("ID") + "', " + Request.QueryString("TIME") + ", " + Request.QueryString("DOOR") + ")", myConnection)
Catch ex As Exception
TextBox1.Text = ex.ToString
End Try
End Sub
End Class



and the moded code from the ethernet example



#include "mbed.h"
#include "HTTPClient.h"
DigitalOut led(LED1);
HTTPClient http;
* Request a google search for HelloWorld and display the first 2000 characters
* of the page source on the serial terminal.
int main(void) {
char url[256];
char result[2000];
// Insert the search term into the URL
sprintf(url, "http://xxx.xxx.xxx.xxx/default.aspx?id=mbed&time=35&door=12");
// Request 2000 characters from the result. Default is 64.
http.get(url, result, 2000);
// Display the result
printf("%s\n", result);
// Work is done!
while(1) {
led = !led;

03 Jun 2010

Hi Tom:

I was just going to do a posting on connecting to a remote mySQL database when I ran onto your post. I guess you are using the Microsoft SQL server? Is that what MSSQL is?

Does the VisualBasic code only work with Microsoft stuff, or could it be modified for other SQL databases? I have written a VB program in 10 years so I don't have a clue as to how you'd get the VB code installed and running on a MSSQL server.


03 Jun 2010

Hi Doug

I haven't worked with mysql and windows, just linux and php. It looks like, from a quick google you can download a mysql driver from http://dev.mysql.com/downloads/connector/net/1.0.html to work with the above code.

you can download vb express 2010 for free from http://www.microsoft.com/express/downloads/


mssql 2008 for free from http://www.microsoft.com/express/database/

hope this helps


Regards Tom.


FYI Micosoft's sqlexpress is free to download and use

03 Jun 2010

Hi Tom:

Thanks for the quick response. Free is good. So maybe I can use your code to try this out.

How do you get the VB portion connceted to the server. Does the server execute the VB code as an ASP or something?

03 Jun 2010 . Edited: 03 Jun 2010

Hi Doug

It’s an asp web page with vb code behind it that takes the parameters passed and loads them into the database.

Regards Tom.

17 Jan 2012


My fingers itch. Maybe if i'm done with my current project i might build an MS-SQL program that can execute an stored procedure and get back a variale or table :)

17 Jan 2012

Hey Theo,

Donatien has done a good job of implementing the connect and write functions to a mySQL database. This code has been used at one of our test sites and has processed over 30,000 transactions without errors. I wished that the full implementation of the mySQL interface was implemented for mbed (that is, the ability to do SELECTS and receive database results back into the mbed module.

For my current application a transaction consists of measuring the time a beer tap is open, tallying flow-meter ticks and converting to ounces. Immediately after a tap closes a transaction packet is sent (mbed MAC addr, time/date stamp, tap number (1-8), flow-meter ticks, ounces, temperature (Celsius)). I use an INSERT to place it in the database. You can see the test database at www.keg-watch.net.

As an enhancement, I'd like to be able to interrogate the database and tables for size, last record number, etc. Also I'd like to do a CREATE table on a month date roll-over and have the mbed check to insure that the table was created properly and such. I assume much of this could be done with stored procedures.

If your fingers are "still itchy" maybe we can work on completing the SQL interface. Porting it to either MS-SQL or mySQL or PostgreSQL shouldn't be difficult after that. (Incidentally, some in the mySQL community are considering a move to PostgreSQL ever since Oracle got a hold of mySQL.)

Cheers, Doug

18 Jan 2012

Hey doug,

In the "real" world (my day job), i'm a Sr. MS-SQL DBA, responsible for all the MS-SQL servers in the whole datacentre. Hence my MS-SQL expertise.

Even though all SQL versions share the ANSI '83 or '92 SQL standards they are FAR from equal. Especially the variations in the TDS (tabular data set) sub-protocols/versions are killing.

And since MS-SQL has a really good freeware version, I think my efforts are best spent on the parts that i do know, intimately... on a daily basis... down to the bone / protocol / networking layer.

For the parts there is overlap, and i can help, i will try to.

And moving to postgress??? nya... i'd think twice about that one. It's called the P word in our office, and we do have to support the damn thing, but NO one wants to maintain it. The rest of us Oracle / MySql / NoSql / MsSQL / IBM DB2 / SyBase / etc etc DBA's at our office steer clear if we can help it.

And yes, offcourse i'm biased, But i know nice things can be built on the database the devil has spawned <evil grin>.

Theo (nl)