Categories: Application Blocks, Application Blocks, Enterprise Library, Enterprise Library Posted by AlexanderZeitler on 2/2/2005 9:30 PM | Comments (0)

Update zu "Einführung in die Enterprise Library - Data Access Application Block / Configuration Application Block":

Natürlich kann auch mit dem neuen DAAB mit Typed DataSets oder existierenden DataSets gearbeitet werden.

Ich habe mir aus der heute nachmittag vorgestellten SP ein ProductDataSet erzeugt. Um dieses mit Daten aus der SP zu befüllen und die Daten auszugeben, wäre dann folgender Code notwendig:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace AdventureWorks
{
    /// <summary>
    /// Zusammenfassung für typedDataSet.
    /// </summary>
    public class typedDataSet : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {
            StringBuilder sbOutput = new StringBuilder();
            foreach(ProductDataSet.ProductsRow dr in GetProductsTyped().Tables[0].Rows) {
                sbOutput.Append(dr.Name);
                sbOutput.Append("&nbsp;");
                sbOutput.Append(dr.ProductNumber);
                sbOutput.Append("<br/>");
            }
            Response.Write(sbOutput.ToString());
        }

        private ProductDataSet GetProductsTyped() {
            ProductDataSet ds = new ProductDataSet();
            Database db = DatabaseFactory.CreateDatabase();
            DBCommandWrapper dbCommandWrapper = db.GetStoredProcCommandWrapper("GetProducts");
            db.LoadDataSet(dbCommandWrapper, ds, "Products");
            return ds;
        }


        #region Vom Web Form-Designer generierter Code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: Dieser Aufruf ist für den ASP.NET Web Form-Designer erforderlich.
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}

Natürlich kann die DataBaseFactory des DAAB auch einen DataReader zurückliefern, wie der Beispielcode des Data Access Application Block Quickstart zeigt:

public string GetCustomerList()
{
// DataReader that will hold the returned results        
// Create the Database object, using the default database service. The
// default database service is determined through configuration.
Database db = DatabaseFactory.CreateDatabase();

string sqlCommand = "Select CustomerID, Name, Address, City, Country, PostalCode " +
                "From Customers";
DBCommandWrapper dbCommandWrapper = db.GetSqlStringCommandWrapper(sqlCommand);

StringBuilder readerData = new StringBuilder();

// The ExecuteReader call will request the connection to be closed upon
// the closing of the DataReader. The DataReader will be closed
// automatically when it is disposed.
using (IDataReader dataReader = db.ExecuteReader(dbCommandWrapper))
{
// Iterate through DataReader and put results to the text box.
// DataReaders cannot be bound to Windows Form controls (e.g. the
// resultsDataGrid), but may be bound to Web Form controls.
while (dataReader.Read())
{
// Get the value of the 'Name' column in the DataReader
readerData.Append(dataReader["Name"]);
readerData.Append(Environment.NewLine);
}
}

return readerData.ToString();
}

Die Verwendung von Parametern für Stored Procedures zeigt die Methode GetProductsByCategory(int Category) - ebenfalls im Quickstart enthalten:

public DataSet GetProductsInCategory(int Category)
{
// Create the Database object, using the default database service. The
// default database service is determined through configuration.
Database db = DatabaseFactory.CreateDatabase();

string sqlCommand = "GetProductsByCategory";
DBCommandWrapper dbCommandWrapper = db.GetStoredProcCommandWrapper(sqlCommand);

// Retrieve products from the specified category.
dbCommandWrapper.AddInParameter("@CategoryID", DbType.Int32, Category);

// DataSet that will hold the returned results        
DataSet productsDataSet = null;

productsDataSet = db.ExecuteDataSet(dbCommandWrapper);

// Note: connection was closed by ExecuteDataSet method call

return productsDataSet;
}

Ebenfalls definiert werden können OutputParameter, hierzu verwendet man

dbCommandWrapper.AddOutParameter(string ParameterName, DbType dbType, int Size);

Comments are closed