<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Jwalin Khatri &#187; Data Access Applicatin Block Example</title>
	<atom:link href="http://jwalin.wordpress.com/tag/data-access-applicatin-block-example/feed/" rel="self" type="application/rss+xml" />
	<link>http://jwalin.wordpress.com</link>
	<description>Don't limit yourself</description>
	<lastBuildDate>Wed, 14 Oct 2009 18:29:15 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='jwalin.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/499eff1ef6b26c8546dbe7bd01c0ac6c?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Jwalin Khatri &#187; Data Access Applicatin Block Example</title>
		<link>http://jwalin.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jwalin.wordpress.com/osd.xml" title="Jwalin Khatri" />
		<item>
		<title>Get Started with the Enterprise Library Data Access Application Block</title>
		<link>http://jwalin.wordpress.com/2009/02/11/get-started-with-the-enterprise-library-data-access-application-block/</link>
		<comments>http://jwalin.wordpress.com/2009/02/11/get-started-with-the-enterprise-library-data-access-application-block/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 20:12:14 +0000</pubDate>
		<dc:creator>jwalin</dc:creator>
				<category><![CDATA[ADO.Net]]></category>
		<category><![CDATA[Asp.Net]]></category>
		<category><![CDATA[DAAB]]></category>
		<category><![CDATA[DAAB Example]]></category>
		<category><![CDATA[Data Access Applicatin Block Example]]></category>
		<category><![CDATA[Data Access Application Block]]></category>
		<category><![CDATA[Enterprise Library Data Access Application Block]]></category>

		<guid isPermaLink="false">http://jwalin.wordpress.com/?p=109</guid>
		<description><![CDATA[Microsoft has redesigned version 2.0 of the Data Access Block to take advantage of new ADO.NET 2.0 features. You can download the Enterprise Library Data Access Application Block from MSDN.
1] Using Application Data Block (Change in Web.Config)
 To use the Data Access Block successfully, you will need to go through the steps listed below:
Add a reference [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jwalin.wordpress.com&blog=2317521&post=109&subd=jwalin&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Microsoft has redesigned version 2.0 of the Data Access Block to take advantage of new ADO.NET 2.0 features. You can download the <a href="http://msdn.microsoft.com/en-us/library/cc309504.aspx">Enterprise Library Data Access Application Block</a> from MSDN.</p>
<p><strong>1] Using Application Data Block (Change in Web.Config)<br />
</strong> To use the Data Access Block successfully, you will need to go through the steps listed below:</p>
<p>Add a reference to the Microsoft.Practices.EnterpriseLibrary.Common.dll and Microsoft.Practices.EnterpriseLibrary.Data.dll assemblies from your solution. You can do this by using the &#8220;Add Reference&#8221; option and navigating to the &lt;Drive Name&gt;:\Program Files\Microsoft Enterprise Library\bin folder.<br />
Add the necessary configuration entries to the web.config or app.config file or a custom configuration file. To this end, you add the below &lt;configSections&gt; element under the root &lt;configuration&gt; element.<br />
   <br />
         &lt;configSections&gt;<br />
            &lt;section   <br />
              name=&#8221;dataConfiguration&#8221;            <br />
              type=&#8221;Microsoft.Practices.          <br />
              EnterpriseLibrary.Data.<br />
              Configuration.<br />
              DatabaseSettings, <br />
              Microsoft.Practices.<br />
              EnterpriseLibrary.Data&#8221; /&gt;<br />
          &lt;/configSections&gt;</p>
<p>Then you also add the &lt;dataConfiguration&gt;&lt;configuration&gt; element as shown below:</p>
<p>         &lt;dataConfiguration         <br />
            defaultDatabase=<br />
            &#8220;AdventureWorksDB&#8221;/&gt;</p>
<p>In this example, I have marked AdventureWorks as the default database, declared separately under the &lt;connectionStrings&gt; element.<br />
          &lt;connectionStrings&gt;<br />
            &lt;add<br />
             name=&#8221;AdventureWorksDB&#8221;<br />
              providerName=<br />
              &#8220;System.Data.SqlClient&#8221;  <br />
              connectionString=<br />
              &#8220;server=localhost;<br />
              database=AdventureWorks;<br />
              UID=user;PWD=word;&#8221; /&gt;<br />
          &lt;/connectionStrings&gt;</p>
<p><strong>2]. Retriving value from the ExecuteDataReader</strong></p>
<p>      Database db = DatabaseFactory.CreateDatabase();<br />
       string sqlCommand = @&#8221;Select EmployeeID,<br />
  NationalIDNumber, LoginID, Title from  Employee &#8220;;<br />
       DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);<br />
       using (IDataReader reader = db.ExecuteReader(dbCommand))<br />
       {<br />
         //Bind with DataGrid<br />
       }               <br />
     }</p>
<p><strong>Retriving single row</strong></p>
<p> Here is the store procedure<br />
 Create Procedure GetEmployeeDetails<br />
 (<br />
      @EmployeeID int,<br />
      @NationalIDNumber nvarchar(15)<br />
        OUTPUT,<br />
      @LoginID nvarchar(256) OUTPUT,<br />
      @Title nvarchar(50) OUTPUT<br />
 )<br />
 AS<br />
      Select @NationalIDNumber =<br />
        NationalIDNumber,<br />
        @LoginID = LoginID,<br />
        @Title = Title from<br />
        Employee<br />
      Where EmployeeID = @EmployeeID<br />
 GO</p>
<p>Here is the code for it.</p>
<p>Database db = DatabaseFactory.CreateDatabase();<br />
string sqlCommand =&#8221;GetEmployeeDetails&#8221;;<br />
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);<br />
//DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);<br />
//dbCommand.CommandType = CommandType.StoredProcedure;<br />
db.AddInParameter(dbCommand, &#8220;EmployeeID&#8221;, DbType.Int32, 1);<br />
db.AddOutParameter(dbCommand, &#8220;NationalIDNumber&#8221;, DbType.String, 15);<br />
db.AddOutParameter(dbCommand, &#8220;LoginID&#8221;, DbType.String, 256);<br />
db.AddOutParameter(dbCommand, &#8220;Title&#8221;, DbType.String, 50);<br />
db.ExecuteNonQuery(dbCommand);<br />
Response.Write(&#8220;NationalID : &#8221; + db.GetParameterValue(dbCommand, &#8220;NationalIDNumber&#8221;) + &#8220;&lt;br&gt;&#8221;);<br />
<strong>3]. Retriving DataSet/DataTable</strong></p>
<p>       string   sqlCommand = @&#8221;Select EmployeeID,<br />
          NationalIDNumber, LoginID, Title from Employee &#8220;;<br />
       DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);<br />
       DataSet dst = db.ExecuteDataSet(dbCommand);</p>
<p> //Bind with Datagrid</p>
<p><strong>4]. Insert the records with Return Value<br />
</strong><br />
 CREATE PROCEDURE InsertRecordIntoCustomer<br />
 (<br />
    @CustomerID NCHAR(5),<br />
    @CompanyName VARCHAR(50)<br />
 )<br />
 AS<br />
 <strong>DECLARE @Result int<br />
</strong> IF EXISTS<br />
 (<br />
    SELECT<br />
       NULL<br />
   FROM<br />
       [Customers]<br />
   WHERE<br />
       [CustomerID] LIKE @CustomerID<br />
 )<br />
    BEGIN<br />
       SELECT @Result = -1<br />
       END<br />
 ELSE<br />
    BEGIN<br />
       INSERT INTO [Customers]<br />
   (<br />
   [CustomerID],<br />
   [CompanyName]<br />
   )<br />
   VALUES<br />
   (<br />
   @CustomerID,<br />
   @CompanyName<br />
   )<br />
       SELECT @Result = @@ERROR<br />
       END<br />
 <strong>RETURN @Result</strong><br />
 GO<br />
The C# code used to execute the above stored procedure is as follows:<br />
 try<br />
 {<br />
    // Create DataBase Instance<br />
    Database db = DatabaseFactory.CreateDatabase();<br />
    // Initialize the Stored Procedure<br />
    DBCommand dbCommand = db.GetStoredProcCommand(&#8220;InsertRecordIntoCustomer&#8221;);<br />
    // If There are output parameters, use ExecuteNonQuery only, better performance<br />
    dbCommand.AddInParameter(&#8220;@CustomerID&#8221;, DbType.String, &#8220;JohnY&#8221;);<br />
    dbCommand.AddInParameter(&#8220;@CompanyName&#8221;, DbType.String, &#8220;Microsoft&#8221;);<br />
    object Internalvalue = new object();<br />
    dbCommand.AddParameter(&#8220;@Result&#8221;, DbType.Int32, ParameterDirection.ReturnValue, &#8220;@Result&#8221;, DataRowVersion.Default,Internalvalue);<br />
    // Get output<br />
    int GetResult = 0;<br />
    // Execute Stored Procedure<br />
    db.ExecuteNonQuery(dbCommand);<br />
    GetResult = (int)dbCommand.GetParameterValue(&#8220;@Result&#8221;);<br />
    switch ( GetResult )<br />
    {<br />
       case 0:<br />
           Response.Write(&#8220;Record Inserted.&#8221;);<br />
           break;<br />
       case -1:<br />
           Response.Write(&#8220;Record Already Found.&#8221;);<br />
           break;<br />
       default:<br />
          Response.Write(&#8220;Record Not Inserted.&#8221;);<br />
          break;<br />
    }<br />
 }<br />
 catch (Exception ex)<br />
 {<br />
    Response.Write(ex.ToString());<br />
 }<br />
<strong>5]. Example with Tansactions</strong></p>
<p>   Database db = DatabaseFactory.CreateDatabase();<br />
  <br />
   //Two operations, one to add the order and another to add order details<br />
   string sqlCommand = &#8220;InsertOrder&#8221;;<br />
   DbCommand orderCommand = db.GetStoredProcCommand(sqlCommand);<br />
   //Add InsertOrder parameters<br />
  <br />
   sqlCommand = &#8220;InsertOrderDetails&#8221;;<br />
   DbCommand orderDetailsCommand = db.GetStoredProcCommand(sqlCommand);<br />
   //Add InsertOrderDetails parameters</p>
<p>   using (DbConnection connection = db.CreateConnection())<br />
   {<br />
     connection.Open();<br />
     DbTransaction transaction = connection.BeginTransaction();<br />
     try<br />
     {<br />
       //Execute the InsertOrder<br />
       db.ExecuteNonQuery(orderCommand, transaction);<br />
       //Execute the InsertOrderDetails   <br />
       db.ExecuteNonQuery( orderDetailsCommand, transaction);<br />
       //Commit the transaction<br />
       transaction.Commit();                   <br />
     }<br />
     catch<br />
     {<br />
       //Roll back the transaction.<br />
       transaction.Rollback();<br />
     }<br />
   } </p>
<p><strong>6]. Retrieve a Single Field<br />
</strong> <br />
 CREATE PROCEDURE [dbo].[GetCustomerSingleColumn]<br />
 (<br />
    @CustomerID NCHAR(5)<br />
 )<br />
 AS<br />
 SET NOCOUNT ON<br />
    SELECT<br />
       [CompanyName]<br />
    FROM<br />
       [Customers]<br />
    WHERE<br />
       [CustomerID] LIKE @CustomerID<br />
 GO<br />
The C# code used to execute the above stored procedure is as follows:<br />
 try<br />
 {<br />
    // Create DataBase Instance<br />
    Database db = DatabaseFactory.CreateDatabase();<br />
    // Initialize the Stored Procedure<br />
    DBCommand dbCommand = db.GetStoredProcCommand(&#8220;GetCustomerSingleColumn&#8221;);<br />
    dbCommand.AddInParameter(&#8220;@CustomerID&#8221;, DbType.String, &#8220;ALFKI&#8221;);<br />
    //Execute the stored procedure<br />
    string GetCompanyName = (string) db.ExecuteScalar(dbCommandWrapper);<br />
    //Display results of the query<br />
    string results = string.Format(&#8220;Company Name : {0}&#8221;,GetCompanyName);<br />
    Response.Write(results);<br />
 }<br />
 catch (Exception ex)<br />
 {<br />
    Response.Write(ex.ToString());<br />
 }</p>
<p>Search following text in Google you can find lots of articles about it <br />
&#8221; how to get the return value from store procedure in application data block dbcommand &#8220;</p>
<p>Happy Programming !!!!</p>
Posted in ADO.Net, Asp.Net Tagged: ADO.Net, Asp.Net, DAAB, DAAB Example, Data Access Applicatin Block Example, Data Access Application Block, Enterprise Library Data Access Application Block <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jwalin.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jwalin.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jwalin.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jwalin.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jwalin.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jwalin.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jwalin.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jwalin.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jwalin.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jwalin.wordpress.com/109/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jwalin.wordpress.com&blog=2317521&post=109&subd=jwalin&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://jwalin.wordpress.com/2009/02/11/get-started-with-the-enterprise-library-data-access-application-block/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e4be699abbb541e9c09cfcfce329f194?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jwalin</media:title>
		</media:content>
	</item>
	</channel>
</rss>