Get Started with the Enterprise Library Data Access Application Block
page 5 of 11
by Bilal Haidar
Feedback
Average Rating: 
Views (Total / Last 10 Days): 98677/ 58

Retrieve More than One Column in a Row

The code sample above retrieves an entire record from a database table. Sometimes however we need only specific fields from a row in a database table. In this case we need to supply the stored procedure with not only an input parameter as in the previous section, but also one or more output parameters to store the result of the stored procedure.

The following is an example of a stored procedure which will take as input the CustomerID value and return only two fields: CompanyName and ContactName. For this limited result set, we have to use the AddOutputParameter() method. This method is used to return the output of a stored procedure.

It is recommended when executing a stored procedure with output parameters to use the ExecuteNonQuery() method instead of the ExecuteDataReader() method.

The SQL stored procedure is as follows:

Listing 6

CREATE PROCEDURE [dbo].[GetCustomerMultipleFields]
(
   @CustomerID NCHAR(5),
   @CompanyName NVARCHAR(40) OUTPUT,
   @ContactName NVARCHAR(30) OUTPUT
) 
AS
SET NOCOUNT ON
   SELECT
      @CompanyName = [CompanyName],
      @ContactName = [ContactName]
   FROM
      [Customers]
   WHERE
      [CustomerID] LIKE @CustomerID
GO

The following C# code executes the above stored procedure and uses the AddOutputParameter() method to obtain the results:

Listing 7

try
{
   // Create DataBase Instance
   Database db = DatabaseFactory.CreateDatabase();
   // Initialize the Stored Procedure
   DBCommandWrapper dbCommandWrapper = db.GetStoredProcCommandWrapper("GetCustomerMultipleFields");
   dbCommandWrapper.AddInParameter("@CustomerID", DbType.String, "ALFKI");
   dbCommandWrapper.AddOutParameter("@CompanyName", DbType.String, 40);
   dbCommandWrapper.AddOutParameter("@ContactName", DbType.String, 30);
   //Execute the stored procedure
   db.ExecuteNonQuery(dbCommandWrapper);
   //Display results of the query
   string results = string.Format("Company Name : {0}, Contact Name {1},",
        dbCommandWrapper.GetParameterValue("@CompanyName"),
        dbCommandWrapper.GetParameterValue("@ContactName"));
   Response.Write(results);
}
catch (Exception ex)
{
   Response.Write(ex.ToString());
}

In the above code, I passed a customer ID value to the stored procedure as an input parameter. As the stored procedure is designed to return two values, CompanyName and ContactName, these were identified as output parameters.

To display the returned values we used another method of dbCommandWrapper, which is GetParameterValue(“ParameterName”). This method returns the value of the parameter specified.


View Entire Article

User Comments

Title: rt   
Name: rt
Date: 2012-10-23 2:40:03 AM
Comment:
tt
Title: net_beginner   
Name: rgent
Date: 2011-11-05 8:38:21 AM
Comment:
Very helpful and clear article, Thanks!!!
Title: Sr. .NET Developer   
Name: Loften Pierce
Date: 2011-04-26 3:34:21 PM
Comment:
Great article! Very clear and concise!
Title: MELibrary   
Name: shiva
Date: 2011-04-16 4:38:37 AM
Comment:
Excelent Work Brother
Title: source code   
Name: phani
Date: 2011-02-14 12:06:50 AM
Comment:
Hi
Your article is good , But it is not working properly in enterprise library 5.0 , can you prepare an article on enterprise library 5.0 for us like beginners in Enterprise Library 5.0

Thank you,
Phani
Title: source code   
Name: Ahmed
Date: 2011-01-12 12:57:10 PM
Comment:
Hi Bilal,

I tried using your code for testing but I can't open it in VS2008, can you please help?

Thanks
Ahmed
Title: Query!!!   
Name: Dipti
Date: 2010-04-28 3:14:56 AM
Comment:
Hi i am new to Microsoft Enterprise Library, i am using it for My DAL in my application. i am not aware any library of microsoft etrprise is containing DbCommand, DbConnection etc..Can you tell me please which name space do i have to import or i am suppose to use System.Data.Common library..
Thanks!!
Please Answer
(04cp271dipti@gmail.com)
Title: Syntax error.   
Name: Dipal Bhavsar
Date: 2009-12-16 11:50:02 PM
Comment:
Syntax wrong.
Use Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
instead of
using Microsoft.Practices.EnterpriseLibrary.Configuration;
Title: EntLib   
Name: Michael Doug
Date: 2009-07-26 2:30:21 AM
Comment:
Hey is a great article for learning and developing Data Access Application Block.
Title: good   
Name: Abhishek Jha
Date: 2009-07-09 7:49:04 AM
Comment:
Thanks a lot u have solved a great deal of problem i have getting in learning about enterprise library,good going
Title: Enterprise Library Wrapper Class to Implement Run-time Dynamic Connection Strings Without Using Config File   
Name: Raja Lakshman
Date: 2009-06-11 8:25:36 PM
Comment:
Have a look at the above URL (http://www.codeproject.com/KB/dotnet/EntLibWrapper.aspx) to use the DAAB in Enterprise Library with run-time dynamic connection strings.

Hope this helps.

Cheers
Title: SQL Server authentication   
Name: gulfraz
Date: 2009-02-25 8:34:13 AM
Comment:
Hi Bilal!
In my desktop application, I read an xml config file and build the connection string at runtime by using an encrypted password. The connection uses SQL Server authentication. How can I set the connection string at runtime to use sql server authentication with Enterprise Library.


Thank You
Title: Get Started with the Enterprise Library Data Access Application Block   
Name: Mrugesh
Date: 2009-01-06 4:57:54 AM
Comment:
This is very helpful article for dotnet users new with Enterprise Library.
Title: entlib with oracle parameter   
Name: Mahir
Date: 2008-11-12 2:43:58 AM
Comment:
I am facing problem with parameter in oracle. I don't know the exact reasons. The value is not passing to the procedure.
Title: Well done   
Name: Tanweer Hussain
Date: 2008-10-28 6:24:36 PM
Comment:
Even the aricel is based on old Enterprise Library. I felt his article is making life easier,
Title: Re: Raji   
Name: Bilal Haidar
Date: 2008-09-18 3:30:56 PM
Comment:
Well, this article was on an old version of the library.

Check the documentation on the Ent. Lib 4.0, they have good resources!

Regards
Title: No Title   
Name: Raji
Date: 2008-09-18 3:22:26 PM
Comment:
This is very old. For example, DBCommandWrapper no longer works. It is very frustrating to find limited resources about the Enterprise Library, and if you happen to find it, it turns out to be too old and no longer applies to the current version.
Title: Little Candle in The Dark   
Name: Gatot 'Gatz' Wahyudi
Date: 2008-09-02 5:01:23 AM
Comment:
Dear Haidar,

Thank you so much for your helpful article. Like you said before, I didn't find any article that explained this material in more easy way before. Then i found your article. It likes a little candle in the dark. It's enlightening. Thank you.

Best regards,

Gatot Wahyudi
Title: Nice Article   
Name: Anil
Date: 2008-07-07 5:16:46 AM
Comment:
Hi ...


Thanq very much for giving this good article. it helps me a lot...

Regards,
Anil Kumar
Title: ENTLIB   
Name: Vivek Vashisht
Date: 2008-07-01 1:17:06 AM
Comment:
Thanks Bilal.
You have done a nice job over here...
Thanks once again
Title: samples for enterprise library january 2006 version   
Name: Jayanthy
Date: 2008-03-27 6:56:39 AM
Comment:
could you please post some samples for enterprise library january 2006 version , it would be most helpful.The configuration wizard is not the same and so it is difficult to understand.Thank you in advance.
-Jayanthy
Title: Great Article   
Name: Ahmad Shadi
Date: 2008-02-18 11:56:27 AM
Comment:
Thank u very much for this powerfull article
Title: Help please   
Name: Vi
Date: 2008-01-24 9:23:03 AM
Comment:
I like this article. It`s easy to understand.
Thanks ^^

But can u help me..
I`m already install Enterprise Library January 2006, but
why my Enterprise Library Configuration won`t open?

There is an error when I build my Enterprise Library.
Maybe there is something I missed?

thanks a lot
Title: Application Blocks   
Name: Joe
Date: 2008-01-15 10:38:56 AM
Comment:
Thanks
Title: Bad article   
Name: Matt Cameron
Date: 2007-12-10 9:09:04 AM
Comment:
Pointless and Senseless man, I am sorry I wasted my 15 mins on this article because on of my friend sent it to me. I guess you need to update this article or take it off man.

Sorry again, I wouldnt had posted the comment if I would not had wasted time on this.
Title: Application Blocks   
Name: mrudula
Date: 2007-11-05 6:28:54 AM
Comment:
Hi this article is very useful .but i have small doubt.

i am using Enterprise Library 2.0 and 3.0 but i didnt find

Configuration application Block.

please help where i can find this block to encrypt my configuration settings plzzzzzzzzzzz help me

Thanks in advance
Title: Very Good   
Name: RichB
Date: 2007-10-31 3:04:11 PM
Comment:
One of the first articles I've read that actually makes me think that I might actually use this stuff.
Title: You are the MAN!!!!   
Name: Tim
Date: 2007-07-25 3:00:44 PM
Comment:
Thanks for great work!
Title: Great !   
Name: Prakash
Date: 2007-02-05 5:07:13 AM
Comment:
I am a newbie. I found this article excellent but can anyone refer me using Enterprise Library 2.0 using Visual Studio 2005 as concise and comprehensive this is !
Title: Data access block - January 2006   
Name: RVM
Date: 2006-12-08 1:25:33 PM
Comment:
I am using the last version of enterprise library(jan 06).
i need to configure my web file for dataaccess app block but... following these steps, i just see the "data access application block" node.

how can i add the other nodes, i mean, the "configuration application block", "database instances" and "database type" node?

or.. in the last version (jan 06), is this not posible?

thanks a lot
Title: Configuring Oracle Connection   
Name: Faisal
Date: 2006-12-06 2:16:43 AM
Comment:
I need to configure Oracle database connection string using Enterprise Configuration Library tool. Can you give some tips about this. Thanks :)
Title: Need for DAAB uisng Enterprise Library 2.0   
Name: venkatesh
Date: 2006-11-22 2:14:39 AM
Comment:
Its a great Article. But it would be helpful if you could provide the same using the Enterprise library 2.0.
Title: Nice one   
Name: krishna kumar
Date: 2006-08-08 2:02:41 PM
Comment:
hi so useful, simple beauty in presntation, easy for beginers
Title: EL DAAB Command Timeout   
Name: Techno_Dex
Date: 2006-07-27 5:16:28 PM
Comment:
DbCommand dcCommand;
DataSet dsDataSet
string[] saTableNames;

In the new EL DAAB, I have discovered another way to set the Commmand Timeout without modifying the EL code, but I would see the P&P people include Kirk's overloaded methods.

Use this
dcCommand = dDatabase.GetSqlStringCommand(sSQLCommand);
dcCommand.CommandTimeout = 60;
dDatabase.LoadDataSet(dcCommand, dsDataSet , saTableNames);

Instead of this
dDatabase.LoadDataSet(CommandType.Text, sSQLCommand, dsDataSet , saTableNames);
Title: Excellent !!   
Name: Dalton Cernach de Andradde
Date: 2006-07-14 4:12:43 PM
Comment:
Thats great, Its the most compelte introduction to DABB that I've seen.
Title: ELAB   
Name: Harsha
Date: 2006-07-11 1:10:15 PM
Comment:
Good One... Thanks
Title: Re:   
Name: Bilal Hadiar [MVP]
Date: 2006-06-01 2:29:16 AM
Comment:
Hi to all:
Thanks a lot for you all for the great comments you honored me with!

Hope I will always be able to deliver good content to all of you!

Regards
Title: Get Started with the Enterprise Library   
Name: Nikhil
Date: 2006-06-01 2:21:16 AM
Comment:
Very helpful article indeed.Good job.
Title: Using Data Access Block with Oracle   
Name: Hrishi
Date: 2006-05-23 12:54:09 AM
Comment:
Really nice article..

I want to know how to use Ent.Library(DAAB) for Oracle coz lot of online material is for Sql server..If any steps or links are provided for oracle use it would be really nice.
Title: Good one   
Name: Subarno
Date: 2006-04-17 5:32:53 AM
Comment:
Nice. Both concise and comprehensive.
Title: Great Title   
Name: Evangelos Hadjichristodoulou
Date: 2006-04-15 4:59:50 PM
Comment:
Great Job there Bilal
Title: Connection Timeout!!   
Name: Kirk Rose
Date: 2006-04-04 1:00:53 PM
Comment:
I had the same issue and had to write code to extend SQLHelper ExecuteDataset methods. I basically copied one of the overloaded ExecuteDataSet methods, added a timeout parm and then just added a single statement to set the command's timeout...

Hope this helps...

Public Overloads Shared Function ExecuteDataset(ByVal connection As SqlConnection, _
ByVal commandType As CommandType, _
ByVal commandText As String, _
ByVal commandTimeout As Integer, _
ByVal ParamArray commandParameters() As SqlParameter) As DataSet
If (connection Is Nothing) Then Throw New ArgumentNullException("connection")
' Create a command and prepare it for execution
Dim cmd As New SqlCommand
Dim ds As New DataSet
Dim dataAdatpter As SqlDataAdapter
Dim mustCloseConnection As Boolean = False
cmd.CommandTimeout = commandTimeout
PrepareCommand(cmd, connection, CType(Nothing, SqlTransaction), commandType, commandText, commandParameters, mustCloseConnection)
Title: Connection Timeout!!   
Name: dan
Date: 2006-03-06 12:12:47 AM
Comment:
Hi...
DAAB is awesome. But i have one problem using it. Im using DAAB in my application. Im using SQLHelper and its methods(ExecuteDataset to be specific) in Data Access. But my query takes almost 15 minutes to execute. But while executing this query my browser times out after 30 seconds. So after searching i found out that 30 sec is default for any SQL connection and can be changed using command objects timeout property. Since im not using command object can you please tell me how to set timeout using SQLHelper?.
Title: Oracle Connection   
Name: Bilal Haidar
Date: 2006-02-08 7:13:37 AM
Comment:
Hello
You should create a new Profile provider to make it work with Oracle.
This is out of the topic in this article but you can find many such links if you try to do a simple search on google.com

Thanks.
Title: Getting error when connect to oracle   
Name: Toral Gohil
Date: 2006-02-08 7:12:01 AM
Comment:
Hello Bilal Haidar,

I am getting error when i tried to connect to the oracle. I have done all the things that you have stated above. But still it says service not found. Please if you can help me for solving the same. My Id is gohil_toral@yahoo.co.in
Title: Excellent Job!   
Name: Patrick
Date: 2005-12-09 10:40:46 PM
Comment:
Awesome job on this article!
Title: Mr.   
Name: Bilal Haidar [MVP]
Date: 2005-11-04 11:11:39 AM
Comment:
Thank you all for your great comments. I pray to God to be able to present the best for you all the times.

Regarding the "LIKE", I beleive it is better to use "Like" when comparing string or varchars in SQL Server.

Regards
Title: LIKE?   
Name: yourq1
Date: 2005-11-03 8:18:49 PM
Comment:
Haidar,
Why is LIKE used and not equals (=) ?
I understand it's an nchar(5). Ex.:

DELETE
[Customers]
WHERE
[CustomerID] LIKE @CustomerID

That seems to me like an inappropriate generalization?
Please explain! Thanks.
Title: OUTSTANDING   
Name: Sgt13Echo
Date: 2005-11-02 12:28:11 PM
Comment:
This was BY FAR the best tutorial I have seen on application blocks!

I wish more were like this ONE

I GIVE IT A 10

Out!
Title: Ms.   
Name: Preeti
Date: 2005-10-18 10:18:20 AM
Comment:
Hi Bilal,

Excellent code. Very Clear.
I did the intial configuration the way its said here. I keep getting the error like "Parser Error Message: Invalid section name.
The section 'dataConfiguration' does not
exist in the requested configuration file ...."
Could you please help me resolve it?

Actually I am trying to have data access as a seprate class library and then include that dll into my web application.
Title: How to call datareader from another Class   
Name: Tanweer
Date: 2005-09-27 6:12:44 PM
Comment:
I want to access datareader from another class how I can do that.

using (IDataReader dataReader = db.ExecuteReader(dbCommandWrapper))
{
while (dataReader.Read())
Title: Strongly Typed DataSet (to Rosie)   
Name: sloan
Date: 2005-09-07 6:33:54 PM
Comment:
Public Function TitlesDSGet() as MyStronglyTypedDS

dim returnDS as MyStronglyTypedDS = new MyStronglyTypedDS

Dim db As Database = DatabaseFactory.CreateDatabase()

Dim dbCommandWrapper As dbCommandWrapper = db.GetStoredProcCommandWrapper("dbo.uspTitlesGetAll")


db.LoadDataSet(dbCommandWrapper, returnDS, New String() {"Titles", "Publishers"})


Return returnDS

naturally, this example has the strongly typed dataset having 2 tables "Titles" and "Publishers", and the usp will return 2 result sets.

I have only tried that code on Sql Server 2000, fyi.
Title: Re: Richard   
Name: Bilal Haidar [MVP]
Date: 2005-08-25 2:28:10 AM
Comment:
Hi Richard:
I am glad this article was of help to you.
Thanks for the comments on my blog, I did some changes, check them when you have time.

Regards
Title: Very Helpful   
Name: Richard Dudley
Date: 2005-08-24 2:10:02 PM
Comment:
Great job, Bilal! I struggled with this thing for a while, and eventually gave it up for the old DAAB. After I read your article, I figured I'd try the EL one more time, and it's working in my application now. Thanks!
Title: Very good   
Name: Tushar
Date: 2005-08-22 2:05:08 PM
Comment:
Do you have more such examples using other Application Blocks in ASP.NET applications...or do you know of any other links or resources that contains examples using other application blocks...???

Thanks...
Title: strongly type dataset   
Name: Rosie
Date: 2005-08-04 4:51:56 PM
Comment:
Can you use DBCommandWrapper to load a strongly typed dataset. LoadDataSet and ExecuteDataSet does not seem to work
Title: Re: how to use daab in asp.net   
Name: Bilal Haidar [MVP]
Date: 2005-08-04 1:50:47 AM
Comment:
Download the accompanying files from this article, there is a Web application in ASP.NET that tests all the mentioned methods.

Regards
Title: how to use daab in asp.net   
Name: jay
Date: 2005-08-03 10:54:57 PM
Comment:
how can i use daab in asp.net?
Title: Re:   
Name: Bilal Haidar [MVP]
Date: 2005-08-03 9:04:14 AM
Comment:
You can do something like that:

// Build the SQL Query Here
string sqlCmd = "SELET CustomerName FROM Customers WHERE CustomerID=1";
DbCommandWrapper dbCommandWrapper = db.GetStoredProcCommandWrapper(sqlCmd, CustomerID);
string CustomerName = (string)db.ExecuteScalar(dbCommandWrapper);

You can use the GetSqlStringCommandWrapper() too, if you don't have any parameters.

Hope that helps,
Title: Re: Re: UpdateDataSet()   
Name: Dave
Date: 2005-08-03 8:50:04 AM
Comment:
Maybe I'm missing something here. I'm using the latest release of Enterprise Library with Visual Studio 2005 Beta2. I only have two overloads for GetStoredProcCommandWrapper - with and without parameters. There aren't any overloads for GetSqlStringCommandWrapper. I even made sure to open up the source projects and check there.

dave
Title: Re: UpdateDataSet()   
Name: Bilal Haidar MVP
Date: 2005-08-02 4:58:40 PM
Comment:
Hello, check all the overloads of the method GetStoredProcCommandWrapper, it has several overloads one of them you can pass inline query.
If still couldn't find it, ping me again please.

Regards
Title: Oracle SQL statements   
Name: Dave
Date: 2005-08-02 4:40:45 PM
Comment:
I'm working with both MS Sql and Oracle on this project. Most MS DBA's seem to favor stored procedures and most Oracle DBA's seem to favor using inline SQL. I am having a real hard time trying to get the UpdateDataSet method to work with SQL statements instead of stored procedures. Can you show an example of this?

Thanks, dave
Title: Good Job   
Name: Gregg
Date: 2005-07-30 12:40:16 PM
Comment:
Nice. Both concise and comprehensive.
Thank you for taking the time to put this together.
I now have a much clearer understanding than I did before.
Title: Eng.   
Name: Bilal Haidar [MVP]
Date: 2005-07-25 5:05:50 PM
Comment:
I am so happy I was able to make your life easy by providing such an article.
I am glad you liked it,
Thank you for the time you took to read it.

Best of luck,
Regards
Title: Superb   
Name: Rajeev Gopal
Date: 2005-07-25 4:24:42 PM
Comment:
This one is really helpful for the beginners and saves lot of time for them.

Kudos Bilal!
Title: Very Well placed   
Name: Farhan Shaikh
Date: 2005-07-25 12:08:30 PM
Comment:
smooth article and very well written by working with Microsoft Enterprise DAAB Lib. can you put your thought for Exception Handling and Cypto...
Title: Get Started with the Enterprise Library   
Name: Vishal
Date: 2005-07-25 3:19:38 AM
Comment:
It's really helpful article with examples, so that easy to understand as well to implement

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 10:32:47 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search