Pronunciation / adjective
Bringing something good and unforseen.
    Skip Navigation Links > Component Library > Data Object
   

Download the Fortunate.Data object to manage data-access to SQL or Access databases for your windows development and web development.



 

Fortunate.DataObject

Download the Fortunate Library Here:

Fortunate Library 32bit - Stand-Alone Install

Fortunate Library 64bit - Stand-Alone Install

Fortunate Library Solution - Source Code

I created the Fortunate.Data object to limit the amount of code that needed to be written to get any type of data object back and to make certain that data connections get closed when an error occurrs.

    The Fortunate.Data object may be used to perform the following common database tasks:
  • EscapeString

  • ExecuteNonQuerySQL

  • ExecuteScalarSQL

  • GetDataSet

  • AddTableToDataSet

  • OpenDataReader

  • CloseDataReader


The Fortunate.Data object will get the connection string and automatically determine if it's connecting to SQL Server or Access. It will then return any type of data object requested - DataSet, DataTable, DataReader, Scalar, etc.

When working with a web applicaton I like to place the Fortunate.Data object on the master page of the application and make it available to all my other pages from that one central location. I do so like this:

Master Page Code:
  1. Private _Data As Fortunate.Data
  2. Public ReadOnly Property DataObject() As Fortunate.Data
  3. Get
  4. Call InitializeDataObject()
  5. Return _Data
  6. End Get
  7. End Property
  8. Private Sub InitializeDataObject()
  9. If _Data Is Nothing Then
  10. Call InitializeApplicationObject()
  11. Me._Data = New Fortunate.Data
  12. '
  13. 'data1
  14. '
  15. Me._Data.DatabaseConnectionStringName = "MyDatabaseConnectionString"
  16. End If
  17. End Sub

Please note that the code above includes giving the Fortunate.Data object a ConnectionStringName which needs to be placed into the web.config file just like any other connection string:
  1. <add name="MyDatabaseConnectionString" connectionString="Data Source=MyServer\SQL2005;Initial Catalog=MyDatabase;Integrated Security=True" providerName="System.Data.SqlClient"/>

To access the ReadOnly DataObject property from a web page the page needs to know what type of master page is being used. Note the second line of code in this page which tells the page's code-behind what master page is being used:
  1. <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" EnableSessionState="True" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" title="Default" %>
  2. <%@ MasterType VirtualPath="~/masterPage.master" %>

Now in the code-behind accessing your database is as simple as creating your SqlCommand and calling the data object:
  1. '---Create the stored procedure
  2. Dim SQLCommand As New Data.SqlClient.SqlCommand()
  3. SQLCommand.CommandType = Data.CommandType.StoredProcedure
  4. SQLCommand.CommandText = "procCodeLibraryGetPagedEntries"
  5. '---Add the parameters
  6. With SQLCommand
  7. .Parameters.Add("@TotalEntries", Data.SqlDbType.Int).Direction = Data.ParameterDirection.Output
  8. .Parameters.Add("@PageIndex", Data.SqlDbType.Int).Value = _PageIndex
  9. .Parameters.Add("@PageSize", Data.SqlDbType.Int).Value = CType(PageSizeDropDownList.SelectedValue, Int32)
  10. End With
  11. '---Get the DataTable
  12. Dim DataTable As Data.DataTable = Master.DataObject.GetDataTable(SQLCommand, "tblPagedCodeEntries")
Microsoft Certified Professional   © 2017 Fortunate.  All rights reserved.
contact: justin@aboutfortunate.com