views:

200

answers:

2

Hello,

Is it possible to implement batching of multiple stored procedure calls (doing updates/deletes) in ADO.NET without resorting to DataAdapters?

+1  A: 

You're SQL text can contain multiple commands. If you return multiple result sets, then you can use a DataReader and use the NextResult function. What I often do is store the SQL to execute as an Embedded Resource, then load that text. If it contains parameters, then set the parameters just like you would normally.

For example, I have a file:

UPDATE dbo.QuotePricedLineItem
SET fkQuoteVendorLineSet = NULL
FROM dbo.QuotePricedLineItem qpli
INNER JOIN dbo.QuoteLineItem qli ON qpli.Id = qli.Id
WHERE qli.fkQuote = @quoteId AND qpli.fkQuoteVendorLineSet = @ciscoConfigId

DELETE CiscoQuoteLineItem
FROM CiscoQuoteLineItem cqli
INNER JOIN QuoteLineItem qli ON cqli.Id = qli.Id
WHERE qli.fkQuote = @quoteId AND cqli.fkCiscoQuoteVendorLineSet = @ciscoConfigId

that I execute as such:

using (SqlConnection conn = DbUtils.CreateConnection() as SqlConnection)
{
    conn.Open();

    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = MvcApplication.GetResource("SQL.DemoteCiscoQuoteLineItems.sql");
    cmd.Parameters.AddWithValue("@quoteId", q.Id);
    cmd.Parameters.AddWithValue("@ciscoConfigId", configSetId);
    cmd.ExecuteNonQuery();
}

Note that MvcApplication.GetResource is not a built in function - it's one you have to write... here's mine:

public static string GetResource(string p)
{
    Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("CortexQuoting.Res." + p);
    if (s == null) return null;

    StreamReader sw = new StreamReader(s);
    string ss = sw.ReadToEnd();
    sw.Close();
    return ss;
}
Michael Bray
+1  A: 

You could try using System.Data.SqlClient.SqlCommandSet. It's actually internal, but Ayende made a wrapper to make it public.

Code is currently hosted in sourceforge.

Mauricio Scheffer

related questions