Using ADO.NET with Flash Remoting is extremely simple. In fact, the implementation is nearly the same as binding data to a DataGrid, DropDownList, or any of the bindable .NET Server Controls.
In this example application, you’ll connect to the Northwind Access database. This database typically comes as a free sample with Microsoft Access, but a copy is also available at
www.jasonmperry.com/downloads/northwind.zip. As you go through the article, it's a good idea to follow along with the finished source code, as the steps guide you through deconstructing it, rather than building it from scratch.
This implementation of ADO.NET uses the OleDb library. This library lets ADO.NET connect with SQL databases that support ODBC connectivity like Microsoft Access. The OleDb library also allows ADO.NET to connect to third-party databases like MySQL or Oracle. ADO.NET also contains a SqlClient library for connecting to Microsoft SQL Server databases. Using SqlClient provides a direct connection to a Microsoft SQL Server database and avoids the expensive ODBC layer. This provides big increases in speed.
To start development, open your preferred ASP.NET development environment (such as Dreamweaver) and do the following:
In SQLQueryToolAction.aspx, insert the following code to declare the ASP.NET development language you plan to use, register the Macromedia Flash Remoting server control, and import the namespaces needed to create your ADO.NET connection:
<%@ Page Language="C#" debug="true" %> <%@ Register TagPrefix=”MM” Namespace=”FlashGateway” Assembly=”flashgateway” %> <%@ import namespace=”System.Data” %> <%@ import namespace=”System.Data.OleDb” %> <html> <head> <title>jasonmperry.com Flash Remoting SQL Query Tool</title> </head> <body bgcolor=”#ffffff” text=”#000000”> </body> </html>
The main thing to notice in the above code snippet is the registration of the flashGateway server control. You install this control with Flash Remoting, and Flash Remoting requires it for ASP.NET to bind data to Flash. If you create a new site in Internet Information Server (IIS), you may need to place a copy of the flashgateway.dll (installed with Flash Remoting) in the /bin directory of your website. View the Flash Remoting documentation or Flash Remoting LiveDocs for more information on setting up IIS with Flash Remoting.
Next, insert the following code into the SQLQueryToolAction.aspx file to create a connection to the Northwind database using the ADO.NET OleDb library.
script language=”C#” runat=”server”>
void Page_Load( Object sender, EventArgs e)
{
string queryString ;
OleDbConnection connection;
connection = new OleDbConnection( @"Provider=Microsoft.Jet.OleDb.4.0; Data Source=Server.MapPath( “Northwind.mdb" );
connection.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter( queryString, connection );
DataSet dbData = new DataSet();
adapter.Fill( dbData, "Results" );
connection.Close();
}
</script>
This code passes a variable named queryString to your database. (Later, you'll get this as a parameter from your Flash application.) In this example, the Northwind Access database resides in the same directory as the ASP.NET page, but, for security reasons, it’s a good idea to place the database in a private directory that is inaccessible from the web. The .NET framework provides a MapPath() function to dynamically determine the full path to the database file.
When you run it, this page will populate the dataTable with the results of your database operation. To bind this data with Flash Remoting, you must use dataTable or another disconnected result set. The dataResult object is a connected data source and will throw an exception if used.
Implement the Flash Remoting connection by creating an instance of the flashGateway server control. This server control grants access to Flash parameters and lets you pass data to your Flash Application.
<body bgcolor=”#ffffff” text=”#000000”> <MM:Flash id=”Flash” runat=”server” /> </body>
Notice that the above code snippet uses the MM tag prefix you used when registering the flashGateway control. The MM:Flash code gives the Flash Remoting server control a unique ID to access parameters passed from Flash or to bind database results to the application. With this unique ID, you can determine if your Flash application has passed any parameters.
void Page_Load( Object sender, EventArgs e)
{
string queryString;
if( Flash.Params.Count > 0 )
{
queryString = Flash.Params[0].ToString();
}
In this example, Flash passes a query string parameter that it will execute against the Northwind database. The Flash document validates the query data. Since Flash uses the client’s resources and not the server's, this reduces the load on the server and makes the application more scalable.
At this point, the application executes the custom query string and creates a disconnected recordset. To pass this information to the Flash application, set the data source as the dataTable object and use the DataBind() function to send this data to Flash.
… adapter.Fill( dbData, "Results" ); Flash.DataSource = dbData.Tables[ "Results" ]; Flash.DataBind(); connection.Close(); …
Note: To view the full source code for this ASP.NET page, open the prebuilt SQLQueryToolAction.aspx located in the sample files download for this tutorial (available in the Requirements section).