Upgrading Data Environment

Data Environment is a Visual Basic 6.0 feature that provides an interactive, design time environment for creating programmatic, run-time data access. Visual Basic .NET provides an even more powerful environment for building database applications, but because the underlying data access library is conceptually different (Visual Basic .NET uses ADO.NET while Visual Basic 6.0 uses ADO), there is no direct mapping between Data Environment in Visual Basic 6.0 and data access features in Visual Basic .NET.

With Data Environment, Visual Basic 6.0 developers can accomplish the following tasks:

• Create ADO Connection objects.

• Create ADO Command objects that are based on stored procedures, tables, views, synonyms and SQL statements.

• Create hierarchies of commands that are based on a grouping of Command objects or by relating one or more Command objects together.

• Write and run code that reads and sets properties of objects hosted in Data Environment.

• Execute commands included in Data Environment as programmatic, run-time methods.

• Bind Form controls to commands hosted in Data Environment.

• Create aggregates that automatically calculate values within any command hierarchy.

When you use the upgrade wizard to upgrade an application that uses Data Environment, the following changes are made:

• For each Data Environment in your Visual Basic 6.0 project, a class with the name DataEnvironment prefixed to the Data Environment's name is created and a variable named with Data Environment's name is instanced in a module.

• For each connection and recordset hosted by a Data Environment object, a Public WithEvents member variable with the same name is created.

• For each command, a public method with the same name is created. It will be available in the same way that its respective command is available in Visual Basic 6.0.

For example, consider an application that has a Data Environment named DE with one connection named Access and a Command named Orders. When it is upgraded using the upgrade wizard, you obtain a class that looks like the following code example.

Module DataEnvironment_test_Module

Friend DE As DataEnvironment_DE = New DataEnvironment_DE() End Module

Friend Class DataEnvironment_DE

Inherits VB6.BaseDataEnvironment Public WithEvents Access As ADODB.Connection Public WithEvents rsOrders As ADODB.Recordset Private m_Orders As ADODB.Command

Public Sub New() MyBase.New()

Dim par As ADODB.Parameter Dim connectStr as String

Access = New ADODB.Connection()

connectString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source=C:\Temp\Northwind.mdb;Persist Security Info=False;" Access.ConnectionString = connectString m_Connections.Add(Access, " Access") m_Orders = New ADODB.Command()

rsOrders = New ADODB.Recordset() m_Orders.Name = "Orders"

m_Orders.CommandText = "SELECT OrderID, OrderDate FROM Orders" m_Orders.CommandType = ADODB.CommandTypeEnum.adCmdText rsOrders.CursorLocation = ADODB.CursorLocationEnum.adUseClient rsOrders.CursorType = ADODB.CursorTypeEnum.adOpenStatic rsOrders.LockType = ADODB.LockTypeEnum.adLockReadOnly rsOrders.Source = m_Orders m_Commands.Add(m_Orders, "Orders") m_Recordsets.Add(rsOrders, "Orders") End Sub

Public Sub Orders()

If Access.State = ADODB.ObjectStateEnum.adStateClosed Then

Access.Open() End If

If rsOrders.State = ADODB.ObjectStateEnum.adStateOpen Then rsOrders.Close() End If m_Orders.ActiveConnection = Access rsOrders.Open() End Sub End Class

All uses of Data Environment in the code are maintained with the same format and the same behavior; however, there is a big difference between a Data Environment object and this new class: you can not visually manipulate the new class as you can in Visual Basic 6.0. So, what happens if your code uses Data Environment objects with data binding? The data binding works fine, but you cannot add new commands visually. The upgrade process is discussed in the following section.

0 0

Post a comment