|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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");
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);
Code:
var connStr = "Provider=SQLOLEDB;Server=mysite;Database=Northwind;User Id=MyId;Password=123aBc";
conn.open(connStr);
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;
}
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");
Code:
record.Open("select id, name, age, sex, score from student", conn, /*adOpenKeyset*/1, /*adLockPessimistic*/2);
Code:
record.moveFirst(); 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(); Code:
record.MoveLast(); Code:
record.MovePrevious();
if (record.bof()) record.MoveFirst();
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 |
|
|||
|
With Flash 8 + SWFKit Pro 3(SWFKit 3), you can use ADO directly in Action Script. Please try the following sample out.
http://www.swfkit.com/forum/attachments/adodb.zip |
|
|||
|
hi i am using swf kit 2.2. ur code is working to access data from ms access. but when i tried to update the database an error message is comng
I used this code: var conn = new ActiveXObject("ADODB.Connection"); connstr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = D:\\student.mdb"; conn.open(connstr); var record = conn.Execute("update stud set age = 30 where Id = 1"); fld = record.fields["age"]; trace(fld.value.toString()); conn.close(); And the errors are: ADODB.Fields.Item cannot be found in the collection corresponding to the requested name or ordinal. See also C:\WINDOWS\HELP\ADO270.CHM. Warning: using undefined varialbe "fld" Warning: using undefined property "Value" Warning; unknown method "toString" undefined Please help me this is in my project |
|
|||
|
The table name in your update statement "update stud set age = 30 where Id = 1" is incorrect. The table name should be "student" not "stud". Hence, the "execute" method will return an empty recordset, which does not contain an "age" field. When you try to get the value of the "age" field, it will say that the field cannot be found.
|
|
|||
|
Thank you for your immediate response.
But here 'stud' is my table name. I followed your suggestion but this time it is not retrieving data. When i use 'stud' in my statement it retrieving the data. so there is no problem with table name. Code: var conn = new ActiveXObject("ADODB.Connection"); connstr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = D:\\student.mdb"; conn.open(connstr); var record = conn.Execute("select Name,age from stud"); var fld = record.Fields["age"]; var sname = record.Fields["Name"]; sname.value = "vishnu"; conn.update(); trace(sname.value.toString()); trace(fld.value.toString()); conn.close(); Output: ADODB.Field:Current Recordset does not support updating. This may be a limitation of the provider, or of the selected locktype.. See also: C:\WINDOWS\HELP\ADO270.CHM. Warning: unknown method "update" Warning: unknown method "toString" undefined 30 In the above output 30 is from age in table 'stud'. But when i am trying to update the data its not working. INSERT is also not working. Please give me a solution. If possible please attach an example of database retrieving and updating in ms access. Thank you again for your quick response. |
|
|||
|
try this selection query
it works for me Code:
var record = new ActiveXObject("ADODB.Recordset");
record.open("select Name,age from stud", conn, 1, 2);
http://www.w3schools.com/ado/met_rs_open.asp |
|
|||
|
I'm trying to connect to a web based MySQL server but i cant figure out the syntax for the connection. I have;
Code:
var conn = new ActiveXObject("ADODB.Connection");
var str = "PROVIDER=sqloledb;Data Source=http://cell-lms.com;Integrated Security=SSPI;Database=celllms_romsuser;UID=celllms_trial;PWD=trial";
conn.Open(str);
Thanks Jon |
![]() |
Was this information helpful? Yes No
| Thread Tools | |
| Display Modes | |
|
|