views:

262

answers:

2

I want to connect to database SQL2005 from ActionScript3, where i need to execute query statements from actionscript! can any one tell me please the code to do that ???? i searched about this issue and the result did'nt help me as the code is not workinf so i got confused.

A: 

try this code in adobe air

import flash.display.*;
import flash.data.*;
//import flash.data.SQLSchema;
import flash.events.SQLEvent;
import flash.events.SQLErrorEvent;
import flash.data.SQLConnection;
import flash.data.SQLStatement;
import flash.errors.SQLError;
//import flash.filesystem;

var conn:SQLConnection;
var dbStatement:SQLStatement;

 trace("sandeep mittal");
 init();

function init():void
{
    conn = new SQLConnection();
    conn.addEventListener(SQLEvent.OPEN, connOpenHandler);
    trace("sandeep mittal");
    dbStatement = new SQLStatement();
    dbStatement.sqlConnection = conn;
    dbStatement.text = "SELECT id, name, ssn FROM employees";

    var dbFile:File = new File(File.separator + "");
    conn.open(dbFile);
}

function connOpenHandler(e:SQLEvent):void
{
    trace("Sandeep Mittal");
    dbStatement.addEventListener(SQLEvent.RESULT, resultHandler);
    dbStatement.addEventListener(SQLErrorEvent.ERROR, errorHandler);
    dbStatement.execute();
}

function resultHandler(even:SQLEvent):void
{
    var result1:SQLResult = dbStatement.getResult();
    if (result1 != null)
    {
        var numRows:int = result1.data.length;
        for (var i:int = 0; i < numRows; i++)
        {
            var row:Object = result1.data[i];
            trace("id:", row.id, ", name:", row.name, ", ssn:", row.ssn);
        }
    }
}
function errorHandler(event:SQLErrorEvent):void
{
    trace("An error occured while executing the statement.");
}