Executing Stored Procedures
The easiest way to use the StoredProcedure class is this:
DataSet ds = new StoredProcedure("myStoredProcName")
.AddParameter("@first", 42, DbType.Int, 0)
.AddParameter("@second", "hello", DbType.String, 50)
.GetDataSet();This example will execute a stored procedure on the database defined by your first connection string, passing two parameters and returning a DataSet.
void Stored Procedures
You can execute a stored procedure that does not return anything by using the Execute method instead of the GetDataSet method.
new StoredProcedure("myStoredProcName")
.AddParameter("@first", 42, DbType.Int, 0)
.AddParameter("@second", "hello", DbType.String, 50)
.Execute();scalar Stored Procedures
You can execute a stored procedure that returns a scalar by using the Execute method instead of the GetDataSet method.
object scalar = new StoredProcedure("myStoredProcName")
.AddParameter("@first", 42, DbType.Int, 0)
.AddParameter("@second", "hello", DbType.String, 50)
.ExecuteScalar();Specifying a connection
You can specify which connection string to use by passing the connection string name in the constructor:
DataSet ds = new StoredProcedure("myStoredProcName", "myConnection")
.AddParameter("@first", 42, DbType.Int, 0)
.AddParameter("@second", "hello", DbType.String, 50)
.GetDataSet();Deadlocks
If two stored procedure invocations lock each other inside transactions, the RDBMS will kill one of them to unlock the other. When this happens, it is correct behaviour to resubmit the killed stored procedure. The StoredProcedure class takes care of this scenario, but only if enabled. To enable deadlock resubmission, include this key in your .config file.
<configuration>
<appSettings>
<!-- This will let the StoreProcedure class
retry submitting deadlock victims 5 times. -->
<add name="StoredProcedureDeadlockRetries" value="5" />
</appSettings>
</configuration>
I have tried to use this Stored Procedure Class and find it does not work. 1. It fails to find the first connection (I only have one). 2. Passing the connection string name, it then cannot find the provider. 3. Change the provider name to default (System....) then it fails on a user login by the SQlServerExpress.2005. The login is ASPNET from MSV2008! Do I have the latest version?? (11/28/2008) m_dgen27@cox.net
You need to add a <clear/> before the the <add/> in the connection strings section.