2010-09-05

Database connection example code for REALbasic.

Here's example REALbasic code for making a connection to REAL Server 2009. The docs have similar code, but this is a bit more complete.

You can copy this code into a PushButton's Action event handler for REAL Studio 2010. Also works in a Web Application if you are using the newly announced Web Edition, in which case you put this code in the Clicked event handler of a Button.

This example assumes a TextField on your form named "dbStatus".

You can make some helper methods to assist with chores such as logging problems, but this code will at least get you started.


dim db as new REALSQLServerDatabase

db.Host="YourDomainNameOrIpAddressGoesHere"
db.port=4430
db.Encryption=128 // Always encrypt your connection when on an untrusted network.
db.UserName="YourUserNameGoesHere" // "admin"
db.Password="YourPasswordGoesHere"
db.DatabaseName = "YourDatabaseNameGoesHere"

if( db.Connect ) then
self.dbStatus.Text = "Success connecting to db server"
else // Error connecting
self.dbStatus.Text = "Error occurred when connecting to database server. Code: " + str(db.ErrorCode) + " " + db.ErrorMessage
end if

dim sql as String = "select * from YourTableGoesHere;"
dim rs as RecordSet = db.SQLSelect( sql)
if(db.Error) then
self.dbStatus.Text = "Error when running a SELECT. Code: " + str(db.ErrorCode) + " " + db.ErrorMessage
else // Else normal.
if( rs = nil ) then
self.dbStatus.Text = "ERROR - RecordSet is nil"
else
self.dbStatus.Text = "Record count = " + str( rs.RecordCount ) // Replace with more useful code.
end if
end if

db.Close

// ------------------------------------------
Exception
if( db <> nil) then
if( db.IsConnected ) then
db.Close
end if
end if

No comments:

Post a Comment