Friday, January 23, 2009

Using Advantage from Delphi Prism

I wrote my first Delphi Prism method today. Nothing crazy, just added a method to their existing ASP.NET example to verify our .NET Provider worked. It did. Our visual dataset designer does not currently work in Prism, as it has portions that make decisions based on the language, and it doesn't currently understand Oxygene. [correction: dataset creation works well. See my post correcting this error.]

It was kind of weird writing Pascal inside of Visual Studio. My hands kept trying to use Delphi keyboard shortcuts to start and control the debugger.  :)

In a few days we are going to release a white paper written by Cary Jensen about building ASP.NET applications using Delphi for .NET and Advantage. It covers the concept and benefits of using a "hand written" data layer as opposed to littering your pages with design-time components. The concept applies to Delphi Prism as well as Delphi for .NET.

I'm including my test method below as a starting point if you feel like experimenting with Prism and Advantage.

To use the code:

  • Open the Prism Simple ASP.NET example application.
  • Add a project reference to Advantage.Data.Provider.dll (right click the project in the Solution Explorer, Add Reference...)
  • Then just copy this text over all of the text in the Prism example index.aspx.pas
namespace;
 
interface
 
uses
  System,
  System.Data,
  System.Configuration,
  System.Web,
  System.Web.Security,
  System.Web.UI,
  System.Web.UI.WebControls,
  System.Web.UI.WebControls.WebParts,
  System.Web.UI.HtmlControls,
  Advantage.Data.Provider;
 
type
  IndexPage = public partial class(System.Web.UI.Page)
  protected
    method btnRefresh_Click(sender: System.Object; e: System.EventArgs);
    method QueryAdvantage;
  end;
 
implementation
 
method IndexPage.QueryAdvantage;
var
   Conn : AdsConnection;
   Cmd  : AdsCommand;
   Rdr  : AdsDataReader;
begin
   Conn := new AdsConnection;
   Conn.ConnectionString := 'data source=c:\;ServerType=local;TableType=ADT';
   
   try
      Conn.Open;   
      Cmd := Conn.CreateCommand;
      
      // Create temp table
      Cmd.CommandText := 'try drop table #quicktest;' +
                         'catch all end try;' +
                         'create table #quicktest( id integer, name cichar(20) );' +
                         'insert into #quicktest values( 1, ''Windham'' );' +
                         'insert into #quicktest values( 2, ''Reed'' )';
      Cmd.ExecuteNonQuery;         
      
      // Quick Query
      Cmd.CommandText := 'select * from #quicktest';
      Rdr := Cmd.ExecuteReader;
            
      // Spit out some data
      while ( Rdr.Read ) do
      begin
         Response.Write( 'ID: ' + Rdr[0].ToString + ' Name: ' + Rdr[1].ToString + '<br/>' );
      end;   
                
   finally
      Conn.Close;     
      Conn.Dispose;
   end;
   
end;
 
method IndexPage.btnRefresh_Click(sender: System.Object; e: System.EventArgs);
begin
 
  QueryAdvantage;
 
  try
    var lValue: Double := Double.Parse(edInput.Text, System.Globalization.NumberFormatInfo.InvariantInfo);
    
    edCtoC.Text := (lValue).ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
    edFtoC.Text := ((lValue - 32) / 1.8).ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
    edKtoC.Text := (lValue - 273.15).ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
    edCtoF.Text := (lValue * 1.8 + 32).ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
    edFtoF.Text := (lValue).ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
    edKtoF.Text := (lValue * 1.8 - 459.67).ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
    edCtoK.Text := (lValue + 273.15).ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
    edFtoK.Text := ((lValue + 459.67) / 1.8).ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
    edKtoK.Text := (lValue).ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
    edMItoKM.Text := (lValue / 0.621371192).ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
    edKMtoMI.Text := (lValue * 0.621371192).ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
  except
    edCtoC.Text := 'Invalid number';
    edFtoC.Text := 'Invalid number';
    edKtoC.Text := 'Invalid number';
    edCtoF.Text := 'Invalid number';
    edFtoF.Text := 'Invalid number';
    edKtoF.Text := 'Invalid number';
    edCtoK.Text := 'Invalid number';
    edFtoK.Text := 'Invalid number';
    edKtoK.Text := 'Invalid number';
    edMItoKM.Text := 'Invalid number';;
    edKMtoMI.Text := 'Invalid number';
    exit;
  end;
end;
 
end.

No comments:

Post a Comment