Send to Printer


ST 4U 140: Connecting to a Database with VA Smalltalk

October 3, 2011 8:12:49.496

Today's Smalltalk 4 You looks at basic database connectivity in VA Smalltalk - what to load, and how to connect. In future screencasts, we'll look at using the connection for things like queries. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

Database Connectivity.

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:

Today we'll look at basic database connectivity in VA Smalltalk. Rather than using the parts based connector technology, we'll be diving into the code. To get started, you'll want to load the database support into VA; in the screen capture below, we show the ODBC connectivity (and example) packages loaded. Note that you need to define an ODBC connection, and have a database locally accessible:

Database Connectivity

Next, you'll need to set up the connection, using code like this:

"Create the Connection Specification"
conSpec := AbtDatabaseConnectionSpec
forDbmClass: #AbtOdbcDatabaseManager
dataSourceName: 'winlocal'.
dict := (Dictionary new).
	at: 'winlocal' put: (conSpec dataSourceName);
	at: 'DBManager' put: (conSpec dbmClass);
	at: 'Prompt?' put: (conSpec promptEnabled).

That sets up a connection specification, saving it under the logical name winLocal. We can then refer to this connection specification by name in our code. Next, if you want to login with a prompt, simply do this:

connection := conSpec connect.

You should see this:

Database Connectivity

If you want to save your login information in the same way you stored the connection information, that's simple enough as well:

"Create the Logon specification"
logonSpec := AbtDatabaseLogonSpec
	id: username
	password: password
	server: nil.
	registerLogonSpec: logonSpec
	withAlias: 'winlocalSpec'.

Make sure you pass in the proper username and password. To connect using connection and login spec (and thus, getting no login prompt), do the following:

"connects with the alias"
connection := conSpec connectUsingAlias: 'winlocalSpec'.

Finally, after working with your database, make sure you close the connection:

connection disconnect.

We'll look at actually using the connection for things like querying in a future tutorial.

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Technorati Tags: , ,

[st4u140-iPhone.m4v ( Size: 5089211 )]

posted by James Robertson

 Share Tweet This