在没有 DataAdapter 的 ADO.NET 中进行批处理

发布于 2024-07-11 04:48:34 字数 64 浏览 4 评论 0原文

是否可以在 ADO.NET 中实现多个存储过程调用(执行更新/删除)的批处理,而无需借助 DataAdapter?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆伤 2024-07-18 04:48:34

您可以尝试使用 System.Data.SqlClient.SqlCommandSet。 它实际上是内部的,但是 Ayende 制作了一个包装器以将其公开

代码当前托管在 sourceforge

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.

看海 2024-07-18 04:48:34

您的 SQL 文本可以包含多个命令。 如果返回多个结果集,则可以使用 DataReader 并使用 NextResult 函数。 我经常做的是将 SQL 存储为嵌入资源来执行,然后加载该文本。 如果它包含参数,则像平常一样设置参数。

例如,我有一个文件:

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

我这样执行:

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();
}

请注意,MvcApplication.GetResource不是内置函数 - 它是您必须编写的函数...这是我的:

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文