【VB/.NET】Converting VB6 to VB.NET 【Part II】【之四】
第四部分原文DLLs,DAO,RDO,ADO,and AD.NET; the History of VB DBsIn the early versions of VB,there were no database controls,and databases were accessed by vendor-specific DLLs,but VB's power and ease of creating forms still made it a favorite among database programmers. One thing that made it so easy to create forms in VB was VBXs,the forerunners of ActiveX controls. In VB3,Microsoft added DAO (Data Access Objects),allowing easy access to ODBC databases,and a good thing was started. RDO (Remote Data Objects) came next. Where DAO focused on connecting to small Access-type databases,RDO targeted a different market,large databases such as MS SQL and Oracle,so to a large extent RDO complemented rather than replaced DAO. ADO was to be the technology to combine the two.Upgrading from the earlier DAO or RDO architectures will be difficult. The Data control that was typically used by DAO and RDO programmers is not supported by .NET,and the upgrade wizard just marks each place the data control is used with a comment that it is no longer supported. Following the comment to the documentation just leads to generic information about upgrading with no further help. I have had some success creating an ActiveX control in VB6,adding the data control to the new ActiveX control,then adding functions and subroutines to make the data control accessible to its container in the VB client program. The accessors looked like Public Sub SetDataBaseName(Name As String) Data.DatabaseName = Name End Sub Public Sub SetRecordSource(Source As String) Data.RecordSource = Source End Sub Public Sub Refresh() Data.Refresh End Sub Public Sub RecordSetAddNew() Data.Recordset.AddNew End Sub Public Sub RecordSetFields(Field As String,Name As String) Data.Recordset.Fields(Field) = Name End Sub
In the client VB program, Data1.RecordSet.AddNew was replaced with Data1.RecordSetAddNew, which was replaced with Data1.RecordSetFields "[Territory Num],RTrim(LTrim(txtTerritoryNum.Text)), and so on. This worked well for strings,other RecordSetFields needed for other types of fields,or RecordSetField declared using Variant as the value type,and detecting the proper type by using the VarType function. This works all right if you just want to use the Data control by itself,but the whole point of using the Data control is to bind it to controls such as the DBGrid. To make that work correctly would require making the custom control a true VB data source. This is beyond the scope of this article,and might not work once converted to .NET. For anyone wishing to try this (and it might work),search for "Creating a Data Source" in the MSDN help files for more information. I used a similar trick to convert a VB6 form to .NET. The form was designed to allow adding new records to an existing database. It could be considered as having three parts,a Data control for accessing the database,a DBGrid (from VB5) for displaying the records in the database,and a set of TextBoxes for inputting the values for the new record. I took the custom data control I created above and added a DBGrid (from VB5) to it. I included some resizing logic so that the grid always covered the entire control area. Next,I set the data source for grid to the data control and built the ActiveX control. I removed both the Data control and the DBGrid from the VB program I was converting,and replaced them with my new custom control containing both. It upgraded to VB.NET and ran fine. Again,being creative in using custom ActiveX controls can make otherwise impossible conversions fairly simple. ADO.NET is one of the biggest changes yet in VB database connectivity. In ADO,programs were meant to always be connected to the database (note that ADO could be used in a disconnected mode,but most VB6 programs used the normal connected mode). In some ways,this simplified database programming a bit because changes made to records and tables in the program were automatically updated in the database. It also causes a scalability issue because one of the things most database vendors charge for is the number of allowed simultaneous connections. Since ADO connections tend to exist for the entire time the VB program is running,a license is tied up as well. Sometimes this does not matter,but if the program has a lot of users,or even worse,is accessed over the Internet,tying up a license for an extended time like this can get very expensive. Having a large number of open connections to the database can slow overall performance as well. With ADO.NET,connections are opened to read,write,or update the database,and closed while the program or user works with the data. This causes a little more work for the programmer,but generates a slightly faster,and much more scalable program. One half-step way to get around a complete rewrite is to switch to .NET,but continue to use ADO instead of ADO.NET. This can be done because ADO is implemented as a COM object in msado15.dll. Like all COM objects .NET needs a wrapper to access the ADO DLL. The .NET framework includes a wrapper for this DLL called adodb.dll,but you can also create your own from any version of ADO by using the tlbimport.exe utility that was briefly mentioned last month. Another option is,as always,that if a form can be separated from the rest of the application,it can be converted into an ActiveX control and added to a .NET form. Another advantage to using one of these techniques is that ADO has a few functions that are not available in ADO.NET,most notably server-side cursors and ADO extensions (ADOX). I have already mentioned that moving from the connected ADO paradigm to the disconnected ADO.NET paradigm will give a boost in performance,and a big boost in scalability. There are other reasons to switch to ADO.NET,too. ADO uses recordsets that resemble tables; ADO.NET uses datasets that resemble databases. To access more than one table in ADO,the query forming the recordset had to do a join across the tables; in ADO.NET,the dataset can contain all the tables,as well as DataReleation objects,which resemble foreign keys in a real database. This can make complex data manipulations much easier. The old client-side Cursor functionality can still be found in the DataReader object. Another big difference is that while ADO can only load data into record sets,ADO.NET can also load data into many "normal" programming structures such as arrays and lists. Where ADO tables were accessed by forward only cursors,ADO.NET tables have indexers,and can be accessed like any other collection. Also,most of the .NET controls,even text boxes,allow data binding not to just text,but to other properties such as color(Note From the Author: This URL will only work if Visual Studio is installed on your computer)(see ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dnadvnet/html/vbnet02122002.htm). Finally,ADO.NET is based on XML,which makes moving data between objects and programs easier and more efficient; using XML for this instead of ADO's COM calls also means that data can pass through firewalls without configuration changes. Next UpThat completes our coverage of converting general and database code. Next month,in the final installment,we will cover ASP to ASP.NET conversion,and converting to VB.NET 2005 and C#; and conclude with some thoughts on when it makes sense to convert.
译文:DLLs,AD.NET:VB DBs 的历史在早期的VB里,是没有数据库控件的,而且要通过供应商提供的DLL文件来访问数据库。但是VB的性能以及创建窗体的简便使它仍然受数据库程序员的欢迎。VB中的VBX(Visual Basic Extension)使得创建窗体是如此的容易,它就是ActiveX控件的前身。在VB3里,微软加入了DAO(Data Access Object)使得访问ODBC数据库更加的方便,一个好东西就这么诞生了。接着RDO(Remote Data Objects)出现了。DAO主要是链接到小型接入式数据库,而RDO则针对另一个不同的市场,像MS SQL 和Oracle这样大型的数据库。因此RDO只是很大程度上弥补了DAO的空缺,而不仅是替代了它。ADO则是组合了两者的新技术。 从早期的DAO或者RDO架构升级是一件困难的事情。.NET不支持DAO和RDO程序员通常使用的数据库控件,同时,升级向导只是在使用了这个控件的地方标记了不再支持云云的注释。按照文档里的注释仅仅是获得了几乎没有价值的信息,并不会给升级带来多大的帮助。 在VB6里,我已经成功地创建一个AX控件,同时还加入了这些个数据控件添,以及一些函数、过程也加进去使得VB客户端程序中的容器能够访问它。这个访问器看起来就像这样:
Public Sub SetDataBaseName(Name As String) Data.DatabaseName = Name End Sub
Public Sub SetRecordSource(Source As String) Data.RecordSource = Source End Sub Public Sub Refresh() Data.Refresh End Sub Public Sub RecordSetAddNew() Data.Recordset.AddNew End Sub Public Sub RecordSetFields(Field As String,Name As String) Data.Recordset.Fields(Field) = Name End Sub
在VB的客户端程序里: Data1.RecordSet.AddNew
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |