Borland® Delphi® 2005 Migration to .NET using VCL for .NET
by Bob Swart, Bob Swart Training & Consultancy
數據訪問
我們已經將一個VCL應用程序移植到.NET, 現在是時候來操作一個大點的項目了, 這次將會有更多的實際應用會遇到的問題. 這次代碼中包括數據訪問, 所以, 讓我們到Delphi7/Demos/Db 下看看. 在目錄的几個範例中, MastApp和IBMastApp是最大的, 有最多的unit(單元)和form(窗體).
如果你沒有安裝Delphi7, 那你也可打開BDS/3.0/Demos/DelphiWin32/Db目錄, 裹面同樣有MastApp 和 IBMastApp這兩個項目(但已經移植到新的項目格式了-包括bdsproj文件).
MastApp已經被移植到.NET了, 你能在BDS/3.0/Demos/Delphi.NET/DB/MastApp這個目錄下找到對應的版本, 而IBMastApp還沒移植到VCL for .NET, 所以, 你無法在BDS/3.0/Demos/Delphi.NET/DB or BDS/3.0/Demos/Delphi.NET/VCL/Db找到對應的版本.
所以, 作為我們一個重要的示範, 讓我們開始將IBMastApp應用程序從Win32移植到.NET下—一個看起來比較復雜的數據庫應用程序範例.
首先, 我們還是將必要的文件作個備份.
現在我們已經準備好了開始在一個新的IBMastApp項目上工作, 并將其移植到.NET了.
因為項目沒有包含有.bdsproj文件, Delphi 2005 IDE 需要詢問你來确定是陞級這個項目為Win32還是.NET的項目. 所以, 會彈出一個陞級對話框詢問你, 如下圖

在項目陞級對話框中選擇將mastapp陞級為.NET
我們會得到一個帶有.NET personality信息的mastapp.bdsproj文件. 現在保存文件, 新的個性信息將會保存到mastapp.bdspro文件.
移植數據模塊(Data Module)
現在我們已經將這個項目關聯為Delphi for .NET, 接下來首先要檢查的是數據庫控件是不是指向正确的數據聯接. 你必須在編繹之前做這個工作.當數據聯接配置正常時, 我們就能將精力集中在代碼的移植上.
數據模塊設計如下面屏幕截圖所示, 采用了InterBase Express作為數據訪問的架構
設計期的VCL for .NET數據模塊
我們必須重新設置Database控件, 在數據模塊的右下角可以找到它.

Database控件編繹框
注意,這裹的https://www.gofarlic.com/是文件路徑所在. 你可能要修改成C:/, 并且使用Common Files 代替路徑中的Borland才可以設置InterBase聯接到這個數據庫. 我默認是將mastsql.gdb安裝到C:/Program Files/Common Files/Borland Shared/Data/mastsql.gdb—你的設置可能有點不同
*注意在每一次你移植VCL項目到.NET時, 你可能都需要做這個動作: 第一, 确認數據模塊上的數據訪問控件能指到正确的數據庫. 并且,雖然大部分的數據庫控件都有對應的VCL for .NET的版本, 但是在.NET下不支持SQL鏈接, 所以, 你必須將這些項目移植改為使用dbExpress, dbGo for ADO, InterBaseExpress或者其它VCL for .NET的數據訪問技術.
移植源代碼
當數據模塊和指定的數據訪問控件修改成功後, 我們能開始第一次的項目編繹工作.
你將會得到大概17條警告信息, 還有一個錯誤信息, 如下所示:
[Warning] mastapp.dpr(23): W1005 Unit 'Borland.Vcl.Forms' is specific to a platform
[Warning] MAIN.PAS(6): W1005 Unit 'Borland.Vcl.Windows' is specific to a platform
[Warning] MAIN.PAS(6): W1005 Unit 'Borland.Vcl.Messages' is specific to a platform
[Warning] MAIN.PAS(6): W1005 Unit 'Borland.Vcl.Graphics' is specific to a platform
[Warning] MAIN.PAS(6): W1005 Unit 'Borland.Vcl.Controls' is specific to a platform
[Warning] MAIN.PAS(7): W1005 Unit 'Borland.Vcl.Forms' is specific to a platform
[Warning] MAIN.PAS(7): W1005 Unit 'Borland.Vcl.Dialogs' is specific to a platform
[Warning] MAIN.PAS(7): W1005 Unit 'Borland.Vcl.Buttons' is specific to a platform
[Warning] MAIN.PAS(7): W1005 Unit 'Borland.Vcl.StdCtrls' is specific to a platform
[Warning] MAIN.PAS(7): W1005 Unit 'Borland.Vcl.Menus' is specific to a platform
[Warning] MAIN.PAS(7): W1005 Unit 'Borland.Vcl.ExtCtrls' is specific to a platform
[Warning] DataMod.pas(8): W1005 Unit 'Borland.Vcl.Windows' is specific to a platform
[Warning] DataMod.pas(8): W1005 Unit 'Borland.Vcl.Messages' is specific to a platform
[Warning] DataMod.pas(8): W1005 Unit 'Borland.Vcl.Graphics' is specific to a platform
[Warning] DataMod.pas(8): W1005 Unit 'Borland.Vcl.Controls' is specific to a platform
[Warning] DataMod.pas(8): W1005 Unit 'Borland.Vcl.Forms' is specific to a platform
[Warning] DataMod.pas(8): W1005 Unit 'Borland.Vcl.Dialogs' is specific to a platform
[Fatal Error] DataMod.pas(9): F1026 File not found: 'VarUtils.dcuil'
我們可以先忽略警告的信息—因為大部分只是提醒我們現在在使用VCL for .NET的單元只能運行于特定的平台. 為了避免老是出現這此平台警告信息, 你能夠屏蔽掉它們.

項目管理選項—警告信息列表
[Fatal Error] DataMod.pas(9): F1026 File not found: 'VarUtils.dcuil'
引起這個錯誤的原因是VarUtils這個單元在于VCL for .NET中已經取消了(比較可能的是合并到Variants單元了).所以, 我們能夠將不在存在的VarUtils單元從uses 子句中移除.
*注意如果你想你的項目還能同時在Win32下編繹, 你可能更願意將VarUtils單元放置在一對{IFDEF WIN32} .... { IFDEF WIN32} .... { IFDEF WIN32} .... {ENDIF}區塊中, 而不是簡單的將它從uses 子句中移除掉.
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
DB, IBQuery, IBCustomDataSet, IBTable, IBDatabase, IB, Variants
{IFDEF WIN32}, VarUtils { IFDEF WIN32}, VarUtils { IFDEF WIN32}, VarUtils {ENDIF};