home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 32 / IOPROG_32.ISO / SOFT / SqlEval7 / MSOLAP / samples / Samples.exe / VbDSOWriteEnableCube / frmMain.frm (.txt) next >
Encoding:
Visual Basic Form  |  1998-10-30  |  15.0 KB  |  374 lines

  1. VERSION 5.00
  2. Begin VB.Form frmMain 
  3.    Caption         =   "Write Enable"
  4.    ClientHeight    =   2700
  5.    ClientLeft      =   48
  6.    ClientTop       =   276
  7.    ClientWidth     =   1884
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   2700
  10.    ScaleWidth      =   1884
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.CommandButton cmdWriteEnable 
  13.       Caption         =   "Write Enable Cube"
  14.       Height          =   492
  15.       Left            =   0
  16.       TabIndex        =   3
  17.       Top             =   2040
  18.       Width           =   1812
  19.    End
  20.    Begin VB.ComboBox cboCubeName 
  21.       Height          =   288
  22.       Left            =   0
  23.       TabIndex        =   2
  24.       Top             =   1680
  25.       Width           =   1812
  26.    End
  27.    Begin VB.ComboBox cboDatabaseName 
  28.       Height          =   288
  29.       Left            =   0
  30.       TabIndex        =   1
  31.       Top             =   960
  32.       Width           =   1812
  33.    End
  34.    Begin VB.TextBox txtServerName 
  35.       Height          =   288
  36.       Left            =   0
  37.       TabIndex        =   0
  38.       Text            =   "LocalHost"
  39.       Top             =   240
  40.       Width           =   1812
  41.    End
  42.    Begin VB.Label Label2 
  43.       Caption         =   "Select a cube to write enable"
  44.       Height          =   372
  45.       Left            =   0
  46.       TabIndex        =   6
  47.       Top             =   1320
  48.       Width           =   1812
  49.    End
  50.    Begin VB.Label Label1 
  51.       Caption         =   "Select a Database"
  52.       Height          =   252
  53.       Left            =   0
  54.       TabIndex        =   5
  55.       Top             =   720
  56.       Width           =   1812
  57.    End
  58.    Begin VB.Label Enter 
  59.       Caption         =   "Enter Server Name"
  60.       Height          =   252
  61.       Left            =   0
  62.       TabIndex        =   4
  63.       Top             =   0
  64.       Width           =   1812
  65.    End
  66. Attribute VB_Name = "frmMain"
  67. Attribute VB_GlobalNameSpace = False
  68. Attribute VB_Creatable = False
  69. Attribute VB_PredeclaredId = True
  70. Attribute VB_Exposed = False
  71. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  72. ' frmMain.frm - Write enables a selected cube. This sample is intended to be
  73. ' used in conjunction with the VbDSOCreateSmallCube example and the
  74. ' VbDSOWriteBack example.
  75. ' (C)Copyright 1998, Microsoft Corporation. All rights reserved.
  76. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  77. Option Explicit
  78. 'Declare the module level variables.
  79. Public m_dsoServer   As DSO.Server
  80. Public m_dsoDatabase As DSO.MDStore
  81. Public m_dsoCube     As DSO.MDStore
  82. '   Initialize - initialize the sample application
  83. Public Sub Initialize()
  84.     ' If the DSO server object has already been in use
  85.     '   then call CloseServer method
  86.     ' NOTE: CloseServer method releases all of the resources
  87.     '       acquired by DSO.  It is important to call this
  88.     '       method when the DSO server object is not needed
  89.     '       any longer
  90.     If Not m_dsoServer Is Nothing Then
  91.         m_dsoServer.CloseServer
  92.     End If
  93. End Sub
  94. '   ConnectToServer - create and connect to the DSO server
  95. Public Sub ConnectToServer()
  96.     ' create an instance of the server object
  97.     Dim dsoServer As DSO.Server
  98.     Set dsoServer = New DSO.Server
  99.     ' connect to the server
  100.     On Error GoTo err_connect
  101.     dsoServer.Connect frmMain.txtServerName
  102.                                         
  103.     ' store the reference to the server object
  104.     ' so that other methods can use it
  105.     Set m_dsoServer = dsoServer
  106. Exit Sub
  107. err_connect:
  108.     ' Failed to connect to the server.
  109.     ' Possible reasons:
  110.     '   - the Microsoft SQL Server OLAP Server is not running
  111.     '   - the machine on which the OLAP Server is running cannot be reached
  112.     '   - the machine on which DSO application is running is not connected
  113.     '       to the network
  114.     '   - you are not a member of the OLAP Administrators user group on the
  115.     '       server machine
  116.     MsgBox Err.Description
  117. End Sub
  118. '   WriteEnableCube - enables the Sales cube for write back
  119. '   Enabling a cube for writeback entails creation of a writeback partition.  Writeback
  120. '   partition is a ROLAP partition without any aggregations.  The source table of the
  121. '   writeback partition is called writeback table.  The writeback information is stored
  122. '   in this table.
  123. '   We will create the writeback table in the Foodmart sample database
  124. Public Sub WriteEnableCube()
  125.     ' get the cube's datasource
  126.     Dim dsoDatasource As DSO.DataSource
  127.     Set dsoDatasource = m_dsoCube.DataSources(1)
  128.     ' make sure that we are still connected
  129.     If dsoDatasource.IsConnected = False Then
  130.         MsgBox "Cannot create write enable cube. Connection to the datasource cannot be established."
  131.         Exit Sub
  132.     End If
  133.     ' what will be the writeback table name
  134.     Dim sWriteBackTableName As String
  135.     sWriteBackTableName = "Sample_Writeback_" & m_dsoCube.Name
  136.     ' get the connection to the datasource
  137.     Dim adoConnection As ADODB.Connection
  138.     Set adoConnection = dsoDatasource.Connection
  139.     ' create the writeback partition
  140.     Dim dsoWBPartition As DSO.MDStore
  141.     Set dsoWBPartition = m_dsoCube.MDStores.AddNew("Writeback")
  142.     ' set the writeback flag
  143.     dsoWBPartition.IsReadWrite = True
  144.     ' set the partition's source table
  145.     dsoWBPartition.SourceTable = dsoDatasource.OpenQuoteChar & sWriteBackTableName & dsoDatasource.CloseQuoteChar
  146.     ' set the partition's storage mode
  147.     ' NOTE: the writeback partition should not have any aggregations
  148.     dsoWBPartition.OlapMode = olapmodeRolap
  149.     ' begin transaction on the datasource connection
  150.     adoConnection.BeginTrans
  151.     ' create the writeback table
  152.     ' and map the partition levels and measures to this table
  153.     On Error GoTo Err_CreateWriteBackTable
  154.     CreateWriteBackTable dsoWBPartition, dsoDatasource, adoConnection, sWriteBackTableName
  155.     ' commit the transaction on the datasource
  156.     ' NOTE: the OLAP server uses a different connection to the
  157.     '       relational database, so we need to commit the
  158.     '       changes here
  159.     adoConnection.CommitTrans
  160.     ' save the writeback partition
  161.     dsoWBPartition.Update
  162.     ' process the write back partition
  163.     On Error GoTo Err_Process
  164.     dsoWBPartition.Process processFull
  165. Exit Sub
  166. Err_CreateWriteBackTable:
  167.     ' rollback transaction on the datasource
  168.     adoConnection.RollbackTrans
  169.     ' remove the writeback partition from the cube
  170.     m_dsoCube.MDStores.Remove dsoWBPartition.Name
  171.     MsgBox "Cannot create writeback table. " & Err.Description
  172.     Exit Sub
  173. Err_Process:
  174.     ' remove the writeback table
  175.     adoConnection.Execute "DROP TABLE " & sWriteBackTableName
  176.     ' remove the writeback partition from the cube
  177.     m_dsoCube.MDStores.Remove dsoWBPartition.Name
  178.     MsgBox "Cannot process the writeback partition. " & Err.Description
  179.     Exit Sub
  180. End Sub
  181. '   CreateWriteBackTable - create the writeback table
  182. '   The writeback table must have one column for each measure
  183. '   and level in the cube.  It also has two columns used by
  184. '   the OLAP server for storing auditing information
  185. '   This function creates the table and maps the writeback
  186. '   partition levels and measures to this table
  187. Private Sub CreateWriteBackTable(dsoWBPartition As DSO.MDStore, _
  188.                                  dsoDatasource As DSO.DataSource, _
  189.                                  adoConnection As ADODB.Connection, _
  190.                                  TableName As String)
  191.                                  
  192.                                  
  193.     Dim Progress
  194.     Dim nParitions As Integer
  195.                                  
  196.     ' drop the table if it already exists
  197.     On Error Resume Next
  198.     adoConnection.Execute "DROP TABLE " & TableName
  199.     Err.Clear
  200.     On Error GoTo 0
  201.     ' construct the create table statement
  202.     Dim sCreateTable As String
  203.     sCreateTable = "CREATE TABLE " & TableName & " "
  204.     sCreateTable = sCreateTable & " ( "
  205.     ' create two columns required by the OLAP server
  206.     ' for storing auditing information
  207.     sCreateTable = sCreateTable & _
  208.                     "MS_AUDIT_USER varchar(32), " & _
  209.                     "MS_AUDIT_TIME  DateTime"
  210.     ' create a column for each level
  211.     ' since the column names have to be unique
  212.     '   add the level OrdinalPosition to the level name
  213.     Dim dsoPartDimension As DSO.Dimension
  214.     Dim dsoPartLevel As DSO.Level
  215.     Dim sColumnName As String
  216.     For Each dsoPartDimension In dsoWBPartition.Dimensions
  217.         For Each dsoPartLevel In dsoPartDimension.Levels
  218.             If Not dsoPartLevel.LevelType = levAll And _
  219.                Not dsoPartLevel.IsDisabled And _
  220.                Not dsoPartLevel.SubClassType = sbclsVirtual Then
  221.                 ' get the column name
  222.                 sColumnName = GetColumnName(dsoPartLevel.Name & "_L" & CStr(dsoPartLevel.OrdinalPosition))
  223.                 ' update the create table statement
  224.                 sCreateTable = sCreateTable & ", " & _
  225.                                sColumnName & " " & GetColumnTypeDefinition(dsoPartLevel.ColumnType, dsoPartLevel.ColumnSize)
  226.                 
  227.                 ' map the partition level to this column
  228.                 dsoPartLevel.MemberKeyColumn = dsoWBPartition.SourceTable & _
  229.                                                "." & _
  230.                                                dsoDatasource.OpenQuoteChar & sColumnName & dsoDatasource.CloseQuoteChar
  231.             End If
  232.         Next
  233.     Next
  234.     ' create a column for each measure
  235.     ' and map the partition measure to this column
  236.     Dim dsoPartitionMeasure As DSO.Measure
  237.     For Each dsoPartitionMeasure In dsoWBPartition.Measures
  238.         ' get the column name
  239.         sColumnName = GetColumnName(dsoPartitionMeasure.Name)
  240.         ' update the create table statement
  241.         sCreateTable = sCreateTable & ", " & _
  242.                        sColumnName & " " & GetColumnTypeDefinition(dsoPartitionMeasure.SourceColumnType)
  243.         ' map the partition measure to this column
  244.         dsoPartitionMeasure.SourceColumn = dsoWBPartition.SourceTable & _
  245.                                            "." & _
  246.                                            dsoDatasource.OpenQuoteChar & sColumnName & dsoDatasource.CloseQuoteChar
  247.     Next
  248.     ' complete the create table statement
  249.     sCreateTable = sCreateTable & ")"
  250.     ' execute the create table statement
  251.     adoConnection.Execute sCreateTable
  252. End Sub
  253. '   GetColumnTypeDefinition - return a string describing the data type
  254. '                             that can be used in create table statement
  255. '   NOTE: this function works with Jet provider, other providers have different data type names
  256. Private Function GetColumnTypeDefinition(ColumnType As ADODB.DataTypeEnum, Optional ColumnSize As Integer = 50) As String
  257.     Select Case ColumnType
  258.         Case adChar, adVarChar, adWChar
  259.             GetColumnTypeDefinition = " VARCHAR (" & CStr(ColumnSize) & ") "
  260.         Case adCurrency
  261.             GetColumnTypeDefinition = " CURRENCY "
  262.         Case adInteger
  263.             GetColumnTypeDefinition = " INTEGER "
  264.         Case adSmallInt
  265.             GetColumnTypeDefinition = " SMALLINT "
  266.         Case adSingle
  267.             GetColumnTypeDefinition = " REAL "
  268.         Case adDouble
  269.             GetColumnTypeDefinition = " DOUBLE "
  270.         Case adDBTimeStamp, adDate
  271.             GetColumnTypeDefinition = " DATETIME "
  272.         Case Else
  273.             Debug.Assert False ' there is a new data type
  274.                                ' we need to update this function
  275.     End Select
  276. End Function
  277. '   GetColumnName   - create a column name based on the given string
  278. '                     get rid of spaces
  279. Private Function GetColumnName(ObjectName As String) As String
  280.     Dim s As String
  281.     Dim c As String
  282.     Dim i As Integer
  283.     For i = Len(ObjectName) To 1 Step -1
  284.         c = Mid(ObjectName, i, 1)
  285.         If c <> " " Then
  286.             ' legal char, use it
  287.             s = c + s
  288.         End If
  289.     Next
  290.     GetColumnName = s
  291. End Function
  292. '   txtServerName_KeyDown - Connects to a server and loads its databases into
  293. '   a combination box if the enter key is pressed on the Server Name boc.
  294. Private Sub txtServerName_KeyDown(KeyCode As Integer, Shift As Integer)
  295.     ' Was the enter key pressed?
  296.     If KeyCode = vbKeyReturn Then
  297.         ' Set the hourglass and clear the contents of the Database combo box
  298.         Screen.MousePointer = vbHourglass
  299.         cboDatabaseName.Clear
  300.         
  301.         On Error GoTo err_connect
  302.         ' Connect to this server.
  303.         ConnectToServer
  304.         ' Load up the Databases combo box with new values.
  305.         LoadDatabases
  306.         
  307. err_connect:
  308.         ' Enable the combo so the user can use it.
  309.         cboDatabaseName.Enabled = True
  310.         'Restore the mouse pointer.
  311.         Screen.MousePointer = vbDefault
  312.     End If
  313.         
  314. End Sub
  315. '   LoadDatabases() - Loads all of the database names on a server
  316. ' into a combo box so the user can select one.
  317. Private Sub LoadDatabases()
  318. Dim tmpDB As DSO.MDStore
  319.     For Each tmpDB In m_dsoServer.MDStores
  320.         cboDatabaseName.AddItem tmpDB.Name
  321.     Next
  322.     cboDatabaseName.Text = cboDatabaseName.List(0)
  323. End Sub
  324. '   LoadCubeNames() - Loads all of the cube names within the database
  325. '   into a combo box.
  326. Private Sub LoadCubeNames()
  327. Dim tmpCube As DSO.MDStore
  328.     For Each tmpCube In m_dsoDatabase.MDStores
  329.         cboCubeName.AddItem tmpCube.Name
  330.     Next
  331.     cboCubeName.Text = cboCubeName.List(0)
  332. End Sub
  333. ' cboCubeName_Click - Checks to see if the selected cube can be
  334. ' write-enabled
  335. Private Sub cboCubeName_Click()
  336.     'Get the cube
  337.     Set m_dsoCube = m_dsoDatabase.MDStores(cboCubeName.ListIndex + 1)
  338.     ' Is the cube write enabled?
  339.     If m_dsoCube.IsReadWrite = False Then
  340.         ' Is the cubes class Regular? (vice virtual)
  341.         If m_dsoCube.SubClassType = sbclsRegular Then
  342.             cmdWriteEnable.Enabled = True
  343.         Else
  344.             cmdWriteEnable.Enabled = False
  345.         End If
  346.     Else
  347.         cmdWriteEnable.Enabled = False
  348.     End If
  349. End Sub
  350. ' cboDatabaseName_Click() - Loads the cube names into their combo box.
  351. Private Sub cboDatabaseName_Click()
  352.     cboCubeName.Clear
  353.     Set m_dsoDatabase = m_dsoServer.MDStores(cboDatabaseName.ListIndex + 1)
  354.     LoadCubeNames
  355.     cboCubeName.Enabled = True
  356. End Sub
  357. ' cmdWriteEnable_Click - Call the write enable function.
  358. Private Sub cmdWriteEnable_Click()
  359.     Screen.MousePointer = vbHourglass
  360.         WriteEnableCube
  361.     Screen.MousePointer = vbDefault
  362.     MsgBox "Cube Write Enabled!"
  363.     cmdWriteEnable.Enabled = False
  364. End Sub
  365. ' Form_Load - Initializes the server and disables all of the controls
  366. ' except ther servers name.
  367. Private Sub Form_Load()
  368.     Initialize
  369.     txtServerName.Enabled = True
  370.     cboDatabaseName.Enabled = False
  371.     cboCubeName.Enabled = False
  372.     cmdWriteEnable.Enabled = False
  373. End Sub
  374.