How to connecting to databases using SWFKit(SWFKit 2 and SWFKit Pro 2)
1. Connect to the database
Before a database can be accessed from a SWFKit app, a database connection has to be established.
The steps to make a database connection
i)Creating an ADO.Connection object
Code:
var conn = new ActiveXObject("ADODB.Connection");
ii)Creating a connection string and open the connection
The connection string contains the provider, db name, etc.
E.g. To connect to a MS Access DB "c:\mydata\sample.mdb", the connection string is
Code:
var connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\mydata\sample.mdb";
conn.open(connStr);
To connect to a MS SQL SERVER DB "Northwind" on server "mysite", the connection string is
Code:
var connStr = "Provider=SQLOLEDB;Server=mysite;Database=Northwind;User Id=MyId;Password=123aBc";
conn.open(connStr);
SWFKit can connect to any database like VB, Delphi, VC or ASP can do.
How to ensure the connection has been made successfully?
You can handle the "ConnectComplete" event of the ADO.Connection object. The event fires when the connection is completed.
Code:
var conn = new ActiveXObject("ADODB.Connection");
conn.connectOK = false;
conn.ConnectComplete = function (err, status, cnt)
{
if (typeof err != "undefined")
{
trace(err.Description);
return;
}
else
{
trace("Connect complete!");
conn.connectOK = true;
}
trace("status: ", status.value);
}
var connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\mydata\sample.mdb";
conn.open(connStr);
if (!conn.connectOK)
{
trace("Failed to connect to the DB");
return;
}
2. Getting Data from the Database
After the connection has been made, you can get data from database using the ADO.Recordset object.
i) Creating the ADO.Recordset object
Code:
var record = new ActiveXObject("ADODB.Recordset");
ii) Opening the ADO.Recordset object with a specified command string
Code:
record.Open("select id, name, age, sex, score from student", conn, /*adOpenKeyset*/1, /*adLockPessimistic*/2);
iii) Moving to the start of the record set
Code:
record.moveFirst();
iv) Fetching Data
Code:
while (!record.eof)
{
trace(record.Fields(0).Value.toString());
trace(record.Fields(1).Value.toString());
trace(record.Fields(2).Value.toString());
trace(record.Fields(3).Value.toString());
trace(record.Fields(4).Value.toString());
record.moveNext();
}
Tips:
1) Move to the start of the record set
Code:
record.MoveFirst();
2) Move to the end of the record set
3) Move to the previous record
Code:
record.MovePrevious();
if (record.bof()) record.MoveFirst();
4) Move to the next record
Code:
record.MoveNext();
if (record.eof) record.MoveLast();
Links
ADO Reference
http://msdn.microsoft.com/library/de...dooverview.asp
ADO Tutorial
http://www.w3schools.com/ado/default.asp