Tuesday, 9 February 2010

Using Enterprise Library's Data Access Block to retrieve dataset

This is basic boilerplate code for the use of the Microsoft Enterprise Library Data Access Block (EntLib DAB) using VB.net to Retrieve data using a Stored procedure.


Imports Microsoft.Practices.EnterpriseLibrary.Data


Try
 'Declare Database - All you need to do is enter the connection string referenced in the  Web.Config. 
 Dim db As Database = DatabaseFactory.CreateDatabase("connectionString")


'Set up the database command and enter the name of the stored procedure you want to use
 Dim dbCmd As DbCommand = db.GetSqlStringCommand("usp_RemoveIndividualSystemID")


'It is important to set the "commandType" to StoredProcedure so it will look for a proc, rather than interpret the String as a SQL statement
dbCmd.CommandType = CommandType.StoredProcedure


'Add the parameters that are required for the proc, in this case, it expects a value called @param, and we are assinging as a type of 'String' and giving it a value of "Parameter Value"
db.AddInParameter(dbCmd, "@param", DbType.String, "Parameter Value")
'Declare a dataset and Execute the db.ExecuteDataSet command, which will fill ds with the data returned from the database
 Dim ds As DataSet = db.ExecuteDataSet(dbCmd)

Catch ex As Exception

'Exception Handling
End Try

No comments:

Post a Comment